如何更改 Tkinter 按钮状态?
Tkinter 是一个用于创建 GUI 应用程序的Python包。 Python有很多 GUI 框架,但 Tkinter 是唯一内置到Python标准库中的框架。 Tkinter 有几个优点;它是跨平台的,因此相同的代码适用于 Windows、macOS 和 Linux。与其他框架相比,Tkinter 是轻量级的,使用起来相对轻松。
在本文中,我们将学习如何更改按钮的状态。
让我们逐步理解这一点:
第 1 步:首先,我们将导入Tkinter模块和一些我们需要的小部件。
Python3
# importing tkinter module
# along with some constants and Widgets
from tkinter import Tk
from tkinter.constants import DISABLED, NORMAL
from tkinter.ttk import Button, Label
Python3
# Creating App class which
# will contain our overall App
class App:
def __init__(self, master) -> None:
# Instantiating master i.e toplevel Widget
self.master = master
# Creating label
self.label = Label(self.master,
text = "Click Button2 to change Button1 State")
self.label.pack(pady = 10)
# Creating button1
# We will change the state of this Button
# it has a initial state of "NORMAL"
# i.e Button can be pressed
self.button1 = Button(self.master,
text = "Button1",
state = NORMAL)
self.button1.pack(pady = 20)
# Creating another button
# We will use this button to
# change the State of first button
self.button2 = Button(self.master,
text = "Button2",
command = self.changeState)
self.button2.pack(pady = 20)
Python3
# Helper function which will change the State of Button1
def changeState(self) -> None:
# Printing the State of
# the Button before ALTERING it
# This is optional
print(self.button1['state'])
# Checking if the STATE of the Button1
# If the STATE is NORMAL
if (self.button1['state'] == NORMAL):
# Change the state to DISABLED
self.button1['state'] = DISABLED
else:
# Otherwise, change the state to NORMAL
self.button1['state'] = NORMAL
Python3
if __name__ == "__main__":
# Instantiating top level
root = Tk()
# Setting the title of the window
root.title("Button State App")
# Setting the geometry i.e Dimensions
root.geometry("400x250")
# Calling our App
app = App(root)
# Mainloop which will cause
# this toplevel to run infinitely
root.mainloop()
Python3
# importing tkinter module
# along with some constants and Widgets
from tkinter import Tk
from tkinter.constants import DISABLED, NORMAL
from tkinter.ttk import Button, Label
# Creating App class
# which will contain our overall App
class App:
def __init__(self, master) -> None:
# Instantiating master
# i.e toplevel Widget
self.master = master
# Creating label
self.label = Label(self.master,
text="Click Button2 to change Button1 State")
self.label.pack(pady = 10)
# Creating button1
# We will change the state of this Button
# it has a initial state of
# "NORMAL" i.e Button can be pressed
self.button1 = Button(self.master,
text = "Button1",
state = NORMAL)
self.button1.pack(pady = 20)
# Creating another button
# We will use this button
# to change the State of first button
self.button2 = Button(self.master,
text = "Button2",
command = self.changeState)
self.button2.pack(pady = 20)
# Helper function which will
# change the State of Button1
def changeState(self) -> None:
# Printing the State of
# the Button before ALTERING it
# This is optional
print(self.button1['state'])
# Checking if the STATE of the Button1
# If the STATE is NORMAL
if (self.button1['state'] == NORMAL):
# Change the state to DISABLED
self.button1['state'] = DISABLED
else:
# Otherwise, change the state to NORMAL
self.button1['state'] = NORMAL
if __name__ == "__main__":
# Instantiating top level
root = Tk()
# Setting the title of the window
root.title("Button State App")
# Setting the geometry i.e Dimensions
root.geometry("400x250")
# Calling our App
app = App(root)
# Mainloop which will cause this toplevel
# to run infinitely
root.mainloop()
如果您使用的是 Python2,那么将tkinter更改为 Tkinter并且tkinter.ttk也将不起作用,因此从 Tkinter 本身导入小部件。
第 2 步:现在我们将创建一个包含所有按钮和标签的App 类。
蟒蛇3
# Creating App class which
# will contain our overall App
class App:
def __init__(self, master) -> None:
# Instantiating master i.e toplevel Widget
self.master = master
# Creating label
self.label = Label(self.master,
text = "Click Button2 to change Button1 State")
self.label.pack(pady = 10)
# Creating button1
# We will change the state of this Button
# it has a initial state of "NORMAL"
# i.e Button can be pressed
self.button1 = Button(self.master,
text = "Button1",
state = NORMAL)
self.button1.pack(pady = 20)
# Creating another button
# We will use this button to
# change the State of first button
self.button2 = Button(self.master,
text = "Button2",
command = self.changeState)
self.button2.pack(pady = 20)
第3步:正如你在上面的代码中看到的,我们有一个与Button2相连的函数,即changeState函数,接下来我们要实现这个函数。在这个函数中,我们将改变 Button1 的状态。
蟒蛇3
# Helper function which will change the State of Button1
def changeState(self) -> None:
# Printing the State of
# the Button before ALTERING it
# This is optional
print(self.button1['state'])
# Checking if the STATE of the Button1
# If the STATE is NORMAL
if (self.button1['state'] == NORMAL):
# Change the state to DISABLED
self.button1['state'] = DISABLED
else:
# Otherwise, change the state to NORMAL
self.button1['state'] = NORMAL
第 4 步:在这一步中,我们将创建将运行此应用程序的主函数。在 main函数中,我们将设置应用程序标题和几何图形并实例化我们的 App 类。
蟒蛇3
if __name__ == "__main__":
# Instantiating top level
root = Tk()
# Setting the title of the window
root.title("Button State App")
# Setting the geometry i.e Dimensions
root.geometry("400x250")
# Calling our App
app = App(root)
# Mainloop which will cause
# this toplevel to run infinitely
root.mainloop()
下面是完整的实现:
蟒蛇3
# importing tkinter module
# along with some constants and Widgets
from tkinter import Tk
from tkinter.constants import DISABLED, NORMAL
from tkinter.ttk import Button, Label
# Creating App class
# which will contain our overall App
class App:
def __init__(self, master) -> None:
# Instantiating master
# i.e toplevel Widget
self.master = master
# Creating label
self.label = Label(self.master,
text="Click Button2 to change Button1 State")
self.label.pack(pady = 10)
# Creating button1
# We will change the state of this Button
# it has a initial state of
# "NORMAL" i.e Button can be pressed
self.button1 = Button(self.master,
text = "Button1",
state = NORMAL)
self.button1.pack(pady = 20)
# Creating another button
# We will use this button
# to change the State of first button
self.button2 = Button(self.master,
text = "Button2",
command = self.changeState)
self.button2.pack(pady = 20)
# Helper function which will
# change the State of Button1
def changeState(self) -> None:
# Printing the State of
# the Button before ALTERING it
# This is optional
print(self.button1['state'])
# Checking if the STATE of the Button1
# If the STATE is NORMAL
if (self.button1['state'] == NORMAL):
# Change the state to DISABLED
self.button1['state'] = DISABLED
else:
# Otherwise, change the state to NORMAL
self.button1['state'] = NORMAL
if __name__ == "__main__":
# Instantiating top level
root = Tk()
# Setting the title of the window
root.title("Button State App")
# Setting the geometry i.e Dimensions
root.geometry("400x250")
# Calling our App
app = App(root)
# Mainloop which will cause this toplevel
# to run infinitely
root.mainloop()
输出: