0

以下のコードは、25行目で定義されていないエラー名「parent」と、13行目で「class 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()
4

1 に答える 1

1

概要として、私が行った変更は次のとおりです。

  1. __init__メソッドの下のコードをシフトする
  2. 引数としてバルーンを削除する__init__
  3. self.buttonBoxself.menuBar、およびの定義をそれぞれ、、およびself.mainPartの定義の後に移動します。buttonBoxmenuBarmainPart
  4. クラスのインスタンスを作成する呼び出しを追加しますmainWindow

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.
于 2013-10-24T14:27:17.277 に答える