📜  如何更改 Tkinter 标签文本?

📅  最后修改于: 2022-05-13 01:54:22.600000             🧑  作者: Mango

如何更改 Tkinter 标签文本?

先决条件: tkinter 简介

Tkinter是Python的标准 GUI(图形用户界面)包。它提供了一种创建 GUI 应用程序的快速简便的方法。

要创建 tkinter 应用程序:

  • 导入模块——tkinter
  • 创建主窗口(容器)
  • 将任意数量的小部件添加到主窗口。
  • 在小部件上应用事件触发器。

小部件是任何 GUI 应用程序的控制工具。这里widget的主要作用是提供很好的各种控件。一些小部件是按钮、标签、文本框等等。

它的小部件之一是label ,它负责实现文本和图像的显示框部分。单击此处了解有关 Tkinter 标签小部件的更多信息。

现在,让我们看看如何更改标签的文本:

方法一:使用 标签.config()方法。

此方法用于执行覆盖标签小部件。

例子:

Python3
# importing everything from tkinter
from tkinter import *
 
# creating the tkinter window
Main_window = Tk()
 
# variable
my_text = "GeeksforGeeks updated !!!"
 
# function define for
# updating the my_label
# widget content
def counter():
   
    # use global variable
    global my_text
     
    # configure
    my_label.config(text = my_text)
 
# create a button widget and attached  
# with counter function  
my_button = Button(Main_window,
                   text = "Please update",
                   command = counter)
 
# create a Label widget
my_label = Label(Main_window,
                 text = "geeksforgeeks")
 
# place the widgets
# in the gui window
my_label.pack()
my_button.pack()
 
# Start the GUI
Main_window.mainloop()


Python3
# importing everything from tkinter
from tkinter import *
 
# create gui window
Main_window = Tk()
 
# set the configuration
# of the window
Main_window.geometry("220x100")
 
# define a function
# for setting the new text
def java():
    my_string_var.set("You must go with Java")
 
# define a function
# for setting the new text
def python():
    my_string_var.set("You must go with Python")
 
 
 
# create a Button widget and attached  
# with java function  
btn_1 = Button(Main_window,
               text = "I love Android",
               command = java)
 
# create a Button widget and attached  
# with python function
btn_2 = Button(Main_window,
               text = "I love Machine Learning",
               command = python)
 
# create a StringVar class
my_string_var = StringVar()
 
# set the text
my_string_var.set("What should I learn")
 
# create a label widget
my_label = Label(Main_window,
                 textvariable = my_string_var)
 
 
# place widgets into
# the gui window
btn_1.pack()
btn_2.pack()
my_label.pack()
 
# Start the GUI 
Main_window.mainloop()



输出:

方法 2:使用StringVar()类。

该类用于设置值并根据要求进行更改。

Python3

# importing everything from tkinter
from tkinter import *
 
# create gui window
Main_window = Tk()
 
# set the configuration
# of the window
Main_window.geometry("220x100")
 
# define a function
# for setting the new text
def java():
    my_string_var.set("You must go with Java")
 
# define a function
# for setting the new text
def python():
    my_string_var.set("You must go with Python")
 
 
 
# create a Button widget and attached  
# with java function  
btn_1 = Button(Main_window,
               text = "I love Android",
               command = java)
 
# create a Button widget and attached  
# with python function
btn_2 = Button(Main_window,
               text = "I love Machine Learning",
               command = python)
 
# create a StringVar class
my_string_var = StringVar()
 
# set the text
my_string_var.set("What should I learn")
 
# create a label widget
my_label = Label(Main_window,
                 textvariable = my_string_var)
 
 
# place widgets into
# the gui window
btn_1.pack()
btn_2.pack()
my_label.pack()
 
# Start the GUI 
Main_window.mainloop()