如何在按下按钮时更改 Tkinter 标签文本?
大多数情况下,Tkinter Label 小部件用于在应用程序中显示文本或图像。我们可以使用 **config(**options))** 方法配置标签小部件,如其文本属性、颜色、背景或前景颜色。
如果需要动态修改或更改标签小部件,可以使用按钮和函数来更改标签小部件的文本。
示例
# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Define a function update the label text
def on_click():
label["text"] = "Python"
b["state"] = "disabled"
# Create a label widget
label = Label(win, text="Click the Button to update this Text",
font=('Calibri 15 bold'))
label.pack(pady=20)
# Create a button to update the label widget
b = Button(win, text="Update Label", command=on_click)
b.pack(pady=20)
win.mainloop()输出
运行上述代码后,窗口中将显示标签文本和按钮。

单击按钮后,它将仅更新标签文本。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP