wxPython frame don't show - python

I have the following Python code that would make a GUI for the command line process of turning a video into frames that is native to VLC Media Player.
# -*- coding: utf-8 -*-
from subprocess import call
import wx
import wx.xrc
vidpath = ''
framedir = ''
def VideoToFrame(videopath, targetdirpath):
"Converts a video into frames. Make sure you have VLC Media Player installed!"
call('vlc.exe "' + videopath + '" --video-filter=scene --vout=dummy --start-time=300 --stop-time=600 --scene-ratio=250 --scene-path=”' + targetdirpath + '” vlc://quit')
if __name__ == "__main__":
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 436,159 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
gbSizer1 = wx.GridBagSizer( 0, 0 )
gbSizer1.SetFlexibleDirection( wx.BOTH )
gbSizer1.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
gbSizer2 = wx.GridBagSizer( 0, 0 )
gbSizer2.SetFlexibleDirection( wx.BOTH )
gbSizer2.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.VidLabel = wx.StaticText( self, wx.ID_ANY, u"Video path", wx.DefaultPosition, wx.DefaultSize, 0 )
self.VidLabel.Wrap( -1 )
gbSizer2.Add( self.VidLabel, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 1 ), wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.m_filePicker1 = wx.FilePickerCtrl( self, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE )
gbSizer2.Add( self.m_filePicker1, wx.GBPosition( 1, 0 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
gbSizer1.Add( gbSizer2, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 1 ), wx.EXPAND, 5 )
gbSizer3 = wx.GridBagSizer( 0, 0 )
gbSizer3.SetFlexibleDirection( wx.BOTH )
gbSizer3.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.FrameLabel = wx.StaticText( self, wx.ID_ANY, u"Frame extraction path", wx.DefaultPosition, wx.DefaultSize, 0 )
self.FrameLabel.Wrap( -1 )
gbSizer3.Add( self.FrameLabel, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 1 ), wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.m_dirPicker1 = wx.DirPickerCtrl( self, wx.ID_ANY, wx.EmptyString, u"Select a folder", wx.DefaultPosition, wx.DefaultSize, wx.DIRP_DEFAULT_STYLE )
gbSizer3.Add( self.m_dirPicker1, wx.GBPosition( 1, 0 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
gbSizer1.Add( gbSizer3, wx.GBPosition( 0, 1 ), wx.GBSpan( 1, 1 ), wx.EXPAND, 5 )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.framebutton = wx.Button( self, wx.ID_ANY, u"Extract frames!", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer1.Add( self.framebutton, 0, wx.ALL, 5 )
gbSizer1.Add( bSizer1, wx.GBPosition( 2, 0 ), wx.GBSpan( 1, 1 ), wx.EXPAND, 5 )
self.SetSizer( gbSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_filePicker1.Bind( wx.EVT_FILEPICKER_CHANGED, self.chvidpath )
self.m_dirPicker1.Bind( wx.EVT_DIRPICKER_CHANGED, self.chframedir )
self.framebutton.Bind( wx.EVT_BUTTON, self.extractframes )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def chvidpath( self, event ):
vidpath = event.path
event.Skip()
def chframedir( self, event ):
framedir = event.path
event.Skip()
def extractframes( self, event ):
VideoToFrame(vidpath, framedir)
event.Skip()
Unfortunately I couldn't manage the frame to show when I ran the script.
The result was... nothing!
Please help! I have no idea of what I am doing, and no other idea of how to make a GUI of a command line process.
I am a good Python player though.

I don't see where you created an instance of your MyFrame1
add:
app = wx.App()
frame = MyFrame1(None)
frame.Show()
app.MainLoop()

Related

Python - Destroying a StaticBoxSizer in wxPython

I'm trying to build an application in Python using wxpython library.
I would like to add and remove widgets dinamically like in the picture
(https://i.stack.imgur.com/bNB3q.png) so that i can add and remove how many StaticBoxSizer I want.
I got a good result in adding widgets while I can't get the way to remove the widgets. I'm successfull in destroying every widget in the StaticBoxSizer but I'm not able to destroy the container StaticBoxSizer itself: if I try to add many blocks and then removing one of them, in the onRemoveMaster method something happens and Python crash.
My code:
import wx
import wx.xrc
import wx.aui
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,
size = wx.Size( 726,624 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
self.number_of_added = 0
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
#Setup MenuBar
self.m_menubar1 = wx.MenuBar( 0 )
self.m_menu1 = wx.Menu()
self.m_menubar1.Append( self.m_menu1, u"MyMenu" )
self.m_menu2 = wx.Menu()
self.m_menubar1.Append( self.m_menu2, u"MyMenu" )
self.SetMenuBar( self.m_menubar1 )
#Setup Main Container
MainSizer = wx.BoxSizer( wx.VERTICAL )
self.ScrolledWindow = wx.ScrolledWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
self.ScrolledWindow.SetScrollRate( 1, 1 )
MainVerticalSizer = wx.BoxSizer( wx.VERTICAL )
self.m_auinotebook1 = wx.aui.AuiNotebook( self.ScrolledWindow, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.aui.AUI_NB_DEFAULT_STYLE )
#Costruzione Master Sequence Tab
self.MasterSequenceTab = wx.ScrolledWindow( self.m_auinotebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
self.MasterSequenceTab.SetScrollRate( 1, 1 )
self.MasterVSizer = wx.BoxSizer( wx.VERTICAL )
MasterButtonSizer = wx.BoxSizer( wx.HORIZONTAL )
self.ButtonAdd = wx.Button( self.MasterSequenceTab, wx.ID_ANY, u"Add", wx.DefaultPosition, wx.DefaultSize, 0 )
MasterButtonSizer.Add( self.ButtonAdd, 0, wx.ALL, 5 )
self.ButtonAdd.Bind(wx.EVT_BUTTON, self.onAddMaster)
self.ButtonRemove = wx.Button( self.MasterSequenceTab, wx.ID_ANY, u"Remove", wx.DefaultPosition, wx.DefaultSize, 0 )
MasterButtonSizer.Add( self.ButtonRemove, 0, wx.ALL, 5 )
self.ButtonRemove.Bind(wx.EVT_BUTTON, self.onRemoveMaster)
self.MasterVSizer.Add( MasterButtonSizer, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 5 )
self.onAddMaster(self)
self.m_auinotebook1.AddPage( self.MasterSequenceTab, u"Master Sequence", False )
#Finalizzazione finestre
MainVerticalSizer.Add( self.m_auinotebook1, 1, wx.EXPAND, 5 )
self.ScrolledWindow.SetSizer( MainVerticalSizer )
self.ScrolledWindow.Layout()
MainVerticalSizer.Fit( self.ScrolledWindow )
MainSizer.Add( self.ScrolledWindow, 1, wx.EXPAND |wx.ALL, 5 )
self.SetSizer( MainSizer )
self.Layout()
self.Centre( wx.BOTH )
def onAddMaster(self, event):
self.number_of_added += 1
self.MastersbSizerAdded = wx.StaticBoxSizer( wx.StaticBox( self.MasterSequenceTab, 100+self.number_of_added, wx.EmptyString ),wx.VERTICAL )
self.MasterCheckEnableAdded = wx.CheckBox( self.MastersbSizerAdded.GetStaticBox(), 200+self.number_of_added, u"Enable", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterCheckEnableAdded.SetValue(True)
self.MastersbSizerAdded.Add( self.MasterCheckEnableAdded, 0, wx.ALIGN_RIGHT|wx.ALL, 5 )
MasterfgSizerAdded = wx.FlexGridSizer( 0, 2, 0, 0 )
MasterfgSizerAdded.SetFlexibleDirection( wx.BOTH )
MasterfgSizerAdded.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.MasterTextNomeAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1000+self.number_of_added, u"Nome", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextNomeAdded.Wrap( -1 )
MasterfgSizerAdded.Add( self.MasterTextNomeAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlNomeAdded = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 300+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.Size( 566,-1 ), 0 )
MasterfgSizerAdded.Add( self.MasterCtrlNomeAdded, 0, wx.ALL, 5 )
self.MasterTextDescrizioneAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1100+self.number_of_added, u"Descrizione ", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextDescrizioneAdded.Wrap( -1 )
MasterfgSizerAdded.Add( self.MasterTextDescrizioneAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlDescrizione1 = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 400+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.Size( 566,-1 ), 0 )
MasterfgSizerAdded.Add( self.MasterCtrlDescrizione1, 0, wx.ALL, 5 )
self.MastersbSizerAdded.Add( MasterfgSizerAdded, 0, wx.ALL, 5 )
MasterfgSizerAdded2 = wx.FlexGridSizer( 3, 6, 0, 0 )
MasterfgSizerAdded2.SetFlexibleDirection( wx.BOTH )
MasterfgSizerAdded2.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.MasterTextSubjectAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1200+self.number_of_added, u"Subject Area", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextSubjectAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextSubjectAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlSubjectAdded = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 500+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlSubjectAdded, 0, wx.ALL, 5 )
self.MasterTextAlimentanteAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1300+self.number_of_added, u" Alimentante", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextAlimentanteAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextAlimentanteAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlAlimentanteAdded = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 600+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlAlimentanteAdded, 0, wx.ALL, 5 )
self.MasterTextGroupAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1400+self.number_of_added, u" Num group", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextGroupAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextGroupAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlGroupAdded = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 700+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlGroupAdded, 0, wx.ALL, 5 )
self.MasterTextSchedulingAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1500+self.number_of_added, u"Schedulazione", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextSchedulingAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextSchedulingAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
MasterCtrlSchedulingChoicesAdded = [ u"G", u"S", u"D", u"Q", u"M", u"BM", u"TM", u"SM", u"A" ]
self.MasterCtrlSchedulingAdded = wx.Choice( self.MastersbSizerAdded.GetStaticBox(), 800+self.number_of_added, wx.DefaultPosition, wx.DefaultSize, MasterCtrlSchedulingChoicesAdded, 0 )
self.MasterCtrlSchedulingAdded.SetSelection( 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlSchedulingAdded, 0, wx.ALL, 5 )
self.MasterTextValidoAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1600+self.number_of_added, u" Record valido", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextValidoAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextValidoAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
MasterCtrlValido1Choices = [ u"Y", u"N" ]
self.MasterCtrlValidoAdded = wx.Choice( self.MastersbSizerAdded.GetStaticBox(), 900+self.number_of_added, wx.DefaultPosition, wx.DefaultSize, MasterCtrlValido1Choices, 0 )
self.MasterCtrlValidoAdded.SetSelection( 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlValidoAdded, 0, wx.ALL, 5 )
self.MastersbSizerAdded.Add( MasterfgSizerAdded2, 0, wx.ALL, 5 )
self.MasterVSizer.Add( self.MastersbSizerAdded, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.MasterSequenceTab.SetSizer( self.MasterVSizer )
self.MasterSequenceTab.Layout()
self.MasterVSizer.Fit( self.MasterSequenceTab )
def onRemoveMaster(self, event):
for i in range(16,0,-1):
button = 100*i+self.number_of_added
widget = self.MasterSequenceTab.FindWindowById(button)
widget.Destroy()
self.MasterSequenceTab.SetSizer( self.MasterVSizer )
self.MasterSequenceTab.Layout()
self.MasterVSizer.Fit( self.MasterSequenceTab )
self.number_of_added -= 1
if __name__ == '__main__':
app = wx.App()
Example(None, title='Test')
app.MainLoop()
It would be much easier to create a new panel class with all those controls parented to it, and then just add and delete instances of the panel.
The panel instances could be stored in a list.

python just restarts and does not run the program at all

can anyone help me with this? i created a gui using wxformbuilder which generated python code for. then i created a separate code on how it should be working.
this first code is for the gui only.
# -*- 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()
then here is the code to be run on 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()
everytime i run it on rpi. python shell just always display -------------------------------restart------------------------- and the gui doesnt appear at all. i just wanna know what is the problem with the code.
and also i created the code on windows and i copied both python files on rpi. on windows it works properly. i sincerely thank you for your hep im just a student and a newbie so i can't really point out what is wrong with my code. thanks :)))
Are you running this via ./myscript.py? If so, you need to add the interpreter to the top. The first line should be:
#!/usr/bin/env python
or something similar so that it knows which interpreter to use.
I know this is not the question, but I'm more concerned about your actual program, to be honest. For example, instead of writing multiple consecutive QN = "QN", you can create them all as a dict and update the global variables. For example:
tmp = ["Q{}".format(i+1) for i in range(20)]
globals().update(dict(zip(tmp, tmp)))
That's significantly easier. Or, better yet, you can map numbers to your desired strings in the form of a dict so the entire thing can be rewritten as:
#!/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()

wx.StaticLine doesn't always display

I have a wx.StaticLine separating some buttons in a wx.GridBagSizer. When the window initially draws, the StaticLine does not show up. Sometimes after a long running task (and I move the window around or something, presumably forcing the frame to be redrawn) it will show up out of nowhere.
Here is what it looks like:
When window initially draws:
After some long running task (or resize):
I create the line with:
self.m_staticline1 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
gbSizer101.Add( self.m_staticline1, wx.GBPosition( 2, 0 ), wx.GBSpan( 1, 2 ), wx.EXPAND |wx.ALL, 5 )
and at the end of the init I draw with:
self.SetSizer( gbSizer1 )
self.Layout()
gbSizer1.Fit( self )
I'm not sure if this is significant, but here is how I start the application:
app = wx.App(False)
frame = FWHM_Application(None)
frame.Show()
app.MainLoop()
Any ideas why this might be happening and how to make it draw initially?
EDIT: Here is a working sample program demonstrating the issue:
import wx
class Test_Frame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test GUI", pos = wx.DefaultPosition, size = wx.Size( -1,-1 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
# Outer container
gbSizer_outer = wx.GridBagSizer( 4, 3 )
gbSizer_outer.SetFlexibleDirection( wx.BOTH )
gbSizer_outer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
# Static box for analysis
analyzeImageBox = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"Analyze Single Image" ), wx.VERTICAL )
# Sizer for button/label layout within static box
gbSizer_single = wx.GridBagSizer( 0, 0 )
gbSizer_single.SetFlexibleDirection( wx.BOTH )
gbSizer_single.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.analyzeImage = wx.Button( self, wx.ID_ANY, u"Analyze Image", wx.DefaultPosition, wx.Size( 170,45 ), 0 )
gbSizer_single.Add( self.analyzeImage, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 2 ), wx.ALL, 5 )
self.m_staticline1 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
gbSizer_single.Add( self.m_staticline1, wx.GBPosition( 1, 0 ), wx.GBSpan( 1, 2 ), wx.EXPAND |wx.ALL, 5 )
self.plotX = wx.Button( self, wx.ID_ANY, u"Plot X", wx.DefaultPosition, wx.Size( 70,35 ), 0 )
gbSizer_single.Add( self.plotX, wx.GBPosition( 2, 0 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
self.plotY = wx.Button( self, wx.ID_ANY, u"Plot Y", wx.DefaultPosition, wx.Size( 70,35 ), 0 )
gbSizer_single.Add( self.plotY, wx.GBPosition( 2, 1 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
analyzeImageBox.Add( gbSizer_single, 1, wx.EXPAND, 5 )
gbSizer_outer.Add( analyzeImageBox, wx.GBPosition( 1, 1 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
self.SetSizer( gbSizer_outer )
gbSizer_outer.Fit( self )
self.Layout()
self.Centre( wx.BOTH )
app = wx.App(False)
frame = Test_Frame(None)
frame.Show()
app.MainLoop()
self.SetSizer(gbSizer1)
gbSizer1.Fit(self)
self.Layout()
Try calling layout last.

Python/wxPython: How to reset wx.Choice to a default value

Am working with multiple wx.Choice control in wxPython and I needed the make a reset button to restore the default option “-- Select --” when clicked.
Am unable to achieve that, the closest I came is to reset to empty options which is NOT what is want. I want the default option “-- Select --” to come up when reset button is pressed. See my code below.
data.py
import wx
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 250,300 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
DataBox = wx.BoxSizer( wx.HORIZONTAL )
gSizer2 = wx.GridSizer( 0, 2, 0, 0 )
self.Color_Label = wx.StaticText( self, wx.ID_ANY, u"Color:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.Color_Label.Wrap( -1 )
self.Color_Label.SetFont( wx.Font( 13, 70, 90, 90, False, wx.EmptyString ) )
gSizer2.Add( self.Color_Label, 0, wx.ALL, 5 )
Color_optionsChoices = [ u"-- Select --", u"Red", u"Green", u"Pink", u"Blue", u"Yellow", u"White", u"Brown" ]
self.Color_options = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Color_optionsChoices, 0 )
self.Color_options.SetSelection( 0 )
gSizer2.Add( self.Color_options, 0, wx.ALL, 5 )
self.Age = wx.StaticText( self, wx.ID_ANY, u"Age:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.Age.Wrap( -1 )
self.Age.SetFont( wx.Font( 13, 70, 90, 90, False, wx.EmptyString ) )
gSizer2.Add( self.Age, 0, wx.ALL, 5 )
Age_optionsChoices = [ u"-- Select --", u"15", u"16", u"17", u"18", u"19", u"20", u"21", u"22", u"23", u"24", u"25", wx.EmptyString ]
self.Age_options = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Age_optionsChoices, 0 )
self.Age_options.SetSelection( 0 )
gSizer2.Add( self.Age_options, 0, wx.ALL, 5 )
self.Country = wx.StaticText( self, wx.ID_ANY, u"Country:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.Country.Wrap( -1 )
self.Country.SetFont( wx.Font( 13, 70, 90, 90, False, wx.EmptyString ) )
gSizer2.Add( self.Country, 0, wx.ALL, 5 )
Country_optionsChoices = [ u"-- Select --", u"Mexico", u"Peru", u"India", u"USA", u"UK", u"Greece", u"Agentina", u"Greece", u"Brazil", u"Egypt" ]
self.Country_options = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Country_optionsChoices, 0 )
self.Country_options.SetSelection( 0 )
gSizer2.Add( self.Country_options, 0, wx.ALL, 5 )
self.Reset_button = wx.Button( self, wx.ID_ANY, u"Reset", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.Reset_button, 0, wx.ALL, 5 )
self.Exit_button = wx.Button( self, wx.ID_ANY, u"Exit", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.Exit_button, 0, wx.ALL, 5 )
DataBox.Add( gSizer2, 1, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 5 )
self.SetSizer( DataBox )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.Reset_button.Bind( wx.EVT_BUTTON, self.OnResetButton )
self.Exit_button.Bind( wx.EVT_BUTTON, self.OnExitButton )
def OnResetButton( self, event ):
# val = '-- Select --' # NOT WORKING
val = ' ' # THIS WORKS, BUT RESETS TO EMPTY CHOICE
self.Color_options.SetLabel(val)
self.Age_options.SetLabel(val)
self.Country_options.SetLabel(val)
def OnExitButton( self, event ):
self.Close()
app = wx.App(0)
MyFrame1(None).Show()
app.MainLoop()
Thanks for your time in advance.
Please reduce your source code to a minimal runnable example (explained below).
Answer: I am amazed that wx.Choice.SetLabel(' ') does something useful. What you want to do instead is:
self.Color_Options.SetStringSelection(val)
(see the wxWidgets documentation for wxChoice/wxItemContainer).
Remark: u'--Select--' is not '--Select--' in Python. As it happens, if the source encoding is set to UTF-8, wxPython will not complain and understand str encoden in UTF-8 as well as u''.
Minimal runnable example: As important as ever: Making your example as small as possible will teach you which parts are relevant for your question and which are not. In many cases, by writing the minimal example, I very often find the answer myself.
Minimising your example:
import wx
class MyFrame1(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
pnl = wx.Panel(self, wx.ID_ANY)
szmain = wx.BoxSizer(wx.VERTICAL)
color_choices = [u"-- Select --", u"Red", u"Green", u"Pink", u"Blue", u"Yellow", u"White", u"Brown"]
self.color_options = wx.Choice(pnl, wx.ID_ANY, choices=color_choices)
self.color_options.SetSelection(0)
self.reset_button = wx.Button(pnl, wx.ID_ANY, u"Reset")
szmain.Add(self.color_options, 0, wx.ALL|wx.EXPAND, 4)
szmain.Add(self.reset_button, 0, wx.ALL|wx.EXPAND, 4)
pnl.SetSizer(szmain)
self.reset_button.Bind( wx.EVT_BUTTON, self.OnResetButton )
def OnResetButton(self, event):
val = '-- Select --' # NOT WORKING
# val = ' ' # THIS WORKS, BUT RESETS TO EMPTY CHOICE
self.color_options.SetLabel(val)
app = wx.App(0)
MyFrame1(None).Show()
app.MainLoop()

updating either a static text or textctrl *read only* upon selection of an item in a choice box

Basically, what I'm trying to do is get either a static label or read only textctrl to change it's price accordingly with the item selected in the choice widget. the lists for the Item_ID's and respective prices share the same indexes despite being on 2 different lists given their simultaneous creation... thing is no matter what Item I select, the price remains the same either way Code above has the static text method... the read only textctrl turned out to be the same any ideas on what can be done?
the code of the dialog in question
class AddItemOrder ( wx.Dialog ):
def __init__( self, parent,entryit ):
wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"Add Item", pos = wx.DefaultPosition, size = wx.Size( 285,173 ), style = wx.DEFAULT_DIALOG_STYLE )
self.entryit = entryit
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
self.ite = i.Item()
bSizer25 = wx.BoxSizer( wx.VERTICAL )
gSizer9 = wx.GridSizer( 2, 2, 0, 0 )
self.m_staticText34 = wx.StaticText( self, wx.ID_ANY, u"Item ID", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText34.Wrap( -1 )
gSizer9.Add( self.m_staticText34, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.m_choice2Choices = []
self.create_choices(self.m_choice2Choices)
self.Item_ID = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, self.m_choice2Choices, 0 )
self.Item_ID.SetSelection( 0 )
self.Item_ID.Bind(EVT_CHOICE,self.get_index)
gSizer9.Add( self.Item_ID, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.m_staticText17 = wx.StaticText( self, wx.ID_ANY, u"Item Price", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText17.Wrap( -1 )
gSizer9.Add( self.m_staticText17, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.Order_Price = wx.StaticText( self, wx.ID_ANY, str(self.prices[self.Item_ID.GetSelection() ]), wx.DefaultPosition, wx.DefaultSize, 0 )
self.Order_Price.Wrap( -1 )
self.Order_Price.SetLabel(str(self.prices[self.Item_ID.GetSelection() ]))
gSizer9.Add( self.Order_Price, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.m_staticText35 = wx.StaticText( self, wx.ID_ANY, u"Item Qty", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText35.Wrap( -1 )
gSizer9.Add( self.m_staticText35, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.Item_Qty = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 120,-1 ), 0 )
gSizer9.Add( self.Item_Qty, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
bSizer25.Add( gSizer9, 1, wx.EXPAND, 5 )
self.m_button19 = wx.Button( self, wx.ID_ANY, u"Ok", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_button19.Bind(wx.EVT_BUTTON, self.ok)
bSizer25.Add( self.m_button19, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 5 )
self.SetSizer( bSizer25 )
self.Layout()
self.Centre( wx.BOTH )
the create choices and get index functions
def create_choices(self,event):
self.prices = []
for entry in self.ite.entries:
self.custom = entry.Item_ID
self.customPr = entry.Item_Price
self.prices.append(self.customPr)
self.m_choice2Choices.append(self.custom)
return self.m_choice2Choices
def get_index(self,event):
self.currIndex = self.Item_ID.GetSelection()
return self.currIndex
When you want to change the text shown by the statictext (presumably when the EVT_CHOICE handler in this case) then you just need to call the statictext's SetLabel method with the new text. So, something like this:
newText = str(self.prices[self.currIndex])
self.m_staticText17.SetLabel(newText)

Categories