蟒蛇和放大器; Tkinter的 - 按钮命令设置标签textvariable问题 [英] Python & Tkinter - buttons command to set label textvariable issue
问题描述
我与Tkinter的玩弄和建设从基础了一个计算器。要尝试理解为学习尽可能多的关于事件和图书馆,我可以当我走。
现在我在一个地步,我只是想在按钮上的按钮的值传递给顶部的标签。
我用一个循环营造的大部分按钮,以避免重复code,但现在唯一的价值被传递到在标签中textvariable是最后一个项目,'。',在我的按钮列表和我不知道这是为什么。有人能帮忙吗?
code如下:
从Tkinter的进口*
导入Tkinter的传统知识#主窗口
根= TK()
root.title(计算器)#键设置
按钮= ['1','2','3','4','5','6','7','8','9','0','+',' - ' '/','*','。']sum_value = STRINGVAR()#输出窗口
output_window = tk.Label(根,textvariable = sum_value,宽度= 20,身高= 2).grid(行= 0,columnspan = 3,粘=(E,W))#创建按钮
R = 1
C = 0因为我在按钮:
如果℃下2:
BI = tk.Button(根,文本=我,命令=拉姆达:sum_value.set(我),padx = 5,pady = 3).grid(行= R,列= C,粘滞=(N,S,E ,W))
C + = 1
其他:
BI = tk.Button(根,文本=我,命令=拉姆达:sum_value.set(我),pady = 3).grid(行= R,列= C,粘滞=(N,S,E,W))
R + = 1
C = 0#清晰等于按钮
明确= tk.Button(根,文本='=',padx = 5,pady = 3).grid(行= 6,列= 0,粘=(N,S,E,W))
明确= tk.Button(根,文本=清除,padx = 5,pady = 3).grid(行= 6,列= 1,columnspan = 2,粘=(N,S,E,W))root.mainloop()
这是在一个循环中声明 在你的情况,你可以改变你的的λ
常见的错误。变量 I
在的λ
是名为的时候被评估,而不是当它的定义的,因此所有的功能最终使用>我在循环的最后一次迭代中分配给 lambda表达式
这种形式,那么它应该工作:
=双向tk.Button(...,命令=拉姆达I = I:sum_value.set(我),...)
bi.grid(...)
另外请注意,如果你这样做 =双向按钮(...)。格(...)
,双向
将被分配电网
函数,即无
的结果。你不需要双向
在这种情况下,无论如何,所以也没有多大关系,但这是另一个常见的问题,所以最好不要开发习惯。
I am playing around with Tkinter and building a calculator from the base up. To try and understand as learn as much as possible about the event and the library as I can while I go along.
Right now I am at a point where I simply want the buttons to pass the value on the button to the label at the top.
I've used a for loop to create most of the buttons to avoid redundant code but now the only value being passed onto the textvariable in the label is the last item, '.', in my buttons list and I am not sure why that is. Can someone help?
code below:
from Tkinter import *
import Tkinter as tk
# main window
root = Tk()
root.title('Calculator')
# button set
buttons = ['1','2','3','4','5','6','7','8','9','0','+','-','/','*','.']
sum_value = StringVar()
# output window
output_window = tk.Label(root, textvariable=sum_value, width=20, height=2).grid(row=0, columnspan=3, sticky=(E,W))
# button creation
r=1
c=0
for i in buttons:
if c < 2:
bi = tk.Button(root, text = i, command = lambda: sum_value.set(i), padx = 5, pady = 3).grid(row = r, column = c, sticky = (N,S,E,W))
c += 1
else:
bi = tk.Button(root, text = i, command = lambda: sum_value.set(i), pady = 3).grid(row = r,column = c,sticky = (N,S,E,W))
r += 1
c = 0
# clear and equals button
clear = tk.Button(root,text='=',padx = 5, pady=3).grid(row=6,column=0,sticky=(N,S,E,W))
clear = tk.Button(root,text='CLEAR',padx = 5, pady=3).grid(row=6,column=1, columnspan = 2,sticky=(N,S,E,W))
root.mainloop()
This is a common pitfall with declaring a lambda
in a loop. The variable i
is evaluated when the lambda
is called, not when it is defined, thus all the functions end up using the value that was assigned to i
in the final iteration of the loop. There are some ways to fix this, e.g. to use a parameter and assign it a default-value, that will be evaluated when the function is defined.
In your case, you can change your lambdas
to this form, then it should work:
bi = tk.Button(..., command=lambda i=i: sum_value.set(i), ...)
bi.grid(...)
Also note, that if you do bi = Button(...).grid(...)
, bi
will be assigned the result of the grid
function, i.e. None
. You do not need bi
in this case anyway, so it does not matter much, but that's another common problem, so better don't develop that habit.
这篇关于蟒蛇和放大器; Tkinter的 - 按钮命令设置标签textvariable问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!