Python 在 Tkinter 中按下按钮后更新标签文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34689889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:22:56  来源:igfitidea点击:

Update label text after pressing a button in Tkinter

pythonuser-interfacetkinter

提问by Yeo Jin

I was wondering how to change the label text after clicking a button. For example:

我想知道单击按钮后如何更改标签文本。例如:

from Tkinter import *
import tkMessageBox

def onclick():
    pass

root = Tk()

root.title("Pantai Hospital")

L1 = Label(root, text='Welcome to Pantai Hospital!')
L1.pack() 
L2 = Label(root, text='Login')
L2.pack() 

L3 = Label(root, text = "Username:")
L3.pack( side = LEFT, padx = 5, pady = 10)
username = StringVar()
E1 = Entry(root, textvariable = username, width = 40)
E1.pack ( side = LEFT)

L4 = Label(root, text = "Password:")
L4.pack( side = LEFT, padx = 5, pady = 10)
password = StringVar() 
E2 = Entry(root, textvariable = password, show = "*", width = 40)    
E2.pack( side = LEFT)'`

I want to change those labels usernameand passwordand the entry field into another different label after clicking a button. How do I do that?

单击按钮后,我想将这些标签usernamepassword输入字段更改为另一个不同的标签。我怎么做?

采纳答案by furas

Answer for "how to do anything on pressing button" should be in any tutorial.
For example in effbotbook: Button

如何按按钮执行任何操作”的答案应该在任何教程中。
例如在effbot书中:按钮

Use command=to assign function name to button.

用于command=为按钮指定功能名称。

(btw: function name(or callback) means name withoutparenthesis and arguments)

(顺便说一句:函数名(或回调)表示没有括号和参数的名称)

btn = Button(root, text="OK", command=onclick)


Answer for "how to change label text" should be in any tutorial too.

如何更改标签文本”的答案也应该在任何教程中。

lbl = Label(root, text="Old text")

# change text

lbl.config(text="New text")

# or

lbl["text"] = "New text"


If you want to change Entryinto Labelthen remove/hide Entry(widget.pack_forget()) or destroy it (widget.destroy()) and create Label.

如果您想更改EntryLabel然后删除/隐藏Entry( widget.pack_forget()) 或销毁它 ( widget.destroy()) 并创建Label.

btw: you can disable Entryinstead of making Label(ent.config(state='disabled'))

顺便说一句:您可以禁用Entry而不是制作Label( ent.config(state='disabled'))



EDIT:I removed dot in lbl.["text"]

编辑:我删除了点lbl.["text"]

回答by Kishor K

write lbl.pack() after you write the button.pack() A small snippet of code to display change in value on clicking a button. This is done so that the changes made in the label will be shown after you perform the button click.

在编写 button.pack() 之后编写 lbl.pack() 一小段代码,用于显示单击按钮时值的变化。这样做是为了在您执行按钮单击后显示在标签中所做的更改。

    from tkinter import *

    root = Tk(className = "button_click_label")
    root.geometry("200x200")

    message = StringVar()
    message.set('hi')

    l1 = Label(root, text="hi")


    def press():
        l1.config(text="hello")

    b1 = Button(root, text = "clickhere", command = press).pack()

    l1.pack()

    root.mainloop()


Im just an entry level python programmer. Forgive , and do correct me if I'm wrong! Cheers!

我只是一个入门级的 Python 程序员。请原谅,如果我错了,请纠正我!干杯!

回答by shanemarvinmay

Here is an example where I created a basic gui with a label. Then I changed the label text.

这是我创建带有标签的基本 gui 的示例。然后我更改了标签文本。

import tkinter as tk
from tkinter import *
app = tk.Tk()
#creating a Label
label = Label(app,  text="unchanged")
label.pack()
#updating text 
label.config(text="changed")
app.mainloop()

回答by Jelle Hendrikx

This should work:

这应该有效:

from tkinter import *

root = Tk(className = "button_click_label")
root.geometry("200x200")

message = StringVar()
message.set('hi')

l1 = Label(root, text="hi")
l1.pack()

def press():
    l1.config(text="hello")

b1 = Button(root, text = "clickhere", command = press).pack()

root.mainloop()