0

我正在尝试在 Windows 中为我的 Python 程序创建一个 GUI,我有点困惑 atm ..

我已经使用wxFormBuilder创建了 GUI ,它看起来像这样:

# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc

###########################################################################
## Class MainFrame
###########################################################################

class MainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Editor_SPED_LP", pos = wx.DefaultPosition, size = wx.Size( 320,255 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.Size( 320,255 ), wx.Size( 320,255 ) )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_textCtrl2 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_textCtrl2, 0, wx.ALL|wx.EXPAND, 5 )

        self.m_btn_abrirArq = wx.Button( self, wx.ID_ANY, u"Abrir arquivo...", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_abrirArq, 0, wx.ALL, 5 )

        self.m_btn_editarTxt = wx.Button( self, wx.ID_ANY, u"Editar .txt", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_editarTxt, 0, wx.ALL, 5 )

        self.m_gauge1 = wx.Gauge( self, wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL )
        self.m_gauge1.SetValue( 0 ) 
        bSizer1.Add( self.m_gauge1, 0, wx.ALL, 5 )

        self.m_btn_ajuda = wx.Button( self, wx.ID_ANY, u"Ajuda", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_ajuda, 0, wx.ALL, 5 )

        self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText2.Wrap( -1 )
        bSizer1.Add( self.m_staticText2, 0, wx.ALL, 5 )

        self.m_btn_sair = wx.Button( self, wx.ID_ANY, u"Sair", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_sair, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_btn_abrirArq.Bind( wx.EVT_BUTTON, self.abrirArquivo )
        self.m_btn_editarTxt.Bind( wx.EVT_BUTTON, self.editarTxt )
        self.m_btn_ajuda.Bind( wx.EVT_BUTTON, self.janelaAjuda )
        self.m_btn_sair.Bind( wx.EVT_BUTTON, self.sair )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def abrirArquivo( self, event ):
        event.Skip()

    def editarTxt( self, event ):
        event.Skip()

    def janelaAjuda( self, event ):
        event.Skip()

    def sair( self, event ):
        event.Skip()

当我从终端运行它时,它没有显示任何错误...

我应该如何在我的应用程序中使用它?

我在这里找到了这个教程,但我无法理解它..

我还尝试在脚本末尾添加该代码:

 if __name__ == "__main__":
        app = wx.App(False)
        frame = MainFrame()
        frame.Show()
        app.MainLoop()

并得到这个错误:

File "gui.py", line 80, in <module>
    frame = MainFrame(seld,parent)
NameError: name 'self' is not defined

所以..看起来gui代码很好,问题是我不知道如何初始化它并添加一些功能..

有人可以帮我吗?

4

3 回答 3

4

您遇到的问题是“init”函数/方法。它接受创建 MainFrame 对象时必须指定的两个参数(self 和 parent)。

def __init__( self, parent ):
     ......

self是自动指定的(你没有明确指定它)

父级设置为无(您明确将其指定为无)

请参阅下面的屏幕截图和wxFormBuilder - Python 的“用于制作 GUI 的 GUI”教程

在此处输入图像描述

于 2016-02-10T11:01:14.517 回答
4

您只需将以下内容添加到程序的底部:

if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame(parent=None)
    frame.Show()
    app.MainLoop()

这告诉 Python 要做的是创建wx.App该类的一个实例。该False参数告诉 wxPython 它不应该将标准输出重定向到新窗口。接下来,我们创建您的类的一个实例,MainFrame并将其父级设置为None. 之后,我们Show的框架。如果我们不这样做,代码仍然可以工作,但你什么也看不到。最后,我们调用应用程序对象的MainLoop函数,以便 wxPython 可以真正开始运行和处理事件。

这是完整的代码:

# -*- coding: utf-8 -*-

###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc

###########################################################################
## Class MainFrame
###########################################################################

class MainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Editor_SPED_LP", pos = wx.DefaultPosition, size = wx.Size( 320,255 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.Size( 320,255 ), wx.Size( 320,255 ) )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_textCtrl2 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_textCtrl2, 0, wx.ALL|wx.EXPAND, 5 )

        self.m_btn_abrirArq = wx.Button( self, wx.ID_ANY, u"Abrir arquivo...", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_abrirArq, 0, wx.ALL, 5 )

        self.m_btn_editarTxt = wx.Button( self, wx.ID_ANY, u"Editar .txt", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_editarTxt, 0, wx.ALL, 5 )

        self.m_gauge1 = wx.Gauge( self, wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL )
        self.m_gauge1.SetValue( 0 )
        bSizer1.Add( self.m_gauge1, 0, wx.ALL, 5 )

        self.m_btn_ajuda = wx.Button( self, wx.ID_ANY, u"Ajuda", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_ajuda, 0, wx.ALL, 5 )

        self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText2.Wrap( -1 )
        bSizer1.Add( self.m_staticText2, 0, wx.ALL, 5 )

        self.m_btn_sair = wx.Button( self, wx.ID_ANY, u"Sair", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_sair, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_btn_abrirArq.Bind( wx.EVT_BUTTON, self.abrirArquivo )
        self.m_btn_editarTxt.Bind( wx.EVT_BUTTON, self.editarTxt )
        self.m_btn_ajuda.Bind( wx.EVT_BUTTON, self.janelaAjuda )
        self.m_btn_sair.Bind( wx.EVT_BUTTON, self.sair )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def abrirArquivo( self, event ):
        event.Skip()

    def editarTxt( self, event ):
        event.Skip()

    def janelaAjuda( self, event ):
        event.Skip()

    def sair( self, event ):
        event.Skip()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame(parent=None)
    frame.Show()
    app.MainLoop()
于 2016-02-10T14:10:34.727 回答
0

我成功尝试使用:

#importing wx files
import wx
#import the newly created GUI file
import gui


#inherit from the MainFrame created in wxFormBuilder and create janelaPrincipal
class janelaPrincipal(gui.MainFrame):
    #constructor
    def __init__(self,parent):
        #initialize parent class
        gui.MainFrame.__init__(self,parent)


    #what to do when each function button is clicked

    def editarTxt(self,event):
        try:
            #write the editarTxt code inside here

        except Exception:
            print 'error'


#mandatory in wx
#create an app, False stands for not deteriction stdin/stdout
#refer manual for details
app = wx.App(False)

#create an object of janelaPrincipal
frame = janelaPrincipal(None)
#show the frame
frame.Show(True)
#start the applications
app.MainLoop()

但我实际上不知道它为什么起作用..仍然需要一些解释,拜托.. :-)

于 2015-07-13T13:18:22.273 回答