如何在按下按鈕時更改 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