wx小部件无法正常工作(python)

时间:2014-07-03 07:21:19

标签: python wxpython

我编写了一个脚本,应该要求两个值,然后在另一个窗口中显示它们。它看起来像这样:

import wx

class Example(wx.Frame):

    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title,
            size=(300, 200))

        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):

        panel = wx.Panel(self)

        sizer = wx.GridBagSizer(4, 2)

        text1 = wx.StaticText(panel, label="Set COM-ports")
        sizer.Add(text1, pos=(0, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM,
            border=15)

        line = wx.StaticLine(panel)
        sizer.Add(line, pos=(1, 0), span=(1, 5),
            flag=wx.EXPAND|wx.BOTTOM, border=10)

        text2 = wx.StaticText(panel, label="Dispersion control port:")
        sizer.Add(text2, pos=(2, 0), flag=wx.ALIGN_RIGHT|wx.LEFT, border=10)

        tc1 = wx.TextCtrl(panel)
        sizer.Add(tc1, pos=(2, 1), flag=wx.LEFT, border=10)

        text3 = wx.StaticText(panel, label="GPS port:")
        sizer.Add(text3, pos=(3, 0),flag=wx.ALIGN_RIGHT|wx.LEFT, border=10)

        tc2 = wx.TextCtrl(panel)
        sizer.Add(tc2, pos=(3, 1), flag=wx.LEFT,border=10)

        button4 = wx.Button(panel, label="Start")
        sizer.Add(button4, pos=(4, 0), flag=wx.ALIGN_RIGHT)
        self.Bind(wx.EVT_BUTTON, self.read, button4)
        button4.SetDefault()

        button5 = wx.Button(panel, wx.ID_EXIT, label="Cancel")
        sizer.Add(button5, pos=(4, 1), flag=wx.ALIGN_LEFT|wx.LEFT, border=10)
        self.Bind(wx.EVT_BUTTON, self.OnQuitApp, id=wx.ID_EXIT)

        panel.SetSizer(sizer)

    def read(self, event):
        try:
            DispPort = int(float(self.tc1.GetValue()))
            GpsPort = int(float(self.tc2.GetValue()))
            wx.MessageDialog(self, "DispPort: %s\nGpsPort: %s"%(DispPort,GpsPort), "Number entered", wx.OK | wx.ICON_INFORMATION).ShowModal()
        except:
            wx.MessageDialog(self, "Enter a number", "Warning!", wx.OK | wx.ICON_WARNING).ShowModal()

    def OnQuitApp(self, event):

        self.Close()


if __name__ == '__main__':
    app = wx.App()
    Example(None, title="Start DisperseIt")
    app.MainLoop()

当我运行它时,它永远不会在read()方法中创建的新窗口中显示值,它总是会出现异常。

这个脚本受到这个示例脚本的启发,它运行得很好:

import wx

class Frame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id,
                          'Read Number',
                          size = (200,200),
                          style=wx.MINIMIZE_BOX | wx.RESIZE_BORDER
    | wx.SYSTEM_MENU | wx.CAPTION |  wx.CLOSE_BOX)
        self.initUI()

    def initUI(self):

        widgetPanel=wx.Panel(self, -1)

        Button = wx.Button(widgetPanel, -1, "Read", pos=(10,10), size=(30,30))

        self.Bind(wx.EVT_BUTTON, self.read, Button)
        Button.SetDefault()

        self.Text1 = wx.TextCtrl(widgetPanel, -1, "", pos=(10, 50),
                                size =(100,30), style=wx.TE_CENTER)

        self.Text1.SetFont(wx.Font(20, wx.DECORATIVE, wx.NORMAL, wx.NORMAL))

        self.Text2 = wx.TextCtrl(widgetPanel, -1, "", pos=(10, 90),
                                size =(100,30), style=wx.TE_CENTER)

        self.Text2.SetFont(wx.Font(20, wx.DECORATIVE, wx.NORMAL, wx.NORMAL))


    def read(self, event):
        try:
            var1 = int(float(self.Text1.GetValue()))
            var2 = int(float(self.Text2.GetValue()))
            wx.MessageDialog(self, "First Number: %s\nSecond number: %s\n"%(var1,var2), "Number entered", wx.OK | wx.ICON_INFORMATION).ShowModal()
        except:
            wx.MessageDialog(self, "Enter a number", "Warning!", wx.OK | wx.ICON_WARNING).ShowModal()


if __name__ == "__main__":
    app = wx.App(False)
    frame = Frame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

有什么区别?为什么一个工作而另一个工作?

1 个答案:

答案 0 :(得分:1)

目前你正在获得例外,但你不知道是什么导致了它,如果你想检查它,请尝试将read函数中的except子句更改为:

except Exception as e: 
    print e

在阅读结束时,您将获得:

'Example' object has no attribute 'tc1'

因为Example类没有tc1属性(它仅在本地为InitUI函数定义)。你必须改变它,因此它是一个对象属性改变:

tc1 = wx.TextCtrl(panel)
sizer.Add(tc1, pos=(2, 1), flag=wx.LEFT, border=10)
tc2 = wx.TextCtrl(panel)
sizer.Add(tc2, pos=(2, 1), flag=wx.LEFT, border=10)

为:

self.tc1 = wx.TextCtrl(panel)
sizer.Add(self.tc1, pos=(2, 1), flag=wx.LEFT, border=10)
self.tc2 = wx.TextCtrl(panel)
sizer.Add(self.tc2, pos=(2, 1), flag=wx.LEFT, border=10)