Python Tkinter CheckButton配置问题

时间:2014-10-17 14:50:14

标签: python user-interface tkinter

我正在尝试使用多个单选按钮和两个复选框创建一个简单的gui。我想根据所选的单选按钮更改一个复选框的状态。但是,当我尝试这样做时,我的代码在

行失败了
def OnParallelChanged(self):
...
...
    elif self.ParallelVariable.get() == 2:
        self.AdaptiveChkBox.config(state='normal')

错误:

Traceback (most recent call last):
File "~/Desktop/main.py", line 206, in <module>
  app = configuration(None)
File "~/Desktop/main.py", line 61, in __init__
  self.initialize()
File "~/Desktop/main.py", line 101, in initialize
  self.OnParallelChanged()
File "~/Desktop/main.py", line 157, in OnParallelChanged
  self.AdaptiveChkBox.config(state='normal')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1826, in __getattr__
  return getattr(self.tk, attr)
AttributeError: AdaptiveChkBox

如果我评论这一行,一切都好。我不明白这个问题。任何帮助表示赞赏。 这是完整的代码:

import Tkinter

class configuration(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.buildBtn = Tkinter.Button(self, text=u'Build!',
                                       command=self.OnBuildButtonClick)
        self.buildBtn.grid(column=1, row=5)

        self.runBtn = Tkinter.Button(self, text=u'Run!',
                                     command=self.OnRunButtonClick)
        self.runBtn.grid(column=2, row=5)

        self.parallelLabel = Tkinter.Label(self,
                                           text=u'Choose device:',
                                           anchor='w')
        self.parallelLabel.grid(column=0, row=0, columnspan=2, sticky='EW')

        self.solverLabel = Tkinter.Label(self,
                                         text=u'Choose Solver:',
                                         anchor="w")
        self.solverLabel.grid(column=2, row=0, columnspan=2, sticky='EW')

        self.parallelMods = [
            ("MPI", 1),
            ("OpenMp", 2),
            ("GPU", 3)
        ]

        self.ParallelVariable = Tkinter.IntVar()
        self.ParallelVariable.set(2)

        for text, mode in self.parallelMods:
            self.b = Tkinter.Radiobutton(self, text=text,
                                         variable=self.ParallelVariable,
                                         value=mode,
                                         command=self.OnParallelChanged)
            self.b.grid(column=0, row=mode, sticky='W')

        self.SolverVariable = Tkinter.IntVar()
        self.OnParallelChanged()

        self.LogVariable = Tkinter.IntVar()
        self.LogVariable.set(1)
        self.LogChkBox = Tkinter.Checkbutton(self,
                                             text='Log solvers',
                                             variable=self.LogVariable)
        self.LogChkBox.grid(column=4, row=1, sticky='W')

        self.AdaptiveVariable = Tkinter.IntVar()
        self.AdaptiveVariable.set(1)
        self.AdaptiveChkBox = Tkinter.Checkbutton(self,
                                                  text='Adaptive',
                                                  state='disabled',
                                                  variable=self.AdaptiveVariable)
        self.AdaptiveChkBox.grid(column=4, row=2, sticky='W')

        self.grid_columnconfigure(0, weight=1)
        self.resizable(True, False)
        self.update()
        self.geometry(self.geometry())
        self.bind("<Return>", self.OnReturnHit)

    def OnBuildButtonClick(self):
        self.buildBtn.config(state='disabled')
        self.runBtn.config(state='disabled')

    def OnRunButtonClick(self):
        self.buildBtn.config(state='disabled')
        self.runBtn.config(state='disabled')

    def OnReturnHit(self, event):
        self.OnRunButtonClick()

    def OnParallelChanged(self):
        if self.ParallelVariable.get() == 1:
            # self.AdaptiveChkBox.config(state="disabled")
            self.SolverVariable.set(2)
            self.solverMods = [
                ("AGMG", 1, 'disabled'),
                ("Trilinos", 2, 'normal'),
                ("Paralution", 3, 'disabled')
            ]
        elif self.ParallelVariable.get() == 2:
            self.AdaptiveChkBox.config(state='normal')
            self.SolverVariable.set(2)
            self.solverMods = [
                ("AGMG", 1, 'normal'),
                ("Trilinos", 2, 'normal'),
                ("Paralution", 3, 'disabled')
            ]
        elif self.ParallelVariable.get() == 3:
            # self.AdaptiveChkBox.config(state="disabled")
            self.SolverVariable.set(3)
            self.solverMods = [
                ("AGMG", 1, 'disabled'),
                ("Trilinos", 2, 'disabled'),
                ("Paralution", 3, 'normal')
            ]

        self.setSolverRadioButtons(solverMods=self.solverMods)

    def setSolverRadioButtons(self, solverMods):
        for text, mode, sState in solverMods:
            b = Tkinter.Radiobutton(self, text=text,
                                    variable=self.SolverVariable,
                                    value=mode,
                                    state=sState)
            b.grid(column=2, row=mode, sticky='W')

    def center(self):
        self.update_idletasks()
        width = self.winfo_width()
        frm_width = self.winfo_rootx() - self.winfo_x()
        win_width = width + 2 * frm_width
        height = self.winfo_height()
        title_bar_height = self.winfo_rooty() - self.winfo_y()
        win_height = height + title_bar_height + frm_width
        x = self.winfo_screenwidth() // 2 - win_width // 2
        y = self.winfo_screenheight() // 2 - win_height // 2
        self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
        if self.attributes('-alpha') == 0:
            self.attributes('-alpha', 1.0)
        self.deiconify()


if __name__ == "__main__":
    app = configuration(None)
    app.title('Configure GIA')
    app.center()
    app.mainloop()

1 个答案:

答案 0 :(得分:1)

您在self.OnParallelChanged()中呼叫initialize,但在之后只设置了几行