0

谁能帮我这个?我使用 wxformbuilder 创建了一个 gui,它为其生成了 python 代码。然后我创建了一个关于它应该如何工作的单独代码。

这第一个代码仅适用于 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 frmQuestions
###########################################################################

class frmQuestions ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 1055,562 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHT ) )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.lblQbox = wx.StaticText( self, wx.ID_ANY, u"Question:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.lblQbox.Wrap( -1 )
        self.lblQbox.SetFont( wx.Font( 18, 73, 93, 90, False, "Brush Script MT" ) )

        bSizer1.Add( self.lblQbox, 0, wx.ALL|wx.EXPAND, 5 )

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

        self.btnNext = wx.Button( self, wx.ID_ANY, u"Next", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.btnNext.SetFont( wx.Font( 12, 72, 90, 90, False, "Cooper Black" ) )

        bSizer1.Add( self.btnNext, 0, wx.ALL|wx.EXPAND, 5 )

        self.btnShowResults = wx.Button( self, wx.ID_ANY, u"Show Results", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.btnShowResults.SetFont( wx.Font( 12, 72, 90, 90, False, "Cooper Black" ) )

        bSizer1.Add( self.btnShowResults, 0, wx.ALL|wx.EXPAND, 5 )

        self.btnExit = wx.Button( self, wx.ID_ANY, u"Exit", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.btnExit.SetFont( wx.Font( 12, 72, 90, 90, False, "Cooper Black" ) )

        bSizer1.Add( self.btnExit, 0, wx.ALL|wx.EXPAND, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.btnNext.Bind( wx.EVT_BUTTON, self.OnNext )
        self.btnShowResults.Bind( wx.EVT_BUTTON, self.OnShowResults )
        self.btnExit.Bind( wx.EVT_BUTTON, self.OnExit )

    def __del__( self ):
        pass


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

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

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


###########################################################################
## Class frmResults
###########################################################################

class frmResults ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        self.SetForegroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_ACTIVECAPTION ) )
        self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHT ) )

        bSizer2 = wx.BoxSizer( wx.VERTICAL )

        self.txtResults = wx.StaticText( self, wx.ID_ANY, u"Results", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.txtResults.Wrap( -1 )
        self.txtResults.SetFont( wx.Font( 12, 72, 90, 90, False, "Cooper Black" ) )

        bSizer2.Add( self.txtResults, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

        self.txtA = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.txtA, 1, wx.ALL|wx.EXPAND, 5 )

        self.txtB = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.txtB, 1, wx.ALL|wx.EXPAND, 5 )

        self.txtC = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.txtC, 1, wx.ALL|wx.EXPAND, 5 )

        self.txtD = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.txtD, 1, wx.ALL|wx.EXPAND, 5 )

        self.btnGoBack = wx.Button( self, wx.ID_ANY, u"Go Back", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.btnGoBack.SetFont( wx.Font( 12, 72, 90, 90, False, "Cooper Black" ) )

        bSizer2.Add( self.btnGoBack, 0, wx.ALL|wx.EXPAND, 5 )


        self.SetSizer( bSizer2 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.btnGoBack.Bind( wx.EVT_BUTTON, self.OnGoBack )

    def __del__( self ):
        pass


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

那么这里是要在 rpi 上运行的代码:

from noname import *
from wx import *

Q1 = 'Q1'
Q2 = 'Q2'
Q3 = 'Q3'
Q4 = 'Q4'
Q5 = 'Q5'
Q6 = 'Q6'
Q7 = 'Q7'
Q8 = 'Q8'
Q9 = 'Q9'
Q10 = 'Q10'
Q11 = 'Q11'
Q12 = 'Q12'
Q13 = 'Q13'
Q14 = 'Q14'
Q15 = 'Q15'
Q16 = 'Q16'
Q17 = 'Q17'
Q18 = 'Q18'
Q19 = 'Q19'
Q20 = 'Q20'



class Questions(frmQuestions):
    x = 0
    def __init__(self, parent):
        frmQuestions.__init__(self,parent)
        self.txtQ.SetLabel('')

    def OnNext(self, event):
        self.x = self.x + 1
        if (self.x==1):
            self.txtQ.SetLabel(Q1)
        elif (self.x==2):
            self.txtQ.SetLabel(Q2)
        elif (self.x==3):
            self.txtQ.SetLabel(Q3)
        elif (self.x==4):
            self.txtQ.SetLabel(Q4)
        elif (self.x==5):
            self.txtQ.SetLabel(Q5)
        elif (self.x==6):
            self.txtQ.SetLabel(Q6)
        elif (self.x==7):
            self.txtQ.SetLabel(Q7)
        elif (self.x==8):
            self.txtQ.SetLabel(Q8)
        elif (self.x==9):
            self.txtQ.SetLabel(Q9)
        elif (self.x==10):
            self.txtQ.SetLabel(Q10)
        elif (self.x==11):
            self.txtQ.SetLabel(Q11)
        elif (self.x==12):
            self.txtQ.SetLabel(Q12)
        elif (self.x==13):
            self.txtQ.SetLabel(Q13)
        elif (self.x==14):
            self.txtQ.SetLabel(Q14)
        elif (self.x==15):
            self.txtQ.SetLabel(Q15)
        elif (self.x==16):
            self.txtQ.SetLabel(Q16)
        elif (self.x==17):
            self.txtQ.SetLabel(Q17)
        elif (self.x==18):
            self.txtQ.SetLabel(Q18)
        elif (self.x==19):
            self.txtQ.SetLabel(Q19)
        elif (self.x==20):
            self.txtQ.SetLabel(Q20)
        else:
            pass
        print self.x

    def OnShowResults(self, event):
        results.Show()

    def OnExit(self, event):
        self.Destroy()



class Results(frmResults):
    def __init__(self, parent):
        frmResults.__init__(self,parent)

    def OnGoBack(self, event):
        self.txtA.SetLabel('')
        self.txtB.SetLabel('')
        self.txtC.SetLabel('')
        self.txtD.SetLabel('')
        self.Hide()


app = wx.App(False)
results = Results(None)
questions = Questions(None)
questions.Show()


app.MainLoop()

每次我在 rpi 上运行它。python shell总是显示-------------------重新启动------------- ------------ 并且gui根本没有出现。我只想知道代码有什么问题。

我还在 Windows 上创建了代码,并在 rpi 上复制了两个 python 文件。在 Windows 上它可以正常工作。我真诚地感谢您的帮助,我只是一个学生和新手,所以我无法真正指出我的代码有什么问题。谢谢 :)))

4

1 回答 1

0

你是通过这个运行./myscript.py吗?如果是这样,您需要将解释器添加到顶部。第一行应该是:

#!/usr/bin/env python

或类似的东西,以便它知道使用哪个解释器。

我知道这不是问题,但老实说,我更关心你的实际程序。例如QN = "QN",您可以将它们全部创建为 dict 并更新全局变量,而不是编写多个连续的。例如:

tmp = ["Q{}".format(i+1) for i in range(20)]
globals().update(dict(zip(tmp, tmp)))

这要容易得多。或者,更好的是,您可以将数字以 dict 的形式映射到所需的字符串,这样整个事情就可以重写为:

#!/usr/bin/env python
from noname import *
from wx import *

tmp = ["Q{}".format(i+1) for i in range(20)] # Create a list of "Q1"..."Q20"
vars = dict(enumerate(tmp, 1)) # { 1 : "Q1", 2 : "Q2", ... 20 : "Q20" }

class Questions(frmQuestions):
    def __init__(self, parent):
        self.x = 0 # Made this an instance variable
        frmQuestions.__init__(self,parent)
        self.txtQ.SetLabel('')

    def OnNext(self, event):
        self.x = self.x + 1
        if self.x in vars:
            self.txtQ.SetLabel(vars[self.x]) # significantly simplified
        print(self.x)

    def OnShowResults(self, event):
        results.show()

    def OnExit(self, event):
        self.Destroy()

class Results(frmResults):
    def __init__(self, parent):
        frmResults.__init__(self,parent)

    def OnGoBack(self, event):
        for _ in [self.txtA, self.txtB, self.txtC, self.txtD]:
            _.SetLabel('')
        self.Hide()

app = wx.App(False)
results = Results(None)
questions = Questions(None)
questions.Show()


app.MainLoop()
于 2016-02-10T06:44:44.950 回答