简体   繁体   English

如何在python中使用多个function创建一个精简的gui?

[英]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.

相关问题 如何在 Python 中使用 function 创建 class 的实例? - How do you create an instance of a class using a function in Python? 使用 Python,如何从另一个 GUI 调用 tkinter GUI? - Using Python, how do you call a tkinter GUI from another GUI? 如何使用 function 在 Python 中过滤? - How do you filter in Python using a function? 如何使用与 Java 中的这个等效的算法从 Python 中的 class 创建多个对象? - How do you create multiple objects from a class in Python using an equivalent algorithm to this one in Java? 如何创建可以使用python脚本调用的postgresql函数? - How do you create a postgresql function that can be called using python script? 如何使用MySQLdb创建跨Python多个语句的事务? - How do you create a transaction that spans multiple statements in Python with MySQLdb? 如何在python中从原始输入创建多个变量? - How do you create multiple variables from raw input in python? 在Python中,如何创建新函数来修改现有字典? - In Python, how do you create a new function that will modify an existing dictionary? 你如何创建一个 function 代表 python 中的英文单词“或”? - How do you create a function representing the english word “or” in python? 你如何从python中的函数返回多个变量 - How do you return multiple variables from a function in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM