wx 小部件不起作用(python) [英] wx widget won't work (python)

查看:20
本文介绍了wx 小部件不起作用(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've written a script that should ask for two values and then display them in another window. It looks like this:

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() 方法中创建的新窗口中的值,它总是进入异常.

When I run it, it never shows the values in the new window that is created in the read() method, it allways goes to exception.

该脚本深受此示例脚本的启发,效果很好:

The script is greatly inspired by this example script, that works just fine:

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()

有什么区别?为什么一个有效,另一个无效?

What is the difference? Why does one work and not the other?

推荐答案

目前你得到一个异常但你不知道是什么引起的,如果你想检查它,尝试改变 read 函数中的 except 子句到:

currently you're getting an exception but you don't know what caused it, if you want to check it, try changing the except clause in the read function to:

except Exception as e: 
    print e

在阅读中,你会得到:

'Example' object has no attribute 'tc1'

因为 Example 类没有 tc1 属性(它仅针对 InitUI 函数在本地定义).您必须更改它,使其成为对象属性更改:

because Example class does not have the tc1 property (it is definned locally for the InitUI function only). you have to change it so it is an object property change:

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)

这篇关于wx 小部件不起作用(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆