Python Tkinter GUI“父”未在类声明行上定义错误和错误,不确定它的含义

时间:2013-10-24 13:21:51

标签: python user-interface tkinter

下面的代码给出了错误名称" parent"未在第25行中定义,并且没有特定定义的错误,第13行中的"类mainWindow"。我不完全确定这些是什么意思,我不熟悉python并制作GUI。我查看了GUI示例,无法找到我做错的事情。希望有人可以帮我调试一下。 :)

import sys, Tkinter
sys.modules['tkinter'] = Tkinter
import Pmw

class Print:

    def __init__(self, text):
        self.text = text

    def __call__(self):
        print self.text

class mainWindow:

    def __init__(self,parent,balloon):
        self.balloon = Pmw.Balloon(parent)
        self.parent = parent
        self.menuBar = menuBar
        self.mainPart = mainPart
        self.buttonBox = buttonBox

    def Quit():
        root.destroy()

    menuBar = Pmw.MenuBar(parent,hull_relief = 'raised',hull_borderwidth = 1,balloon = self.balloon)
    menuBar.pack(fill = 'x')

    menuBar.addmenu('Run Control','Calibration,Download Configuration,Number of Triggers,Data Output File,Upload Configuration,Start DAQ,Quit')
    menuBar.addcascademenu('Run Control','Calibration','View and/or change the calibration',traverseSpec = 'z',tearoff = 1)
    menuBar.addmenuitem('Calibration','command','Display the DAC calibration',command = Print('display the DAC calibration'),label = 'Display DAC Calibration')

    menuBar.addmenuitem('Calibration','command','Display the calibration mask',command = Print('display the calibration mask'),label = 'Display Calibration Mask')
    menuBar.addmenuitem('Calibration','command','Change the DAC calibration',command = Print('change the DAC calibration'),label = 'Change DAC Calibration')
    menuBar.addmenuitem('Calibration','command','Change the calibration mask',command = Print('change the calibration mask'),label = 'Change Calibration Mask')

    menuBar.addmenuitem('Run Control','command','Download a configuration',command = Print('download configuration'),label = 'Download Configuration')

    menuBar.addmenuitem('Run Control','command','Set the number of triggers',command = Print('set number of triggers'),label = 'Number of Triggers')
    menuBar.addmenuitem('Run Control','command','Change the file where the data will be sent to',command = Print('set data output file'),label = 'Data Output File')
    menuBar.addmenuitem('Run Control','command','Upload a configuration',command = Print('upload a configuration'),label = 'Upload Configuration')

    menuBar.addmenuitem('Run Control','command','Start the data aquisition',command = Print('start data aquisition'),label = 'Start DAQ')
    menuBar.addmenuitem('Run Control','separator')
    menuBar.addmenuitem('Run Control','command','Close the GUI',command = Quit,label = 'Quit')

    mainPart = Tkinter.Label(parent,text = 'GUI',background = 'white',foreground = 'white',padx = 100,pady = 100)
    mainPart.pack(fill = 'both', expand = 1)

    buttonBox = Pmw.ButtonBox(parent)
    buttonBox.pack(fill = 'x')
    buttonBox.add('Start\nRoot', command = Print('start root'))

if __name__ == '__main__':
    root = Tkinter.Tk()
    Pmw.initialise(root)
    root.title('pCT GUI')
    root.mainloop()

1 个答案:

答案 0 :(得分:1)

概述,我所做的更改是:

  1. __init__方法
  2. 下移动代码
  3. __init__
  4. 删除气球作为参数
  5. 分别在self.buttonBoxself.menuBarself.mainPart的定义之后移动buttonBoxmenuBarmainPart的定义。< / LI>
  6. 添加电话以创建班级mainWindow的实例。
  7. 正如tobias在评论中提到的那样,您需要缩略从def Quit():buttonBox.add的行。

    接下来,您需要添加一行来创建类的实例,方法是将以下行添加到主区域

    if __name__ == '__main__':
        root = Tkinter.Tk()
        parent = Pmw.initialise(root)
        root.title('pCT GUI')
        derp = mainWindow(root) # <--- create your class
        root.mainloop()
    

    现在您需要对类定义进行一些更改。

    首先,您不需要将气球作为__init__方法的参数,因为您已将__init__方法中的第一行初始化为self.balloon = Pmw.Balloon(parent)并且未引用论证,而不是传递的论据。

    其次,您需要为类

    的变量移动一些声明
    def __init__(self,parent):
        self.balloon = Pmw.Balloon(parent)        
        self.parent = parent
    
    
        def Quit():
            root.destroy()
    
        menuBar = Pmw.MenuBar(parent,hull_relief = 'raised',hull_borderwidth = 1,balloon = self.balloon)
        menuBar.pack(fill = 'x')
    
        menuBar.addmenu('Run Control','Calibration,Download Configuration,Number of Triggers,Data Output File,Upload Configuration,Start DAQ,Quit')
    
        # ...
        # ......
        # All the calls to menuBar in the lines above stay the same
    
        self.menuBar = menuBar   # This needs to be called AFTER you declare the variable
                                 # and make your changes to it.
    
        mainPart = Tkinter.Label(parent,text = 'GUI',background = 'white',foreground = 'white',padx = 100,pady = 100)
        mainPart.pack(fill = 'both', expand = 1)
    
        self.mainPart = mainPart  # This needs to be called AFTER you declare the variable
                                  # and make your changes to it.
    
        buttonBox = Pmw.ButtonBox(parent)
        buttonBox.pack(fill = 'x')
        buttonBox.add('Start\nRoot', command = Print('start root'))
    
        self.buttonBox = buttonBox   # This needs to be called AFTER you declare the variable
                                     # and make your changes to it.