循环通过tkinter中的按钮

时间:2017-09-28 10:22:07

标签: python loops tkinter python-3.6

我编写了以下代码来创建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

2 个答案:

答案 0 :(得分:1)

以下内容可满足您的需求:

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),让我们在声明时使用值command调用i回调。这意味着当我们调用命令时,我们会通过Buttonlist小部件位置的整数值。

另一方面,使用Radiobutton可以更轻松地完成此操作。见下文:

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()

答案 1 :(得分:0)

你可以使用列表。

    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"