【问题标题】:I have this code python, while running the code does not show any error but when Please click on the button does not show the result [duplicate]我有这个代码python,在运行代码时没有显示任何错误但是当请点击按钮时没有显示结果[重复]
【发布时间】:2015-09-21 13:55:04
【问题描述】:

我有这段代码python,在运行代码时没有显示任何错误但是当请点击按钮时没有显示结果

from tkinter import *

root = Tk()

def cal():
    l=[15,7,9,11]
    s=0
    for i in range(0,len(l)):
        s=s+l[i]
return s

b = Button(root, text="ok", command=cal())
b.pack()

label = Label(root, text=cal())
label.pack()

root.mainloop()

【问题讨论】:

  • 回调函数应该做什么?

标签: python tkinter


【解决方案1】:

您需要存储对函数command=cal 的引用,而不是存储函数的返回值command=cal()

【讨论】:

  • @KumarPyth:喂?它写在答案中:command=cal.
  • 那仍然不会改变文字,st = StringVar();label = Label(root, textvariable=st),你可以在回调函数中设置标签,st.set(s)
  • 当然是这样。
  • 哦,是的,op 在他们的代码中仍然有很多问题,但这解决了他们帖子中的问题。其余的留给读者作为练习。
猜你喜欢
  • 2019-06-17
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
相关资源
最近更新 更多