【问题标题】:Python tkinter Entry value additionPython tkinter 入口值加法
【发布时间】:2012-05-29 20:35:05
【问题描述】:

我正在制作一个 Python GUI,其中用户将 int 值输入到 Entry 小部件中,然后程序将这些值加在一起。

但是,由于某种原因,每当我试图告诉程序添加这些值时,它都会出现错误:

TypeError: unsupported operand type(s) for +: 'Entry' and 'Entry'"

我环顾四周,但找不到有关此主题的任何内容。我曾尝试将Entry 小部件声明为整数和IntVars,但它没有奏效,所以我想知道是否真的可以将Entry 值相加。

【问题讨论】:

  • 首先学习 Python 的基础知识,然后继续创建 GUI。
  • 简短回答:不,不是这样。但是,您可以访问小部件对象中包含 的值并将它们添加到一起。

标签: python tkinter ttk tkinter-entry


【解决方案1】:

首先你必须get 来自Entry 的字符串,然后将其转换为整数。

from Tkinter import *
root = Tk()

e1 = Entry(root)
e2 = Entry(root)
l = Label(root)
def callback():
    total = sum(int(e.get()) for e in (e1, e2))
    l.config(text="answer = %s" % total)
b = Button(root, text="add them", command=callback)
for widget in (e1, e2, l, b):
    widget.pack()
b.mainloop()

【讨论】:

    猜你喜欢
    • 2016-06-10
    • 2023-03-27
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多