Tkinter GUI 问题 [英] Tkinter GUI problems
问题描述
我正在研究一个 项目,但进展并不顺利.
这段代码:
from tkinter import *定义新文件():new = Label(root, text="关于\n")定义打开文件():openf = Label(root, text="关于\n")定义关于():about = Label(root, text="关于\n")根 = Tk()菜单 = 菜单(根)root.config(菜单=菜单)文件菜单 = 菜单(菜单)menu.add_cascade(标签=文件",菜单=文件菜单)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)帮助菜单 = 菜单(菜单)menu.add_cascade(label="Help", menu=helpmenu)helpmenu.add_command(label="About...", command=About)body = Label(root, text="")主循环()
不符合我的需要.
当您单击 file > 时,它应该会写入 certant 消息.新建
, file >打开
和help>关于
.
它什么都不做
我怎样才能让它做我想要的?
from Tkinter import *根 = Tk()菜单 = 菜单(根)root.config(菜单=菜单)文件菜单 = 菜单(菜单)my_label = Label(root, text="选择菜单")my_label.place(x=10,y=10)def my_command(查询):my_label.config(文本=查询)menu.add_cascade(标签=文件",菜单=文件菜单)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)帮助菜单 = 菜单(菜单)menu.add_cascade(label="Help", menu=helpmenu)helpmenu.add_command(label="About...", command=lambda x = "About...":my_command(x))body = Label(root, text="")主循环()
@Lafexlos 是正确的.您可以在 GUI
设计中使用所有 python 类型(list、dict、var 等...),最重要的一点 如何管理所有 GUI 元素
,所以都需要一个可访问状态.
不要破坏
任何GUI元素,更改并重用它.
不要在mainloop
中包含任何外部命令(如:文件、网络等)
所有 GUI 应用程序都需要 3 个临界区:INIT >>构建 >>RUN
否则你会很痛苦.
使用 GUI 元素 text
作为 variable
,当然如果所有元素都可以访问!
我希望有帮助并接受@Lafexlos 不是我的回答!此代码适用于 Python2.7
im working on a project but its not going so well.
this code:
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()
Doesnt work how I need it.
its suposed to write certant messages when you click file > new
, file > open
and help > about
.
IT DOES NOTHING
how can I make it do what I want?
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 is correct.
You can use all python types(list,dict,var and more...) on GUI
design, most important point how to manage all GUI elements
, so all need an accessible status.
Don't destroy
any GUI element, change and reuse it.
Don't include any external command to mainloop
(like:file,network, etc.)
All GUI application required 3 critical section : INIT >> BUILD >> RUN
Otherwise you got a lot painful.
Use GUI elements text
as variable
, of-course if all element is accesible !
I hope helpful and accept @Lafexlos answer not my ! This code work on Python2.7
这篇关于Tkinter GUI 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!