wxPython 进行GUI 设计解决PyNoAppError: The wx.App object must be created first!

安装wxPython

下载网址:https://sourceforge.net/projects/wxpython/files/wxPython,选择和自己电脑操作系统以及python版本对应的安装即可

例子:

# -*- coding: utf-8 -*-
import wx 
class Application(wx.Frame): 
    def __init__(self, parent): 
        wx.Frame.__init__(self, parent, -1, 'My GUI', size=(400, 300)) 
        panel = wx.Panel(self) 
        sizer = wx.BoxSizer(wx.VERTICAL) 
        panel.SetSizer(sizer) 
        txt = wx.StaticText(panel, -1, 'Hello World!') 
        sizer.Add(txt, 0, wx.TOP|wx.LEFT, 20) 
        self.Centre() 
        self.Show(True)
        
app = wx.App(0) 
Application(None) 
app.MainLoop()
运行第一次没毛病,

wxPython 进行GUI 设计解决PyNoAppError: The wx.App object must be created first!_第1张图片

运行第二次出错,PyNoAppError: The wx.App object must be created first!


查资料解决:

这段代码只能在CMD窗口输入python test.py运行(当然还有其它方法),因为IDLE是用Tkinker开发的,它会和wxWidget的mainloop()冲突。

或者你拷贝到wxPython自带的pyCrust中也行,因为它也是用wxWidget开发的,不会有以上的冲突。


wxPython 进行GUI 设计解决PyNoAppError: The wx.App object must be created first!_第2张图片









你可能感兴趣的:(wxPython 进行GUI 设计解决PyNoAppError: The wx.App object must be created first!)