0

我遇到了将 Button 对象放入列表的问题。
我不确定我想要实现的目标是否真的可以实现,但就是这样..

工作样本:

from tkinter import *

class Main(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.pack(fill = 'both', expand = True)

        # General purpose variables     ----------
        self.isButtonSelected = False

        self.isButton_A_Selected = False    # controll boolean
        self.isButton_B_Selected = False
        self.isButton_C_Selected = False
        self.isButton_D_Selected = False

        # Layers                        ----------
        self.background = Frame(self)

        # Buttons                       ----------
        self.button_A = Button(self.background, text = "A", bg = 'white', command = self.selectButton_A)
        self.button_B = Button(self.background, text = "B", bg = 'white', command = self.selectButton_B)
        self.button_C = Button(self.background, text = "C", bg = 'white', command = self.selectButton_C)
        self.button_D = Button(self.background, text = "D", bg = 'white', command = self.selectButton_D)

        # Packs                         ----------
        #layers
        self.background.pack(fill = 'both', expand = True)
        #buttons
        btnPackPrefix = {'side' : 'left', 'fill' : 'both', 'expand' : 'True'}
        self.button_A.pack(btnPackPrefix)
        self.button_B.pack(btnPackPrefix)
        self.button_C.pack(btnPackPrefix)
        self.button_D.pack(btnPackPrefix)

    def selectButton_A(self):
        '''The idea here is similar to radiobuttons.
           Click to select. Click same to deselect.
           Click other to deselect old and select new.
           Only one button can be selected at a time.
               -repeated for all similar methods.'''

        if self.isButtonSelected == False:          # click-select
            self.button_A.config(bg = 'green')
            self.isButtonSelected = True
            self.isButton_A_Selected = True
        else:                                       
            if self.isButton_A_Selected:                # click-deselect
                self.resetButtons()
                self.isButtonSelected = False
            else:                                       # deselect other and select this
                self.resetButtons()
                self.button_A.config(bg = 'green')
                self.isButtonSelected = True
                self.isButton_A_Selected = True

    def selectButton_B(self):
        if self.isButtonSelected == False:
            self.button_B.config(bg = 'green')
            self.isButtonSelected = True
            self.isButton_B_Selected = True
        else:
            if self.isButton_B_Selected:
                self.resetButtons()
                self.isButtonSelected = False
            else:
                self.resetButtons()
                self.button_B.config(bg = 'green')
                self.isButtonSelected = True
                self.isButton_B_Selected = True

    def selectButton_C(self):
        if self.isButtonSelected == False:
            self.button_C.config(bg = 'green')
            self.isButtonSelected = True
            self.isButton_C_Selected = True
        else:
            if self.isButton_C_Selected:
                self.resetButtons()
                self.isButtonSelected = False
            else:
                self.resetButtons()
                self.button_C.config(bg = 'green')
                self.isButtonSelected = True
                self.isButton_C_Selected = True

    def selectButton_D(self):
        if self.isButtonSelected == False:
            self.button_D.config(bg = 'green')
            self.isButtonSelected = True
            self.isButton_D_Selected = True
        else:
            if self.isButton_D_Selected:
                self.resetButtons()
                self.isButtonSelected = False
            else:
                self.resetButtons()
                self.button_D.config(bg = 'green')
                self.isButtonSelected = True
                self.isButton_D_Selected = True

    def resetButtons(self):
        '''this pretty much resets all buttons and controll booleans
           to their default state'''
        #A
        self.button_A.config(bg = 'white')  # paint button white
        self.isButton_A_Selected = False    # controll boolean reset
        #B
        self.button_B.config(bg = 'white')
        self.isButton_B_Selected = False
        #C
        self.button_C.config(bg = 'white')
        self.isButton_C_Selected = False
        #D
        self.button_D.config(bg = 'white')
        self.isButton_D_Selected = False


def run_Application():
    app = Main()
    app.master.geometry('200x50')
    app.mainloop()

run_Application()

正如您所注意到的,我的代码有很多复制粘贴的片段,每个片段的细节都发生了变化。
所以我需要以某种方式缩短它。我需要 1 个函数来控制所有这些动作
,因为它们是相似的。

我想出了这个。

不工作样本:

from tkinter import *

class Main(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.pack(fill = 'both', expand = True)

        # General purpose variables     ----------
        self.isButtonSelected = False

        self.isButton_A_Selected = False    # controll boolean
        self.isButton_B_Selected = False
        self.isButton_C_Selected = False
        self.isButton_D_Selected = False

        # Layers                        ----------
        self.background = Frame(self)

        # Buttons                       ----------
        self.button_A = Button(self.background, text = "A", bg = 'white', command = self.selectButton(0)) # apparently this doesn't work...
        self.button_B = Button(self.background, text = "B", bg = 'white', command = self.selectButton(1))
        self.button_C = Button(self.background, text = "C", bg = 'white', command = self.selectButton(2))
        self.button_D = Button(self.background, text = "D", bg = 'white', command = self.selectButton(3))

        # Packs                         ----------
        #layers
        self.background.pack(fill = 'both', expand = True)
        #buttons
        btnPackPrefix = {'side' : 'left', 'fill' : 'both', 'expand' : 'True'}
        self.button_A.pack(btnPackPrefix)
        self.button_B.pack(btnPackPrefix)
        self.button_C.pack(btnPackPrefix)
        self.button_D.pack(btnPackPrefix)

    def selectButton(self, i = None):
        '''The idea here is similar to radiobuttons.
           Click to select. Click same to deselect.
           Click other to deselect old and select new.
           Only one button can be selected at a time.
               -do that only on objects
                 at [i]. 'i' is given from the button command'''

        global buttons, btn_bools

        buttons = [self.button_A, self.button_B,
                   self.button_C, self.button_D]    # list buttons.  # Raises error. It seems that Button objects cannot be listed

        btn_bools = [self.isButton_A_Selected, self.isButton_B_Selected,
                     self.isButton_C_Selected, self.isButton_D_Selected]    # list contoll booleans

        if self.isButtonSelected == False:          # click-select
            self.buttons[i].config(bg = 'green')
            self.isButtonSelected = True
            self.btn_bools[i] = True
        else:                                       
            if self.isButton_A_Selected:                # click-deselect
                self.resetButtons()
                self.isButtonSelected = False
            else:                                       # deselect other and select this
                self.resetButtons()
                self.buttons[i].config(bg = 'green')
                self.isButtonSelected = True
                self.btn_bools[i] = True

    def resetButtons(self):
        '''iterate through all buttons/controll booleans
           and reset to their default state'''

        for b in range(len(buttons)):
            buttons[b].config(bg = 'white')

        for bb in range(len(btn_bools)):
            btn_bools[bb] = False



def run_Application():
    app = Main()
    app.master.geometry('200x50')
    app.mainloop()

run_Application()

我尝试使用包含 Int 属性的“简单”类测试类似的方法,并且
效果很好!

从错误报告中,我知道列出按钮不是一个好主意,如果不是不可能的话。

我确实在 Google 上搜索了很多示例或类似问题,但没有
得到任何好的结果。

我还搜索了 Python 中的指针实现并
感到困惑。我试图查看是否可以保存指向
按钮的变量,但我认为结果是相同的。列表将其对象读取为按钮并
引发错误。

所以,我的问题是:
1. 这能以某种方式完成吗?(大大缩短了第一个代码示例。)
2. 关于列出一般的按钮或小部件:如果可以,怎么做?如果不是,为什么?
3. 在第二个代码示例中,我需要我的按钮命令包含
我的函数所需的“i”参数。这个怎么打?似乎只添加 (i) 是行不通的。

提前致谢!

4

1 回答 1

1

看这行代码:

self.button_A = Button(..., command = self.selectButton(0)) 

怎么了?做什么self.selectButton(0)?您正在调用函数self.selectButton(0),然后使用该函数的结果作为command属性的值。这几乎肯定不是你想要的。

你需要做的事情是这样的:

self.button_A = Button(..., command = lambda: self.selectButton(0))

这会将调用推迟到self.selectButton实际单击按钮之前。现在这将允许创建按钮,如果您愿意,可以将它们分配到列表中:

self.buttons = [self.button_A, self.button_B, self.button_C, self.button_D]
于 2013-07-11T15:44:52.967 回答