【问题标题】:How would I modify/add text to a tkinter.Label?我将如何修改/添加文本到 tkinter.Label?
【发布时间】:2016-12-12 01:53:42
【问题描述】:

我正在学习基本的 Python。我目前正在尝试创建一个只有加法和减法的简单计算器程序。我有一个问题。我不确定如何在按下按钮时将文本添加到我的 Python 标签。现在,按下“1”按钮后,我的程序会将显示标签更改为文本“1”。但是,我希望我的程序添加文本,而不是设置它。

例如,如果我按 'button 1' 5 次,它当前将重置标签文本 5 次,结果是单个 1。我希望它在按下时将数字添加到标签中,而不是替换。

按下按钮 5 次后的当前结果:1
按下按钮 5 次后请求的结果:11111

这是我当前的程序代码。如果有什么不清楚的,请问;谢谢。

from tkinter import *

window = Tk()

# Creating main label
display = Label(window, text="")
display.grid(row=0, columnspan=3)

def add_one():
    display.config(text='1')

# Creating all number buttons
one = Button(window, text="1", height=10, width=10, command=add_one)
two = Button(window, text="2", height=10, width=10)
three = Button(window, text="3", height=10, width=10)
four = Button(window, text="4", height=10, width=10)
five = Button(window, text="5", height=10, width=10)
six = Button(window, text="6", height=10, width=10)
seven = Button(window, text="7", height=10, width=10)
eight = Button(window, text="8", height=10, width=10)
nine = Button(window, text="9", height=10, width=10)
zero = Button(window, text="0", height=10, width=10)

# Placing all number buttons
one.grid(row=1, column=0)
two.grid(row=1, column=1)
three.grid(row=1, column=2)
four.grid(row=2, column=0)
five.grid(row=2, column=1)
six.grid(row=2, column=2)
seven.grid(row=3, column=0)
eight.grid(row=3, column=1)
nine.grid(row=3, column=2)

# Creating all other buttons
add = Button(window, text="+", height=10, width=10)
subtract = Button(window, text="-", height=10, width=10)
equal = Button(window, text="=", height=10, width=10)

# Placing all other buttons
add.grid(row=4, column=0)
subtract.grid(row=4, column=1)
equal.grid(row=4, column=2)

window.mainloop()

【问题讨论】:

  • 删除之前的所有标签文本,然后添加新文本。
  • 使用变量来控制标签文本的值。

标签: python user-interface tkinter calculator


【解决方案1】:

您应该为此使用 StringVar。而你的回调需要获取StringVar的当前内容,对其进行修改,并使用修改后的字符串来设置StringVar的新值。像这样:

import tkinter as tk

window = tk.Tk()

# Creating main label
display_text = tk.StringVar()
display = tk.Label(window, textvariable=display_text)
display.grid(row=0, columnspan=3)

def add_one():
    s = display_text.get()
    s += '1'
    display_text.set(s)

one = tk.Button(window, text="1", height=10, width=10, command=add_one)
one.grid(row=1, column=0)

window.mainloop()

顺便说一句,您应该尝试根据DRY 原则,使用for 循环来创建和布局您的按钮,从而使您的程序更紧凑。

另外,使用from tkinter import * 也不是一个好主意。它将 130 多个名称导入您的命名空间,如果您不小心将 Tkinter 名称用于您自己的变量或函数之一,则很容易造成名称冲突。

【讨论】:

  • 我不会说“应该使用StringVar可以会更准确。您可以使用或不使用@ 987654326@.
  • 谢谢,完美运行。你建议我为这个特定项目将我的导入更改为什么? (而不是:从 tkinter 导入 *)
  • 好点,@BryanOakley。我只是认为使用 StringVar 和朋友更容易一些。 :)
  • @jzbakos:我建议您使用import tkinter as tk,如我的代码所示。您需要使用tk.Label 等而不是Label。它需要更多的输入,但它使您的代码更容易阅读,并且它保持命名空间干净,而不是用所有这些 Tkinter 名称污染它。
  • 使用StringVar 很好,只是这不是绝对必要的,它会添加一个必须跟踪的额外对象。可以说,在设置值时使用它会更容易一些,因此您只需要决定您更喜欢哪个——一个额外的对象来管理,或者一行额外的代码来更改值。对我来说,更多对象 == 更复杂。
【解决方案2】:

您可以像下面这样定义add_one,首先获取现有值,然后将新值附加到它:

def add_one():
    current_value = display.cget("text")
    new_value = current_value + "1"
    display.config(text=new_value)

【讨论】:

    【解决方案3】:

    这是你要找的吗:

    from tkinter import *
    root = Tk()
    
    var = StringVar()
    
    def f1():
        var.set(" ")
        var.set("1")
    
    def f2():
        var.set(" ")
        var.set("2")
    
    label = Label(root, textvariable=var)
    label.pack()
    
    button1 = Button(root, text="One", command=f1)
    button1.pack()
    
    button2 = Button(root, text="Two", command=f2)
    button2.pack()
    

    ?

    【讨论】:

      猜你喜欢
      • 2021-01-25
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 2018-05-11
      相关资源
      最近更新 更多