Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修改/向tkinter.Label添加文本?_Python_User Interface_Tkinter_Calculator - Fatal编程技术网

Python 如何修改/向tkinter.Label添加文本?

Python 如何修改/向tkinter.Label添加文本?,python,user-interface,tkinter,calculator,Python,User Interface,Tkinter,Calculator,我正在学习基本的Python。我目前正试图创建一个简单的计算器程序,只有加法和减法。不过我有一个问题。我不知道如何在按下按钮时向Python标签添加文本。现在,按下“1”按钮后,我的程序会将显示标签更改为文本“1”。但是,我希望我的程序添加文本,而不是设置文本 例如,如果我按“按钮1”5次,它当前将重置标签文本5次,结果是一个1。我希望它在按下时将数字添加到标签上,而不是替换 按下按钮5次后的当前结果:1 按下按钮5次后请求的结果:11111 这是我目前的程序代码。如果有什么不清楚的地方,就问吧

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

例如,如果我按“按钮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()

这就是你想要的吗:

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()

您应该为此使用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()

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


此外,使用tkinter import*中的
也不是一个好主意。它将130多个名称导入到您的命名空间中,如果您意外地为自己的变量或函数使用了Tkinter名称,则很容易产生名称冲突。

您可以定义
添加一个
,如下所示,首先获取现有值,然后向其追加新值:

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

只需删除所有以前的标签文本,然后添加新文本。使用变量控制标签文本的值。我不会说“应使用
StringVar
;可以更准确。您可以使用或不使用
StringVar
。谢谢,工作非常完美。您建议我将此特定项目的导入更改为什么?(而不是:来自tkinter import*)很好的观点,@BryanOakley。我只是觉得使用StringVar和friends会更容易一些@jzbakos:我建议您使用
导入tkinter作为tk
,如我的代码所示。您需要使用
tk.Label
等,而不是
Label
。它的输入稍微多了一点,但它使您的代码更容易阅读,并且它保持了名称空间的干净,而不是用所有的Tkinter名称污染名称空间。使用
StringVar
很好,只是这不是严格必要的,它添加了一个必须跟踪的额外对象。可以说,在设置值时使用它稍微容易一点,所以您只需决定您更喜欢哪一个——管理一个额外的对象,或者更改值的一行额外的代码。对我来说,对象越多=越复杂。