[英]How do you create a streamlined gui in python using multiple function?
I am trying to create a gui which would for example end the program once the user clicks a button, however, since the main portion of the program is located in the main() function, I cannot find a way to to end the program inside another function. This is my code:我正在尝试创建一个 gui,例如,一旦用户单击一个按钮,它就会结束程序,但是,由于程序的主要部分位于 main() function 中,我找不到在内部结束程序的方法另一个 function。这是我的代码:
import turtle
import random
from tkinter import *
from tkinter import messagebox
def main():
windowScreen = Tk()
windowScreen.title("Our cinema Application!")
lbl = Label(windowScreen, text="Welcome to our cinema Application!",font=("Arial bold",50))
lbl.grid(column=0,row=0)
btn1 = Button(windowScreen, text="View Available Movie", bg="green", )
btn1.grid(column=0,row=1)
btn2 = Button(windowScreen, text="Purchase Tickets", bg="green", command=movie)
btn2.grid(column=0,row=2)
btn3 = Button(windowScreen, text="Exit the Application", bg="red", command=action1, activebackground="white")
btn3.grid(column=0,row=3)
windowScreen.mainloop()
print("Welcome to Dan & Fred's Cinema!")
def action1():
messagebox.showinfo("Exit Application","Thank you for visiting and come again!")
mainloop().exitonclick()
exit()
Your program is almost correct, just remove mainloop().exitonclick()
.您的程序几乎是正确的,只需删除
mainloop().exitonclick()
。
exitonclick
is a function of turtle and would be used to close turle window, so it wont have any effect on tkinter window. exitonclick
是 turtle 的 function,将用于关闭 turle window,因此它不会对 tkinter window 产生任何影响。
You can either make windowScreen
a global variable (as @Tim Robert suggested) and add windowScreen.destroy()
in action1
function or have a nested function to achive the same.您可以使
windowScreen
成为全局变量(如@Tim Robert 建议的那样)并在action1
function 中添加windowScreen.destroy()
或嵌套 function 以实现相同的目的。
Using nested function, your code would look like this:使用嵌套的 function,您的代码将如下所示:
def main():
def action1():
messagebox.showinfo("Exit Application","Thank you for visiting and come again!")
windowScreen.destroy()
windowScreen = Tk()
windowScreen.title("Our cinema Application!")
btn3 = Button(windowScreen, text="Exit the Application", bg="red", command=action1, activebackground="white")
btn3.grid(column=0,row=3)
windowScreen.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.