[英]tkinter python program structure
I'm new to python programming, and I have a bit of trouble with the program structure: When I make my GUI in the main python part then the code works:我是 python 编程的新手,我在程序结构方面遇到了一些麻烦:当我在主要 python 部分制作我的 GUI 时,代码工作:
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
root.geometry("800x480")
def cb_Gebruiker():
btnUser["text"]= "changed"
btnUser = tk.Button(root, text="User",command = cb_Gebruiker)
btnUser.place(x=1,y=1,width="300",height="73")
root.mainloop()
When I make my GUI in a function, the btn variable is local, so this doesn't work当我在函数中创建 GUI 时, btn 变量是本地的,所以这不起作用
def MakeBtn():
btnUser = tk.Button(root, text="User",command = cb_Gebruiker)
btnUser.place(x=1,y=1,width="300",height="73")
def cb_Gebruiker():
btnUser["text"]= "changed"
MakeBtn()
root.mainloop()
Now I have a program that's rather large and I want my GUI in a separate file, but then I can't access my GUI components... And I can't seem to be able to find a tutorial on how to structure a program (python has to many possibilities: script, module, object oriented,..)现在我有一个相当大的程序,我希望我的 GUI 在一个单独的文件中,但是我无法访问我的 GUI 组件......而且我似乎无法找到有关如何构建程序的教程(python 有很多可能性:脚本、模块、面向对象……)
How do I solve this?我该如何解决这个问题?
You will need a lambda
to delay the call to the command with a parameter.您将需要一个lambda
来延迟对带有参数的命令的调用。
def MakeBtn():
btnUser = tk.Button(root, text="User", command = lambda: cb_Gebruiker(btnUser))
btnUser.place(x=1, y=1, width="300", height="73")
def cb_Gebruiker(btnUser):
btnUser["text"] = "changed"
MakeBtn()
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.