Python Tkinter GUI “parent“ not defined error 和类声明行错误

在编写 Python Tkinter GUI 程序时,遇到如下错误:
在这里插入图片描述

  • “parent” not defined 在第 25 行
  • 第 13 行 “class mainWindow” 发生错误,但未给出具体原因

检查代码后发现,这些错误可能是由于以下原因引起的:

  • 类成员变量和方法未正确定义
  • 缺少必要的参数
  • 变量和方法声明的顺序不正确

2、解决方案

为了解决这些错误,可以采取以下步骤:

1.在类成员变量和方法的声明前使用 self 关键字。
2.确保所有类成员变量和方法都正确定义,并且没有语法错误。
3.确保在类方法中使用了正确的参数。
4.检查变量和方法声明的顺序,确保它们是正确的。

修改后的代码如下:

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):
        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')
    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()
    parent = Pmw.initialise(root)
    root.title('pCT GUI')
    mainWindow(parent)  # Create an instance of the mainWindow class
    root.mainloop()

现在,程序应该可以正常运行,并且不会出现 “parent” not defined 和 “class mainWindow” 类声明行错误。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值