[英]Tkinter GUI problems
我正在研究 github項目,但進展不順利。
此代碼:
from tkinter import *
def NewFile():
new = Label(root, text="about \n")
def OpenFile():
openf = Label(root, text="about \n")
def About():
about = Label(root, text="about \n")
root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=NewFile)
filemenu.add_command(label="Open...", command=OpenFile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=About)
body = Label(root, text="")
mainloop()
不工作,我怎么需要它。
當您單擊file > new
, file > open
和help > about
它時,它可能會寫入證書信息。
沒什么
我怎樣才能讓它做我想要的?
您沒有在小部件上使用幾何圖形管理器(pack / grid / place),因此tkinter不會向您顯示這些圖形。
而且,您可以在全局范圍內創建所有三個標簽並打包並忘記點擊,而不必創建每個標簽的新標簽,也可以僅創建一個標簽並根據需要更改其值。
from Tkinter import *
root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
my_label = Label(root, text="Select Menu")
my_label.place(x=10,y=10)
def my_command(query):
my_label.config(text=query)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=lambda x = "New":my_command(x))
filemenu.add_command(label="Open...", command=lambda x = "Open...":my_command(x))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.destroy)
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=lambda x = "About...":my_command(x))
body = Label(root, text="")
mainloop()
@Lafexlos是正確的。 您可以在GUI
設計上使用所有python類型(list,dict,var等),最重要的一點是how to manage all GUI elements
,因此所有how to manage all GUI elements
都需要可訪問的狀態。
不要destroy
任何GUI元素,更改和重用它。
不要在mainloop
包含任何外部命令(例如:文件,網絡等)
所有GUI應用程序都需要3個關鍵部分: INIT >> BUILD >> RUN
否則您會感到很痛苦。
當然,如果所有元素都可以使用,請使用GUI元素text
作為variable
!
我希望有幫助並接受@Lafexlos回答不是我的! 這段代碼適用於Python2.7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.