我正在尝试使用python 3.4学习编码。我构建了这个迷你GUI,但我无法让它工作。即使我按下按钮1,它每次打印出2。我想要做的就是当我点击按钮打开时在标签上打印出1,用if按钮打开2打印出来。
from tkinter import *
root = Tk()
root.geometry("500x500")
root.title("Test")
def button_press():
if one:
x = 1
display.configure(text=x)
if two:
x = 2
display.configure(text=x)
display = LabelFrame(root, bg="red", width="462", height="60")
display.pack()
one = Button(root, text="1", width="15", height="5",command=button_press)
one.pack(side="left")
two = Button(root, text="2", width="15", height="5", command=button_press)
two.pack(side="left")
root.mainloop()
答案 0 :(得分:1)
您有两种方式:
或为每个按钮使用不同的功能
或者传递一个带有一个参数的lambda。
bt … command = fct1
bt … command = fct2
或者使用lambda ......
from tkinter import *
root = Tk()
root.geometry("500x500")
root.title("Test")
def button_press(var):
display.configure(text=var)
display = LabelFrame(root, bg="red", width="462", height="60")
display.pack()
one = Button(root, text="1", width="15", height="5",command=lambda : button_press(1))
one.pack(side="left")
two = Button(root, text="2", width="15", height="5", command=lambda : button_press(2))
two.pack(side="left")
root.mainloop()
答案 1 :(得分:1)
说实话,我还没有完成任何Tkinter。那说下面的代码片段
if one:
x = 1
display.configure(text=x)
if two:
x = 2
display.configure(text=x)
看起来很奇怪。基本上,您正在检查one
和two
是否与None
相等。这没有意义,因为if VARIABLE
True
总是VARIABLE
0
不等False
,None
或None
。< / p>
嗯,它们肯定不等于False
,0
或Button
,因为这两个变量都代表one != None
的实例。所以你要做的是:
True
two != None
,则设置标签 - 发生这种情况并将标签设置为&#34; 1&#34;但是...... True
def button_press():
if one:
x = 1
display.configure(text=x)
elif two:
x = 2
display.configure(text=x)
,则设置标签 - 这也会发生,它也会覆盖标签设置的先前值2,即&#34; 1&#34;至&#34; 2&#34;。所有这一切都会立即发生,因此您无法看到过渡,并留下只有&#34; 2&#34;显示。我们可以进一步重写您的代码,以便揭露此错误:
def button_press():
if one:
x = 1
display.configure(text=x)
else:
x = 2
display.configure(text=x)
甚至
1
在上述两种情况下,第一个语句都将被执行,您将始终获得2
而永远不会True
。检查if-else块中的第一个逻辑表达式并且它是elif
后,你进入那里,然后让整个块完全不接触else
或== "one"
。
有多种方法可以解决这个问题。您可以为按钮指定一个名称(以便您可以根据其名称区分每个按钮),以防您仍希望使用单个函数处理两个按钮,然后比较调用事件处理的对象的名称。功能是== "two"
或one = Button(root, text="1", width="15", height="5",command=button_press1)
one.pack(side="left")
two = Button(root, text="1", width="15", height="5",command=button_press2)
two.pack(side="left")
。但是在大多数(所有?)UI框架中,通常的做法是为每个处理组件支持的事件的UI组件提供单独的函数(如果组件支持多个事件,则为多个函数)。
value changes
这个解决方案类似于@ Clodion,但是没有lambdas,因为我不确定你是否熟悉这个概念并把它扔进去可能有点令人困惑。
在使用UI时,我建议遵循一些基本原则。如果您的组件支持 - 请说 - resized
,clicked
,pressed down
,pressed up
,g++ mushroom.c -o shroom.exe -lm -I src\ -I src\include\ src\doublefann.c
等。(不要...知道这些在Tkinter中的名称,但我猜它至少支持其中一些) - 你将为每个事件创建一个单独的函数。这不仅使您的代码更具可读性,而且还迫使您学习一种做事事实上标准的方式,无论您是使用Tkinter,Qt,.NET,Swing,AngularJS编写UI还是等等。
答案 2 :(得分:-1)
这就是你想要的。可能是新的语法,但它会让你习惯看到它。不要被吓倒。
from tkinter import *
root = Tk()
class BuildGui():
def __init__(self, parent):
self.parent = parent
self.parent.geometry("500x500")
self.parent.title("Building a Gui")
self.parent.configure(background="steel blue")
self.parent.resizable(0,0)
self.parent.grid()
self.CreateFrames()
self.AddButtons()
def CreateFrames(self):
"""Create The Frames"""
self.Frm = Frame(self.parent, bg = "steel blue")
self.Frm.grid(row = 0, column = 0)
def AddButtons(self):
self.Btn1 = Button(self.Frm, text = "Button1", height = 2, width = 10, fg = "black", activebackground = "yellow", bg = "light blue", command = self.Button1Pressed)
self.Btn2 = Button(self.Frm, text = "Button2", height = 2, width = 10, fg = "black", activebackground = "yellow", bg = "light blue", command = self.Button2Pressed)
self.Btn1.grid(row = 0, column = 0, padx = 40, pady = 40)
self.Btn2.grid(row = 0, column = 1, padx = 40, pady = 40)
def Button1Pressed(self):
self.Lbl1 = Label(self.Frm, text = "Button1pressed!", font = ("Calibri", 12))
self.Lbl1.grid(row = 1, column = 0, padx = 20, pady = 20)
def Button2Pressed(self):
self.Lbl2 = Label(self.Frm, text = "Button2pressed!", font = ("Calibri", 12))
self.Lbl2.grid(row = 1, column = 1, padx = 20, pady = 20)
buildgui = BuildGui(root)
root.mainloop()