循环浏览 tkinter 中的按钮
Looping through buttons in tkinter
我编写了以下代码来创建 4 个按钮,单击一个按钮后,该按钮的背景颜色发生变化,当我单击另一个按钮时,该按钮的颜色现在发生变化,而旧按钮的颜色也发生变化一个正在改回默认颜色。它工作得很好,但编码效率不高。我在想一个循环,它通过我所有的按钮来设置所有不活动的按钮为默认颜色。我怎样才能做到这一点?
from tkinter import *
def update_Button(number):
if number == 1:
Button_1["background"] = Button_1["activebackground"] = "lightblue"
Button_2.configure(background="SystemButtonFace")
Button_3.configure(background="SystemButtonFace")
Button_4.configure(background="SystemButtonFace")
elif number == 2:
Button_2["background"] = Button_2["activebackground"] = "lightblue"
Button_1.configure(background="SystemButtonFace")
Button_3.configure(background="SystemButtonFace")
elif number == 3:
Button_3["background"] = Button_3["activebackground"] = "lightblue"
Button_2.configure(background="SystemButtonFace")
Button_1.configure(background="SystemButtonFace")
Button_4.configure(background="SystemButtonFace")
elif number == 4:
Button_4["background"] = Button_4["activebackground"] = "lightblue"
Button_2.configure(background="SystemButtonFace")
Button_3.configure(background="SystemButtonFace")
Button_1.configure(background="SystemButtonFace")
pass
root = Tk()
Button_font = ("Calibri", 20, "bold")
Button_Size = [70, 70] # width, height
pady = 5
padx = 5
Button_1 = Button(root, text="1", font=Button_font, command=lambda: update_Button(1))
Button_1.grid(sticky="wens", pady=pady, padx=padx)
Button_2 = Button(root, text="2", font=Button_font, command=lambda: update_Button(2))
Button_2.grid(sticky="wens", pady=pady, padx=padx)
Button_3 = Button(root, text="3", font=Button_font, command=lambda: update_Button(3))
Button_3.grid(sticky="wens", pady=pady, padx=padx)
Button_4 = Button(root, text="4", font=Button_font, command=lambda: update_Button(4))
Button_4.grid(sticky="wens", pady=pady, padx=padx)
root.mainloop()
就像@Mitiku 建议的那样,使用列表的解决方案是:
def update_Button(number):
number = number-1
buttons = [Button_1, Button_2, Button_3, Button_4]
buttons[number]["background"] = buttons[number]["activebackground"] = "lightblue"
for button in buttons:
if button == buttons[number]:
pass
else:
button.configure(background="SystemButtonFace")
pass
你可以使用列表。
def update_Button(number):
buttons = [Button_1,Button_2,Button_3,Button_4]
for button in buttons:
button.configure(background="SystemButtonFace")
buttons[number-1]["activebackground"] = "lightblue"
下面实现你所需要的:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.number = 4 #change me to get more buttons
self.buttons = []
for i in range(self.number):
self.buttons.append(Button(self.root, text="Change!", bg="white", command=lambda c=i: self.command(c)))
self.buttons[i].pack()
def command(self, var):
for i in range(self.number):
self.buttons[i].configure({"bg": "white", "activebackground": "white"})
self.buttons[var].configure({"bg": "lightblue", "activebackground": "lightblue"})
root = Tk()
App(root)
root.mainloop()
我们在这里使用的主要有趣机制是 lambda
。
我们声明了command=lambda c=i: self.command(c)
,让我们在声明时用i
的值调用command
回调。这意味着当我们调用命令时,我们传递 Button
小部件在 list
.
中的位置的整数值
附带说明一下,使用 Radiobutton
s 可以更轻松地完成此操作。见下文:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.v = IntVar()
self.number = 4 #change me to get more buttons
self.buttons = []
for i in range(self.number):
self.buttons.append(Radiobutton(self.root, text="Change!", bg="white", activebackground="lightblue", selectcolor="lightblue", variable=self.v, indicatoron=0, value=i))
self.buttons[i].pack()
root = Tk()
App(root)
root.mainloop()
我编写了以下代码来创建 4 个按钮,单击一个按钮后,该按钮的背景颜色发生变化,当我单击另一个按钮时,该按钮的颜色现在发生变化,而旧按钮的颜色也发生变化一个正在改回默认颜色。它工作得很好,但编码效率不高。我在想一个循环,它通过我所有的按钮来设置所有不活动的按钮为默认颜色。我怎样才能做到这一点?
from tkinter import *
def update_Button(number):
if number == 1:
Button_1["background"] = Button_1["activebackground"] = "lightblue"
Button_2.configure(background="SystemButtonFace")
Button_3.configure(background="SystemButtonFace")
Button_4.configure(background="SystemButtonFace")
elif number == 2:
Button_2["background"] = Button_2["activebackground"] = "lightblue"
Button_1.configure(background="SystemButtonFace")
Button_3.configure(background="SystemButtonFace")
elif number == 3:
Button_3["background"] = Button_3["activebackground"] = "lightblue"
Button_2.configure(background="SystemButtonFace")
Button_1.configure(background="SystemButtonFace")
Button_4.configure(background="SystemButtonFace")
elif number == 4:
Button_4["background"] = Button_4["activebackground"] = "lightblue"
Button_2.configure(background="SystemButtonFace")
Button_3.configure(background="SystemButtonFace")
Button_1.configure(background="SystemButtonFace")
pass
root = Tk()
Button_font = ("Calibri", 20, "bold")
Button_Size = [70, 70] # width, height
pady = 5
padx = 5
Button_1 = Button(root, text="1", font=Button_font, command=lambda: update_Button(1))
Button_1.grid(sticky="wens", pady=pady, padx=padx)
Button_2 = Button(root, text="2", font=Button_font, command=lambda: update_Button(2))
Button_2.grid(sticky="wens", pady=pady, padx=padx)
Button_3 = Button(root, text="3", font=Button_font, command=lambda: update_Button(3))
Button_3.grid(sticky="wens", pady=pady, padx=padx)
Button_4 = Button(root, text="4", font=Button_font, command=lambda: update_Button(4))
Button_4.grid(sticky="wens", pady=pady, padx=padx)
root.mainloop()
就像@Mitiku 建议的那样,使用列表的解决方案是:
def update_Button(number):
number = number-1
buttons = [Button_1, Button_2, Button_3, Button_4]
buttons[number]["background"] = buttons[number]["activebackground"] = "lightblue"
for button in buttons:
if button == buttons[number]:
pass
else:
button.configure(background="SystemButtonFace")
pass
你可以使用列表。
def update_Button(number):
buttons = [Button_1,Button_2,Button_3,Button_4]
for button in buttons:
button.configure(background="SystemButtonFace")
buttons[number-1]["activebackground"] = "lightblue"
下面实现你所需要的:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.number = 4 #change me to get more buttons
self.buttons = []
for i in range(self.number):
self.buttons.append(Button(self.root, text="Change!", bg="white", command=lambda c=i: self.command(c)))
self.buttons[i].pack()
def command(self, var):
for i in range(self.number):
self.buttons[i].configure({"bg": "white", "activebackground": "white"})
self.buttons[var].configure({"bg": "lightblue", "activebackground": "lightblue"})
root = Tk()
App(root)
root.mainloop()
我们在这里使用的主要有趣机制是 lambda
。
我们声明了command=lambda c=i: self.command(c)
,让我们在声明时用i
的值调用command
回调。这意味着当我们调用命令时,我们传递 Button
小部件在 list
.
附带说明一下,使用 Radiobutton
s 可以更轻松地完成此操作。见下文:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.v = IntVar()
self.number = 4 #change me to get more buttons
self.buttons = []
for i in range(self.number):
self.buttons.append(Radiobutton(self.root, text="Change!", bg="white", activebackground="lightblue", selectcolor="lightblue", variable=self.v, indicatoron=0, value=i))
self.buttons[i].pack()
root = Tk()
App(root)
root.mainloop()