pythontkinterwindows-11

Why does the button recognize clicks despite deactivation?


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()

Solution

  • 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