0

Program outline: show a helpfile (PDF) on button click -> runs well

But if I press the button even though the file is still open, the frontend remembers this and then opens the file as often as it was clicked as soon as I close the helpfile. So I decided to deactivate the button.

Unfortunately it did not help. It remembers every click until the program crashes.

def openHelpfile():
    helpfile_btn.config(state=DISABLED)
    os.system("helpfile.pdf")
    helpfile_btn.config(state=NORMAL)

## FRONTEND =====================
main_window = Tk()
main_frame = Frame(main_window)
main_frame.pack(padx=10, pady=10)

status_frame = LabelFrame(main_frame, text="Programm Status")
status_frame.grid(row=3, column=0, sticky="ew", pady=5)
statusbar = Text(status_frame, height=1, width=1, font=("", 8, "normal"))
statusbar.pack(side="left", expand=True, fill="both", padx=10, pady=10)
helpfile_btn = Button(
    status_frame,
    text="?",
    command=openHelpfile,
    width=5,
    bootstyle="-secondary-outline",
)
helpfile_btn.pack(side="right", padx=10, pady=10)

main_window.mainloop()

Debugging:

def openHelpfile():
    print("in")
    helpfile_btn.config(state=DISABLED)
    print(f"state: {helpfile_btn["state"]}")
    os.system("helpfile.pdf")
    helpfile_btn.config(state=NORMAL)
    print(f"state: {helpfile_btn["state"]}")
    print("out")

## FRONTEND =====================
main_window = Tk()
main_frame = Frame(main_window)
main_frame.pack(padx=10, pady=10)

status_frame = LabelFrame(main_frame, text="Programm Status")
status_frame.grid(row=3, column=0, sticky="ew", pady=5)
statusbar = Text(status_frame, height=1, width=1, font=("", 8, "normal"))
statusbar.pack(side="left", expand=True, fill="both", padx=10, pady=10)
helpfile_btn = Button(
    status_frame,
    text="?",
    command=openHelpfile,
    width=5,
    bootstyle="-secondary-outline",
)
helpfile_btn.pack(side="right", padx=10, pady=10)

main_window.mainloop()
11
  • you could reduce code to Tk and Button without other elements and settings. And you could add import and mainloop so we could simply copy and run. Commented Jul 16 at 10:23
  • os.system may suggests that you use Windows (because it doesn't exists on Linux). You could confirm this information in question. Commented Jul 16 at 10:25
  • Maybe first use print() (and print(type(...)), print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called "print debugging" and it helps to see what code is really doing. Commented Jul 16 at 10:26
  • "print debugging" already done. everything runs as expected. button is disabled, but still notices every click. Commented Jul 16 at 11:21
  • I use Linux and I don't have os.system but I think all problem is because os.system is blocking all code and mainloop can't get clicked from system and skip them - and they waits - so it gets them after you close helpfile. (I tested it with time.sleep() and it gives the same effect). It would need to run helpfile in subprocess or thread or multiprocessing. OR it would need to find method to remove events from queue. Similar problem python - Prevent a queue of events in tkinter - Stack Overflow Commented Jul 16 at 11:39

2 Answers 2

0

os.system("helpfile.pdf") goes to next line when file is open. It doesn't wait until user close it. So helpfile_btn is deactive only for a moment because the next line makes it working again. I don't think that it's possible to do with reader that select in system. In windows you can't get access to reader. And almost you don't know to witch. Acrobate? Chrome? Firefox... Maby don't do it or make reader a part of your программе?

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

With all your advice I came to the following solution: os.system is not a good solution.

I found the alternative thanks to you in the forum:

https://stackoverflow.com/a/19453630/19577924

Sometimes it's that simple:

def openHelpfile():
    subprocess.Popen("helpfile.pdf", shell=True)

Edit:

After a few more complications, I finally decided on this option:

def openHelpfile():
    os.startfile("helpfile.pdf")

It runs on all of our servers in-house, without any problems...so far

1 Comment

You could also use subprocess.run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.