简体   繁体   English

Tkinter 停止程序执行按钮

[英]Tkinter Stop Program Execution Button

I am attempting to construct a simple game using the Tkinter GUI.我正在尝试使用 Tkinter GUI 构建一个简单的游戏。 The first portion of the game is asking if one wants to play.游戏的第一部分是询问一个人是否想玩。 My Tkinter looks as follows:我的 Tkinter 如下所示:

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. “是”按钮按预期运行,并继续代码的 rest。 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.然而,“否”按钮,我不知道如何让它一起退出程序的运行(不仅仅是窗口),或者更好的是,如何让它打开一个额外的 window 说明“也许下次",然后在 window 退出时结束程序。

Create a new function where you can define a new tkinter screen which will appear when user press NO button See创建一个新的 function ,您可以在其中定义一个新的 tkinter 屏幕,该屏幕将在用户按下NO 按钮时出现 请参阅

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现在将此function传递给NO 按钮

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满意就打勾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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