简体   繁体   中英

Tkinter Stop Program Execution Button

I am attempting to construct a simple game using the Tkinter GUI. The first portion of the game is asking if one wants to play. My Tkinter looks as follows:

window = tk.Tk()

window.rowconfigure(0, minsize=50, weight=1)
window.columnconfigure([0, 1, 2], minsize=50, weight=1)

lbl_value = tk.Label(master=window, text="Welcome!")
lbl_value.grid(row=0, column=1)

lbl_value = tk.Label(master=window, text="Would you like to play?")
lbl_value.grid(row=1, column=1)

btn_yes = tk.Button(master=window, text="Yes", command=window.destroy)
btn_yes.grid(row=2, column=0, sticky="nsew")

btn_no = tk.Button(master=window, text="No", command=**??**)
btn_no.grid(row=2, column=2, sticky="nsew")

window.mainloop()

The "Yes" button is operating as intended, and continuing with the rest of the code. The "No" button however, I am not sure how to get it to exit the the running of the program all together (not just the window), or even better, how to get it to open an additional window stating "Maybe next time", then ending the program when that window is exited out of.

Create a new function where you can define a new tkinter screen which will appear when user press NO button See

def no_button_function():   # function that will be called when user pressed no button
    new_window = tk.Toplevel(window)
    new_window.title('See You Next Time')
    # create the good bye message using labels and buttons accordingly 

and now pass this function to NO buttton

btn_no = tk.Button(master=window, text="No", command=no_button_function)
btn_no.grid(row=2, column=2, sticky="nsew")

Hope You Got This. Tick Mark If Satisfied

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM