Related
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()
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()
I am creating a Python application that requires a login on start up. I want to have a main panel that will hold my login panel and on successful login the login panel will hide and the main wx.Notebook will show. The following code works, but if in the login panel I re-size the application, after I successfully login and go to the main wx.Notebook the wx.Notebook does not fit the size of the screen. If I resize again while in the main wx.Notebook the main wx.Notebook will fit the window. How do I get the main wx.Notebook to automatically re-size to the window?
Main.py
import wx
import os
from titlePanel import titlePanel
from OneLblOneCB_HorzBoxSizer_Panel import OneLblOneCB_HorzBoxSizer_Panel
from OneLblOneMultiTxt_HorzBoxSizer_Panel import OneLblOneMultiTxt_HorzBoxSizer_Panel
from OneLblOneSingleTxt_HorzBoxSizer_Panel import OneLblOneSingleTxt_HorzBoxSizer_Panel
from OneLblTxtFile_HorzBoxSizer_Panel import OneLblTxtFile_HorzBoxSizer_Panel
from OneBtn_HorzBoxSizer_Panel import OneBtn_HorzBoxSizer_Panel
from LoginPanel import LoginPanel
from ProjectsPanel import ProjectsPanel
from EvDB import EvDB
class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title=title, size=(650,725))
# Create Database Tables
self.db = EvDB(self)
self.db.createTbls()
# Setting up the menu.
filemenu= wx.Menu()
ID_LOGIN = wx.NewId()
ID_LOGOUT = wx.NewId()
# wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open and existing file")
menuAbout = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
filemenu.AppendSeparator()
menuLogin = filemenu.Append(ID_LOGIN, 'Login')
menuLogout = filemenu.Append(ID_LOGOUT, 'Logout')
filemenu.AppendSeparator()
menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
# Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
# Set events.
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen )
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout )
self.Bind(wx.EVT_MENU, self.OnLogin, menuLogin )
self.Bind(wx.EVT_MENU, self.OnLogout, menuLogout )
self.Bind(wx.EVT_MENU, self.OnExit, menuExit )
main = wx.Panel(self)
self.mainLogin = LoginPanel(main,-1,addSpacers=1)
self.mainLogin.Show()
# Create a notebook on the panel
self.nb = wx.Notebook(main)
self.nb.Hide()
# create the page windows as children of the notebook
flowchartPg = wx.Panel(self.nb)
entryPg = wx.ScrolledWindow(self.nb, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, style=wx.VSCROLL)
entryPg.SetScrollRate( 5, 5 )
projectsPg = ProjectsPanel(self.nb, -1)
# add the pages to the notebook with the label to show on the tab
self.nb.AddPage(projectsPg, "Projects")
self.nb.AddPage(entryPg, "Entry")
self.nb.AddPage(flowchartPg, "Flowchart")
self.entTitle = titlePanel(entryPg, -1)
self.entDescr = OneLblOneMultiTxt_HorzBoxSizer_Panel(entryPg,-1,
name="entDescr", lbl="Description")
self.srcTypeList = ['None','Website', 'Youtube', 'PDF', 'Book']
self.entSourceType = OneLblOneCB_HorzBoxSizer_Panel(entryPg,-1,
name="entSourceType", lbl="Source Type: ", cbList=self.srcTypeList, startVal='None')
self.entSource = OneLblTxtFile_HorzBoxSizer_Panel(entryPg,-1,
name="entSource", lbl="Source: ")
self.entSource.singleTxt.SetEditable(False)
self.entSource._filename.Disable()
self.entAuthor = OneLblOneSingleTxt_HorzBoxSizer_Panel(entryPg,-1,
name="entAuthor", lbl="Author: ", addSpacers=0)
self.entAuthorCre = OneLblOneMultiTxt_HorzBoxSizer_Panel(entryPg,-1,
name="entAuthorCre", lbl="Author's Credentials: ")
self.asPrjList = ['None','Project1', 'Project2', 'Project3', 'Project4']
self.entAsProject = OneLblOneCB_HorzBoxSizer_Panel(entryPg,-1,
name="asProject", lbl="Assign to Project: ", cbList=self.asPrjList, startVal='None')
self.saveOrEditList = ['New','Ev1', 'Ev2', 'Ev3', 'Ev4']
self.entSaveOrEdit = OneLblOneCB_HorzBoxSizer_Panel(entryPg,-1,
name="saveOrEdit", lbl="New or Edit: ", cbList=self.saveOrEditList, startVal='New')
self.entRemarks = OneLblOneMultiTxt_HorzBoxSizer_Panel(entryPg,-1,
name="sourceRemarks", lbl="Evidence Remarks: ")
self.entRemarks.multiTxt.SetEditable(False)
self.entAddBtn = OneBtn_HorzBoxSizer_Panel(entryPg, -1, name="entAddBtn", btn="Add")
self.entSaveBtn = OneBtn_HorzBoxSizer_Panel(entryPg, -1, name="entSaveBtn", btn="Save")
#self.loginTest = LoginPanel(entryPg, -1,addSpacers=1)
self.entSaveBtn.button.Hide()
# Bindings
self.Bind(wx.EVT_COMBOBOX, self.SourceTypeEvtComboBox, self.entSourceType.cb)
self.Bind(wx.EVT_COMBOBOX, self.AsProjectEvtComboBox, self.entAsProject.cb)
self.Bind(wx.EVT_COMBOBOX, self.SaveOrEditEvtComboBox, self.entSaveOrEdit.cb)
self.Bind(wx.EVT_BUTTON, self.EvtAddBtn, self.entAddBtn.button)
self.Bind(wx.EVT_BUTTON, self.EvtSaveBtn, self.entSaveBtn.button)
self.Bind(wx.EVT_BUTTON, self.EvtLoginBtn, self.mainLogin.loginBtns.LoginBtn)
self.Bind(wx.EVT_BUTTON, self.EvtLogoutBtn, self.mainLogin.loginBtns.LogoutBtn)
# Creating Sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
entryPgBox = wx.BoxSizer(wx.VERTICAL)
# Adding Panels to BoxSizer entry panel sizer
mainSizer.AddSpacer(10)
mainSizer.Add(self.nb, 1, wx.ALL|wx.EXPAND)
mainSizer.Add(self.mainLogin, 1, wx.ALL|wx.EXPAND)
entryPgBox.AddSpacer(20)
entryPgBox.Add(self.entAsProject, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
entryPgBox.Add(self.entSaveOrEdit, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
entryPgBox.Add(self.entTitle, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
entryPgBox.Add(self.entDescr, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
entryPgBox.Add(self.entSourceType, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
entryPgBox.Add(self.entSource, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
entryPgBox.Add(self.entAuthor, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
entryPgBox.Add(self.entAuthorCre, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
entryPgBox.Add(self.entRemarks, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
entryPgBox.Add(self.entAddBtn, 0, wx.EXPAND)
entryPgBox.Add(self.entSaveBtn, 0, wx.EXPAND)
entryPgBox.AddSpacer(10)
# Setting Layouts
entryPg.SetAutoLayout(True)
entryPg.SetSizer(entryPgBox)
entryPgBox.Fit(entryPg)
main.SetAutoLayout(True)
main.SetSizer(mainSizer)
mainSizer.Fit(main)
self.Layout()
self.Show()
def OnLogin(self,e):
self.nb.Hide()
self.mainLogin.Show()
self.Layout()
self.mainLogin.Layout()
def OnLogout(self,e):
self.mainLogin.Show()
self.nb.Hide()
self.Layout()
self.mainLogin.Layout()
def EvtLoginBtn(self,e):
self.nb.Show()
self.mainLogin.Hide()
self.Layout()
self.nb.Layout()
LoginPanel.py
class LoginPanel(wx.Panel):
def __init__(self, parent, ID, addSpacers):
wx.Panel.__init__(self, parent, ID)
sizer = wx.BoxSizer(wx.VERTICAL)
self.userNamePnl = OneLblOneSingleTxt_HorzBoxSizer_Panel(self,-1,
name="loginUser", lbl="Username: ", addSpacers=1)
self.passwordPnl = OneLblOneSingleTxt_HorzBoxSizer_Panel(self,-1,
name="loginPass", lbl="Password: ", addSpacers=1)
self.loginBtns = LoginBtnsPanel(self,-1)
if addSpacers == 1:
sizer.AddStretchSpacer()
sizer.Add(self.userNamePnl,0,wx.EXPAND)
sizer.AddSpacer(10)
sizer.Add(self.passwordPnl,0,wx.EXPAND)
sizer.AddSpacer(10)
sizer.Add(self.loginBtns,0,wx.EXPAND)
if addSpacers == 1:
sizer.AddStretchSpacer()
self.SetAutoLayout(True)
self.SetSizer(sizer)
sizer.Fit(self)
Got it! I was trying to redraw the frame with Layout(), but I needed to redraw the BoxSizer with Layout()
I added the following code to the login button:
def EvtLoginBtn(self,e):
self.nb.Show()
self.mainLogin.Hide()
self.mainSizer.Layout()
Call the Layout() method of the Frame and Panel like this
def EvtLoginBtn(self,e):
self.nb.Show()
self.mainLogin.Hide()
self.Layout()
self.nb.Layout()
The last call is probably not needed. Just check the program without it as well. The Layout() method redraws the Frame or Panel or whichever widget it is a method of.
UPDATE
Your code is obviously very big and has several other parts which I do not know. Anyways, here's something that I came up with, along the lines of the code you have provided, to show how you can do this. Hope this helps:
Main.py
import wx
from LoginPanel import LoginPanel
class MyFrame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Something something", pos = wx.DefaultPosition, size = wx.Size( 300,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
BoxSizer0 = wx.BoxSizer( wx.VERTICAL )
# Add login panel
self.login_panel = LoginPanel(self)
BoxSizer0.Add(self.login_panel, 1, wx.EXPAND | wx.ALL, 0)
# Add notebook panel and its pages
self.nb = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
self.nb_subpanel1 = wx.Panel( self.nb, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.nb.AddPage( self.nb_subpanel1, u"something", False )
self.nb_subpanel2 = wx.Panel( self.nb, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.nb.AddPage( self.nb_subpanel2, u"something else", False )
BoxSizer0.Add( self.nb, 1, wx.EXPAND |wx.ALL, 0 )
# Adds a logout button
self.logout_button = wx.Button( self, wx.ID_ANY, u"Logout", wx.DefaultPosition, wx.DefaultSize, 0 )
BoxSizer0.Add( self.logout_button, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
# Hide nb and logout button
self.nb.Hide()
self.logout_button.Hide()
self.SetSizer( BoxSizer0 )
self.Layout()
self.Centre( wx.BOTH )
self.Show()
# Connect Events
self.logout_button.Bind( wx.EVT_BUTTON, self.on_logout )
# Virtual event handlers, override them in your derived class
def on_logout( self, event ):
self.nb.Hide()
self.logout_button.Hide()
self.login_panel.Show()
self.Layout()
if __name__ == "__main__":
app = wx.App()
MyFrame(None)
app.MainLoop()
LoginPanel.py
import wx
class LoginPanel ( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 300,300 ), style = wx.TAB_TRAVERSAL )
BoxSizer01 = wx.BoxSizer( wx.VERTICAL )
self.login_button = wx.Button( self, wx.ID_ANY, u"Login", wx.DefaultPosition, wx.DefaultSize, 0 )
self.login_button.SetDefault()
BoxSizer01.Add( self.login_button, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
self.SetSizer( BoxSizer01 )
self.Layout()
# Connect Events
self.login_button.Bind( wx.EVT_BUTTON, self.on_login )
# Virtual event handlers, overide them in your derived class
def on_login( self, event ):
self.Hide()
self.Parent.nb.Show()
self.Parent.logout_button.Show()
self.Parent.Layout()
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()
I'm working on a fairly large GUI project and am thus using wxpython to build it. I have one frame, multiple frames with their own functions and a main file that imports the gui components as well as other, external functions. I've decided to keep the gui/wxpython code different to better modularize the code.
The main question I have is how to execute the functions in the separate panels and how to make them work from my main python file.
Below is a sample of my wxpython code:
import wx
class MainFrame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.SetSizer( bSizer1 )
self.Layout()
self.panelOne = panel_one(self)
self.panelTwo = panel_two(self)
self.panelTwo.Hide()
self.Centre( wx.BOTH )
def __del__( self ):
pass
class panel_one ( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.TAB_TRAVERSAL )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_button2 = wx.Button( self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer5.Add( self.m_button2, 0, wx.ALL, 5 )
self.SetSizer( bSizer5 )
self.Layout()
# Connect Events
self.m_button2.Bind( wx.EVT_BUTTON, self.changeIntroPanel )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def changeIntroPanel( self, event ):
event.Skip()
class panel_two ( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.TAB_TRAVERSAL )
... some code in here ...
def __del__( self ):
pass
So those are my gui components. Then, in my main file, I import it and run the gui:
import gui
class MainApp(gui.MainFrame):
def __init__(self, parent):
gui.MainFrame.__init__(self, parent)
self.panelOne = Panel1(self)
self.panelTwo = Panel2(self)
self.panelTwo.Hide()
class Panel1(gui.panel_one):
def __init__(self, parent):
gui.panel_one.__init__(self, parent)
def changeIntroPanel( self, event ):
if self.panelOne.IsShown():
self.SetTitle("Panel Two Showing")
self.PanelOne.Hide()
self.PanelTwo.Show()
else:
self.SetTitle("Panel One Showing")
self.PanelOne.Show()
self.PanelTwo.Hide()
self.Layout()
class Panel2(gui.panel_two):
def __init__(self, parent):
gui.panel_two.__init__(self, parent)
def main():
app = wx.App()
window = MainApp(None)
window.Show(True)
app.MainLoop()
if __name__ == '__main__':
main()
As you can tell, the idea is that I want to do all my implementation in my main file. How would I go about defining the functionality of my changeIntroPanel function that was first defined in panel_one? For now, the idea is to hide panelOne and show panelTwo.
I have done something similar before, but only with one frame. In that case, it was trivial since all the functions are in the MainFrame class to begin with. In that case, the code I posted would work perfectly.
Any help is appreciated. Thank you.
def OnInit(self, parent): is used for wx.App, you need def _ _init_ _(self, parent) instead.
About __init__ please check: Using inheritance in python
About difference between __init__ and OnInit please check this link
Edit:
gui.py
import wx
class MainFrame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.SetSizer( bSizer1 )
self.Layout()
# self.panelOne = panel_one(self)
# self.panelTwo = panel_two(self)
# self.panelTwo.Hide()
self.Centre( wx.BOTH )
def __del__( self ):
pass
class panel_one ( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.TAB_TRAVERSAL )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_button2 = wx.Button( self, wx.ID_ANY, u"panel 1 button", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer5.Add( self.m_button2, 0, wx.ALL, 5 )
self.SetSizer( bSizer5 )
self.Layout()
# Connect Events
self.m_button2.Bind( wx.EVT_BUTTON, self.changeIntroPanel )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def changeIntroPanel( self, event ):
event.Skip()
class panel_two ( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.TAB_TRAVERSAL )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_button2 = wx.Button( self, wx.ID_ANY, u"panel 2 button ", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer5.Add( self.m_button2, 0, wx.ALL, 5 )
self.SetSizer( bSizer5 )
self.Layout()
# Connect Events
self.m_button2.Bind( wx.EVT_BUTTON, self.changeIntroPanel )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def changeIntroPanel( self, event ):
event.Skip()
mainapp.py
import wx
import gui
class MainApp(gui.MainFrame):
def __init__(self, parent):
gui.MainFrame.__init__(self, parent)
self.panelOne = Panel1(self)
self.panelTwo = Panel2(self)
self.panelTwo.Hide()
class Panel1(gui.panel_one):
def __init__(self, parent):
gui.panel_one.__init__(self, parent)
self.parent = parent
def changeIntroPanel( self, event ):
if self.IsShown():
self.parent.SetTitle("Panel Two Showing")
self.Hide()
self.parent.panelTwo.Show()
class Panel2(gui.panel_two):
def __init__(self, parent):
gui.panel_two.__init__(self, parent)
self.parent = parent
def changeIntroPanel( self, event ):
if self.IsShown():
self.parent.SetTitle("Panel One Showing")
self.parent.panelOne.Show()
self.Hide()
def main():
app = wx.App()
window = MainApp(None)
window.Show(True)
app.MainLoop()
if __name__ == '__main__':
main()