Tkinter按钮小部件自动运行命令,然后停止工作

2024-09-29 21:26:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我的窗口中目前有3个包含特定变量的条目。我的目标是能够在这些条目中键入一些内容,然后按下一个按钮来更新它们并运行一个命令。然而,我设置我的按钮运行的命令会在你启动程序时自动运行,然后变得不可用。下面是我的代码中的一部分,它显示了一个问题buttonA就是这个命令。你知道吗

#Entries
entryA = Entry(Window,text="Angle",bg=bgB,fg=fgB,bd=0)
entryA.grid(row=1,column=2)
entryA.insert(0,info['a'])
entryB = Entry(Window,text="Velocity",bg=bgB,fg=fgB,bd=0)
entryB.grid(row=2,column=2)
entryB.insert(0,info['v'])
entryC = Entry(Window,text="Initial Height",bg=bgB,fg=fgB,bd=0)
entryC.grid(row=3,column=2)
entryC.insert(0,info['iH'])
entryD = Entry(Window,text="Rotate",bg=bgB,fg=fgB,bd=0)
entryD.grid(row=5,column=7)
entryE = Entry(Window,text="Amount",bg=bgB,fg=fgB,bd=0)
entryE.grid(row=6,column=7)
entryF = Entry(Window,text="Password",bg=bgB,fg=fgB,bd=0,show='*')
entryF.grid(row=13,column=6)
#CheckBoxes
var=None
checkA = Checkbutton(Window,bg=bgB,fg=fgB,bd=0,activebackground=ActiveC,activeforeground=fgB,variable=var).grid(row=1,column=7,sticky='w')
checkB = Checkbutton(Window,bg=bgB,fg=fgB,bd=0,activebackground=ActiveC,activeforeground=fgB,variable=var).grid(row=2,column=7,sticky='w')
checkC = Checkbutton(Window,bg=bgB,fg=fgB,bd=0,activebackground=ActiveC,activeforeground=fgB,variable=var).grid(row=3,column=7,sticky='w')
checkD = Checkbutton(Window, text="",bg=bgB,fg=fgB,bd=0,activebackground=ActiveC,activeforeground=fgB,Avariable=var).grid(row=4,column=7,sticky='w')
#Buttons
buttonA = Button(Window,text="Confirm",bg=bgB,fg=fgB,activebackground=ActiveC,activeforeground=fgB,bd=0,command=updateButton('A')).grid(row=1,column=4)
buttonB = Button(Window,text="Confirm",bg=bgB,fg=fgB,activebackground=ActiveC,activeforeground=fgB,bd=0).grid(row=2,column=4)
buttonC = Button(Window,text="Confirm",bg=bgB,fg=fgB,activebackground=ActiveC,activeforeground=fgB,bd=0).grid(row=3,column=4)
buttonD = Button(Window,text="ACTIVATE",bg=bgB,fg=fgB,activebackground=ActiveC,activeforeground=fgB,bd=0).grid(row=5,column=9)
buttonE = Button(Window,text="ACTIVATE",bg=bgB,fg=fgB,activebackground=ActiveC,activeforeground=fgB,bd=0).grid(row=6,column=9)
buttonFIRE = Button(Window,text="Fire",bg=bgB,fg=fgB,activebackground=ActiveC,activeforeground=fgB,bd=0).grid(row=14,column=6,sticky='w')

Tags: textcolumnbuttonwindowbdgridrowbg
1条回答
网友
1楼 · 发布于 2024-09-29 21:26:31

command=(和bind())需要函数名(回调)——这意味着没有()和参数

command=updateButton

如果必须使用带参数的函数,则使用lambda

command=lambda:updateButton('A')

顺便说一句:现在您的函数在开始时执行,它的结果被分配给command=——有时生成分配给按钮的函数会很有用。你知道吗


顺便说一句:var = Widget(...).grid(...)None赋值给变量,因为grid()/pack()/place()返回None。你必须分两步来做

var = Widget(...)
var.grid(...)

或者删除变量,如果你不需要它

Widget(...).grid(...)

顺便说一句:见PEP 8 Style Guide for Python Code

为了使代码更具可读性,我们对变量和函数/方法使用lower_case名称,即windowbutton_aentry_a等,对类使用CamelCase名称。你知道吗

我们在每个逗号后面用空格。你知道吗

相关问题 更多 >

    热门问题