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()
TkandButtonwithout other elements and settings. And you could addimportandmainloopso we could simply copy and run.os.systemmay suggests that you use Windows (because it doesn't exists on Linux). You could confirm this information in question.print()(andprint(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.os.systembut I think all problem is becauseos.systemis blocking all code andmainloopcan't get clicked from system and skip them - and they waits - so it gets them after you close helpfile. (I tested it withtime.sleep()and it gives the same effect). It would need to run helpfile insubprocessorthreadormultiprocessing. OR it would need to find method to remove events from queue. Similar problem python - Prevent a queue of events in tkinter - Stack Overflow