【问题标题】:Change button color on click among similar multiple buttons在类似的多个按钮之间单击更改按钮颜色
【发布时间】:2018-03-01 13:24:20
【问题描述】:

让我详细说明这个过程。以下是我的应用的示例代码sn-p

import tkinter as tk
class ldo(Frame):
    def __init__(self, master = None):
        Frame.__init__(self,master)
        self.grid()
        self.appOutline()

    def appOutline(self):
        self.masterframe1 = Frame(self.master,width=300,height=300)
        self.masterframe1.grid(sticky=N,padx=20)

        self.masterframe2 = Frame(self.master,width=300,height=100)
        self.masterframe2.grid(sticky=S,padx=20)

        self.addtabbutton = Button(self.masterframe2,text="Add Step", width=10, command=self.popup2, bg="sienna1")
        self.addtabbutton.grid(row=0,column=0,sticky=EW)
        self.runallbutton = Button(self.masterframe2,text="Run All", width=10, command=self.underprocess, bg="sienna1",state=DISABLED)
        self.runallbutton.grid(row=0,column=1,sticky=EW)
        self.runselbutton = Button(self.masterframe2,text="Run Selected", width=10, command=self.popup2, bg="sienna1",state=DISABLED)
        self.runselbutton.grid(row=0,column=2,sticky=EW)

    def popup2(self):
        self.child = Toplevel()
        self.child.geometry("300x300")
        self.child.title("Secondary")

        self.label1 = Label(self.child,text="Your input")
        self.label1.pack()

        self.entryinp = StringVar()
        self.entry1 = Entry(self.child,textvariable=entryinp)
        self.entry1.pack()

        self.submit = Button(self.child,text="Submit",command=self.evaluation,bg="pink")
        self.submit.pack()

    def evaluation(self):
        self.inputValue = self.entry1.get()

        self.steps = Button(self.masterframe1,text=self.inputValue,command=self.colorchange,bg="light blue")
        self.steps.pack()

        self.runallbutton.config(state=NORMAL) 
        self.child.destroy()

    def colorchange(self):
        self.steps.config(bg="white")
        self.runselbutton.config(state=NORMAL)

    def underprocess(self):
        print("processing...")

if __name__ == "__main__":
    app = ldo()
    app.master.title("Primary")
    app.mainloop()

下面的屏幕截图是我的应用程序的输出。我的需要是我想更改我在主界面中单击的按钮的颜色,但是无论我单击什么按钮,它都会改变最后一个按钮的颜色。我尝试使用 lambda,但我怀疑我是否正确使用它。上面的 sn-p 正在工作,除了颜色变化部分。请指导我如何完成这项工作。

另外,我希望“运行选定”按钮打印(文本)我单击的那个特定按钮。

P.S.:我也签入了this link,但无法解决。

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    一种选择是使用lambdafunctools.partial 并将boton 的实例作为参数传递。您可以使用config 方法,而不是在实例化按钮时设置回调(引用尚不存在)。另一方面,您可以使用实例属性来存储可选按钮的引用(下面的代码中的self._step_buttons,一个列表)和另一个存储所选按钮的引用的属性(下面的代码中的self._step_selected) .

    代码可能如下所示:

    import tkinter as tk
    from functools import partial
    
    
    
    class Popup2(tk.Toplevel):
        def __init__(self, master=None):
            super().__init__(master)
            self.master = master
            self.geometry("300x300")
            self.title("Secondary")
    
            self.entry_input = tk.StringVar()
            tk.Label(self, text="Your input").pack()
            tk.Entry(self, textvariable=self.entry_input).pack()
            tk.Button(self, text="Submit", command=self.evaluation, bg="pink").pack()
    
        def evaluation(self):
            input_value = self.entry_input.get()
            self.master.add_step(input_value)
            self.destroy()
    
    
    class Ldo(tk.Frame):
        def __init__(self, master=None):
            super().__init__(master)
            self._step_buttons = []
            self._step_selected = None
            self.grid()
            self.app_outline()
    
    
        def app_outline(self):
            self.masterframe1 = tk.Frame(self.master, width=300, height=300)
            self.masterframe1.grid(sticky=tk.N, padx=20)
    
            self.masterframe2 = tk.Frame(self.master, width=300, height=100)
            self.masterframe2.grid(sticky=tk.S, padx=20)
    
            self.addtabbutton = tk.Button(self.masterframe2, text="Add Step", width=10,
                                          command=self.open_popup, bg="sienna1")
            self.addtabbutton.grid(row=0, column=0, sticky=tk.EW)
            self.runallbutton = tk.Button(self.masterframe2, text="Run All", width=10,
                                          command=self.process_all, bg="sienna1", state=tk.DISABLED)
            self.runallbutton.grid(row=0, column=1, sticky=tk.EW)
            self.runselbutton = tk.Button(self.masterframe2, text="Run Selected", width=10,
                                          command=self.process_selected, bg="sienna1", state=tk.DISABLED)
            self.runselbutton.grid(row=0, column=2, sticky=tk.EW)
    
        def open_popup(self):
            Popup2(master=self)
    
        def add_step(self, step):
            btn = tk.Button(self.masterframe1, text=step, bg="light blue")
            btn.configure(command=partial(self.select_button, btn))
            btn.pack()
            self._step_buttons.append(btn)
            self.runallbutton.config(state=tk.NORMAL)
    
        def select_button(self, button):
            if self._step_selected == button:
                self._step_selected.config(bg="light blue")
                self._step_selected = None
                self.runselbutton.config(state=tk.DISABLED)
    
            else:
               if self._step_selected:
                   self._step_selected.config(bg="light blue")
               button.config(bg="white")
               self._step_selected = button
               self.runselbutton.config(state=tk.NORMAL)
    
        def process_all(self):
            print("Processing all: ")
            for btn in self._step_buttons:
                print("  Processing {}...".format(btn["text"]))
    
        def process_selected(self):
            print("Processing selected: ")
            print("  Processing {}...".format(self._step_selected["text"]))
    
    
    if __name__ == "__main__":
        app = Ldo()
        app.master.title("Primary")
        app.mainloop()
    

    我修改了其他东西,例如,我为按钮提供了切换按钮行为。

    这是结果:

    【讨论】:

    • 这太绝对了..我从来没有尝试过部分函数..你有没有想过以任何其他方式实现它..另外,你能告诉我你是怎么想到这个的过程..这对我想学习的帮助更大
    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    相关资源
    最近更新 更多