在类似的多个按钮

时间:2018-02-28 14:48:55

标签: python python-3.x tkinter

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

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,但我怀疑我是否正确使用它。除了换色部分之外,上面的代码片段正在工作。请指导我如何完成这项工作。

另外,我想要“运行已选择”按钮来打印(文本)我点击的特定按钮。

P.S。:我已经检查了this link但是还没有解决。

Output

1 个答案:

答案 0 :(得分: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()

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

结果如下:

enter image description here