ltpitt
asked on
WXPython: call a function with no binding or handler
I am building a small python program and I would like it to check for its ini file at startup: if the ini file it's not there then open a file dialog and write a line of it with the folder selected in the dialog.
I am using WXPython and I can't figure how to call a function while the program is running in the simplest possible way...
here's the code:
I am using WXPython and I can't figure how to call a function while the program is running in the simplest possible way...
here's the code:
#----------------------------------------------------------------------
# A very simple wxPython example. Just a wx.Frame, wx.Panel,
# wx.StaticText, wx.Button, and a wx.BoxSizer, but it shows the basic
# structure of any wxPython application.
#----------------------------------------------------------------------
import wx
import os
class MyFrame(wx.Frame):
"""
This is MyFrame. It just shows a few controls on a wxPanel,
and has a simple menu.
"""
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title,
pos=(150, 150), size=(350, 250))
menuBar = wx.MenuBar()
filemenu = wx.Menu()
aboutmenu = wx.Menu()
filemenu.Append(wx.ID_OPEN, "Ch&oisissez le dossier de travail\tCtrl-O", "Choisissez le dossier")
filemenu.Append(wx.ID_EXIT, "&Quitter\tCtrl-Q", "Quitter")
aboutmenu.Append(wx.ID_ABOUT, "Aide de Pic Portier\tF1", "Aide de Pic Portier")
aboutmenu.Append(wx.ID_ABOUT, "A propos de &Pic Portier\tCtrl-P", "A propos de Pic Portier")
self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
self.Bind(wx.EVT_MENU, self.OnFileOpenMenu, id=wx.ID_OPEN)
menuBar.Append(filemenu, "&Fichier")
menuBar.Append(aboutmenu, "&Aide")
self.SetMenuBar(menuBar)
self.CreateStatusBar()
panel = wx.Panel(self)
text = wx.StaticText(panel, -1, "Es tu pret?")
text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
text.SetSize(text.GetBestSize())
funbtn = wx.Button(panel, -1, "Just for fun...")
btn = wx.Button(panel, -1, "Quitter")
self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn)
self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn)
# Use a sizer to layout the controls, stacked vertically and with
# a 10 pixel border around each
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(text, 0, wx.ALL, 10)
sizer.Add(btn, 0, wx.ALL, 10)
sizer.Add(funbtn, 0, wx.ALL, 10)
panel.SetSizer(sizer)
panel.Layout()
# And also use a sizer to manage the size of the panel such
# that it fills the frame
sizer = wx.BoxSizer()
sizer.Add(panel, 1, wx.EXPAND)
self.SetSizer(sizer)
# Check for ini file
self.OnFileOpenMenu(None)
#ConfigFileCheck()
def OnTimeToClose(self, evt):
"""Event handler for the button click."""
print "See ya later!"
self.Close()
def OnFunButton(self, evt):
"""Event handler for the button click."""
print "Having fun yet?"
def OnFileOpenMenu(self, evt):
# Args below are: parent, question, dialog title, default answer
dd = wx.DirDialog(None, "Select directory to open", "~/", 0, (10, 10), wx.Size(400, 300))
# This function returns the button pressed to close the dialog
ret = dd.ShowModal()
# Let's check if user clicked OK or pressed ENTER
if ret == wx.ID_OK:
wx.MessageBox('You selected: %s\n' % dd.GetPath(), 'Info',
wx.OK | wx.ICON_INFORMATION)
# The dialog is not in the screen anymore, but it's still in memory
#for you to access it's values. remove it from there.
dd.Destroy()
return dd.GetPath()
def ConfigFileCheck(self, evt):
from ConfigParser import SafeConfigParser
configfilename = "picportier.ini"
scriptpath = os.path.abspath(os.path.dirname(__file__))
configfile = os.path.join(scriptpath, configfilename)
try:
with open(configfile):
parser = SafeConfigParser()
parser.read(configfile)
pic_portier_working_folder = parser.get('picportier', 'working_folder')
except IOError:
if not os.path.isfile(configfile):
pic_portier_working_folder = self.OnFileOpenMenu(None)
configfilename = "picportier.ini"
scriptpath = os.path.abspath(os.path.dirname(__file__))
configfile = os.path.join(scriptpath, configfilename)
try:
"test"+pic_portier_working_folder
except TypeError:
wx.MessageBox('Missing ini file', 'Info',
wx.OK | wx.ICON_INFORMATION)
sys.exit(0)
f = open(configfile, 'w')
f.write('[picportier]\nworking_folder = '+pic_portier_working_folder+'\\')
wx.MessageBox('Ini file creation successful', 'Info',
wx.OK | wx.ICON_INFORMATION)
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, "Cosmonet - Pic Portier")
self.SetTopWindow(frame)
print "Print statements go to this stdout window by default."
frame.Show(True)
return True
app = MyApp(redirect=True)
app.MainLoop()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER