更改 Python 中 Tkinter 按钮的命令方法
按钮小部件的意义在于它用于处理事件以在应用程序中执行某些操作。为了处理此类事件,我们通常定义包含某些操作的方法。
假设我们希望在初始化按钮后更改事件方法。我们可以使用configure(options)方法配置按钮及其处理程序。所以,通过定义一个新方法和配置按钮,我们可以用相同的按钮触发一个新事件。
示例
#Import tkinter library
from tkinter import *
#Create an instance of tkinter frame
win = Tk()
#Set the geometry
win.geometry("750x250")
#Define a function to show the text label
def text_label():
Label(win, text= "Woohoo! An Event has occurred!", font= ('Helvetica 10 bold')).pack(pady=20)
#Configure the Button to trigger a new event
button.configure(command= close_win)
#Define a function to close the event
def close_win():
win.destroy()
#Create a Button widget
button= Button(win, text= "Click", font= ('Helvetica 10 bold'), command= text_label)
button.pack(side= TOP)
win.mainloop()输出
运行以上代码会显示一个包含一个按钮的窗口。

首次按下按钮后,它将显示一个文字标签。

现在第二次单击该按钮,它将终止 TCL 解释器。
广告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP