how to get a variable out of wxpython file open - python

I am trying to get a variable out of wxPython file open dialog. I have 2 button which gets the path to 2 files with .GetPath() ?
this is the code I have so far
'def onclk1(event):
with wx.FileDialog(panel, "OPEN EMG FILE", wildcard="TXT Files(*.txt)|*.txt",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as EmgFile:
if EmgFile.ShowModal() == wx.ID_CANCEL:
return "cancelled"
emg = EmgFile.GetPath()
e1.Bind(wx.EVT_BUTTON, onclk1)
Now I need to pass the path outside def to another variable.
Thanks in advance

In future it might be better to provide a minimal working example of your code.
Don't forget that a GUI is event driven and you must use the event to assign the return value of a dialog to the variable you want.
You've not said what you want to do with the file path, but this code shows how to assign it to a label.
"""Main Frame module for basic wxPython App."""
import wx
class MainFrame(wx.Frame):
"""Create MainFrame class."""
def __init__(self, *args, **kwargs):
super().__init__(None, *args, **kwargs)
self.size = (400, 1000)
self.Title = 'wx App'
self.Bind(wx.EVT_CLOSE, self.on_quit_click)
self.panel = MainPanel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.panel)
self.SetSizer(sizer)
self.Center()
self.Show()
def onclk1(self, event):
with wx.FileDialog(self.panel, "OPEN EMG FILE", wildcard="TXT Files(*.txt)|*.txt",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as EmgFile:
if EmgFile.ShowModal() == wx.ID_CANCEL:
return "cancelled"
emg = EmgFile.GetPath()
self.panel.lbl_file1.SetLabel(emg)
def on_quit_click(self, event):
"""Handle close event."""
del event
wx.CallAfter(self.Destroy)
class MainPanel(wx.Panel):
"""Create a panel class to contain screen widgets."""
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
e1 = wx.Button(self, label='File1')
e1.Bind(wx.EVT_BUTTON, parent.onclk1)
self.lbl_file1 = wx.StaticText(self, label=' '*100)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(e1)
sizer.Add(self.lbl_file1)
self.SetSizer(sizer)
if __name__ == '__main__':
wx_app = wx.App()
MainFrame()
wx_app.MainLoop()

Just to provide another option, there is also a wx.FilePickerCtrl
import wx
import os
wildcard = "All files (*.*)|*.*"
class MainWindow(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='File Selector')
self.currentDirectory = os.getcwd()
self.panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
ie_box = wx.StaticBox(self.panel, -1, 'Please select the input file')
ie_sizer = wx.StaticBoxSizer(ie_box, wx.VERTICAL)
fl_box = wx.BoxSizer(wx.HORIZONTAL)
self.fl_ctrl = wx.FilePickerCtrl(self.panel, message="Choose a file")
fl_box.Add(self.fl_ctrl, 1, wx.ALL | wx.CENTER | wx.EXPAND, 5)
ie_sizer.Add(fl_box, 1, wx.ALL | wx.CENTER | wx.EXPAND, 10)
self.fl_ctrl.Bind(wx.EVT_FILEPICKER_CHANGED, self.on_open_file)
vbox.Add(ie_sizer, 0, wx.ALL | wx.CENTER | wx.EXPAND, 5)
self.panel.SetSizer(vbox)
self.Center()
self.panel.Fit()
self.Show()
def on_open_file(self, event):
self.fl_ctrl.GetPath()
if __name__ == '__main__':
app = wx.App()
frame = MainWindow()
app.MainLoop()

Related

Deactivate wxpython FilePickerCtrl

Is there a way to activate/deactivate the editability of an wxpython widget and especially the FilePickerCtrl widget?
In my case, I am creating a desktop app from which the user will be able to start and stop a server. Before starting the server the user can specify a variable through the FilePickerCtrl widget and this variable will be used from the server. I want to make that FilePickerCtrl editable while the server is stopped and un-editable while the server is running.
So far I have tried the wxpython Validators without success. Some reproducable code:
configuration.py (you need to edit the excel path)
global SERVER_CONF
SERVER_CONF = {
'XE_EXCEL_PATH': '/absolute/path/to/excel.xlsx'
}
gui_main.py
import wx
from server_tab import Server
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="")
# Create a panel and notebook (tabs holder)
p = wx.Panel(self)
nb = wx.Notebook(p)
# Create the tab windows
tab1 = Server(nb)
# Add the windows to tabs and name them.
nb.AddPage(tab1, tab1.name)
# Set noteboook in a sizer to create the layout
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()
server.py
from flask import request
from flask import Flask
from configuration import SERVER_CONF
app = Flask(__name__)
#app.route('/', methods=['POST'])
def read_html_source():
global SERVER_CONF
print('HEREEE',SERVER_CONF['XE_EXCEL_PATH'])
return ('', 200)
#app.route("/shutdown", methods=['GET'])
def shutdown():
shutdown_func = request.environ.get('werkzeug.server.shutdown')
if shutdown_func is None:
raise RuntimeError('Not running werkzeug')
shutdown_func()
return ("Shutting down...", 200)
def main():
app.run(debug=False, port=5001)
if __name__ == "__main__":
main()
server_tab.py
import os
import requests
import threading
import wx
import server
from configuration import SERVER_CONF
# Define the tab content as classes:
class Server(wx.Panel):
name = "Server"
def __init__(self, parent):
global SERVER_CONF
self.is_server_active = False
wx.Panel.__init__(self, parent)
generic_sizer = wx.BoxSizer(wx.VERTICAL)
title = wx.StaticText(self, 0, "The APE API server")
generic_sizer.Add(title, 0, wx.ALL | wx.EXPAND, 5)
file_sizer = wx.BoxSizer(wx.HORIZONTAL)
excel_label = wx.StaticText(self, 0, "Excel ")
file_sizer.Add(excel_label, 0, wx.ALL | wx.EXPAND, 5)
self. excel_browser = wx.FilePickerCtrl(
self,
wildcard='*.xlsx',
path=SERVER_CONF['XE_EXCEL_PATH'],
validator=ServerValidator(self.is_server_active)
)
file_sizer.Add(self.excel_browser, 0, wx.ALL , 5)
generic_sizer.Add(file_sizer, 0, wx.ALL , 5)
server_btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
start_server_btn = wx.Button(self, label='Run Server')
start_server_btn.Bind(wx.EVT_BUTTON, self.start_server)
server_btn_sizer.Add(start_server_btn, 0, wx.ALL | wx.EXPAND, 5)
stop_server_btn = wx.Button(self, label='Stop Server')
stop_server_btn.Bind(wx.EVT_BUTTON, self.stop_server)
server_btn_sizer.Add(stop_server_btn, 0, wx.ALL | wx.EXPAND, 5)
generic_sizer.Add(server_btn_sizer, 0, wx.ALL | wx.EXPAND, 5)
parent.SetSizer(generic_sizer)
def start_server(self, entry):
SERVER_CONF['XE_EXCEL_PATH'] = self.excel_browser.GetPath()
self.is_server_active = True
self.thread = threading.Thread(target=server.main)
self.thread.start()
def stop_server(self, entry):
resp = requests.get('http://localhost:5001/shutdown')
self.thread.join()
print('Server was stoped')
class ServerValidator(wx.PyValidator):
def __init__(self, is_server_active):
wx.Validator.__init__(self)
self.is_server_active = is_server_active
def Clone(self):
'''Required Validator method'''
return ServerValidator(self.is_server_active)
def Validate(self, win):
window = self.GetWindow()
print("Is running?", self.is_server_active)
return not self.is_server_active
Run with python gui_main.py and test with curl http://localhost:5001 -d POST. In case you forget to stop the server from the GUI run curl http://localhost:5001/shutdown
The simplest way is to Enable and Disable the control. This works generally on wx controls
import wx
class MainFrame(wx.Frame):
"""Create MainFrame class."""
def __init__(self, *args, **kwargs):
super().__init__(None, *args, **kwargs)
self.size = (400, 1000)
self.title_text = 'Wx App'
self.panel = MainPanel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.panel)
self.SetSizer(sizer)
self.Size = (600, 200)
self.Center()
self.Show()
def on_disable(self, event):
self.panel.excel_browser.Disable()
def on_enable(self, event):
self.panel.excel_browser.Enable()
class MainPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.excel_browser = wx.FilePickerCtrl(self)
btn_disable = wx.Button(self, id=wx.ID_CANCEL)
btn_disable.Bind(wx.EVT_BUTTON, parent.on_disable)
btn_enable = wx.Button(self, id=wx.ID_OK)
btn_enable.Bind(wx.EVT_BUTTON, parent.on_enable)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.excel_browser)
sizer.Add(btn_disable)
sizer.Add(btn_enable)
self.SetSizer(sizer)
if __name__ == '__main__':
wx_app = wx.App()
MainFrame()
wx_app.MainLoop()
Just to prove Psionman's point about Enabling and Disabling wx widgets and that you can also test if a widget IsEnabled or not, which can be a useful tool, especially for toggling a control.
Here, the FilePickerCtrl and the Lock button are toggled between Enabled and Disabled and the button label is toggled, based on whether the Picker is Enabled or not.
import wx
class MainWindow(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Lockable File Selector', size=(400, 200))
self.panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
self.s_box = wx.StaticBox(self.panel, -1, 'Please select the input file')
s_sizer = wx.StaticBoxSizer(self.s_box, wx.VERTICAL)
fp_box = wx.BoxSizer(wx.HORIZONTAL)
self.fp_ctrl = wx.FilePickerCtrl(self.panel, wx.ID_ANY, message="Choose a file")
self.fp_lock = wx.Button(self.panel, wx.ID_ANY, label="Lock")
fp_box.Add(self.fp_ctrl, 1, wx.ALL | wx.CENTER | wx.EXPAND, 5)
fp_box.Add(self.fp_lock, 0, wx.ALL | wx.CENTER, 5)
s_sizer.Add(fp_box, 1, wx.ALL | wx.CENTER | wx.EXPAND, 10)
vbox.Add(s_sizer, 0, wx.ALL | wx.CENTER | wx.EXPAND, 5)
self.panel.SetSizer(vbox)
self.fp_ctrl.Bind(wx.EVT_FILEPICKER_CHANGED, self.on_open_file)
self.fp_lock.Bind(wx.EVT_BUTTON, self.on_lock)
self.panel.Fit()
self.fp_lock.Enable(False)
self.Show()
def on_open_file(self, event):
path = self.fp_ctrl.GetPath()
use_path, x = path.rsplit('/', 1)
self.s_box.SetLabel(use_path)
if path:
self.fp_lock.Enable(True)
def on_lock(self, event):
if self.fp_ctrl.IsEnabled():
self.fp_ctrl.Enable(False)
self.fp_lock.SetLabel('Unlock')
else:
self.fp_ctrl.Enable(True)
self.fp_lock.SetLabel('Lock')
if __name__ == '__main__':
app = wx.App()
frame = MainWindow()
app.MainLoop()

Start a hidden panel with wxpython

I want to start an app with several panels hidden from the user and show them per request when interacting with the menu.
So far the `budget_panel' that I want to be initially hidden is always on display.
My code is as follows (likely it can be dramatically improved as it is my first time using wxwidgets and OOP in Python).
#!/usr/bin/python3
import sqlite3
import wx
class DB():
def __init__(self):
self.db = sqlite3.connect(':memory:')
self.cursor = self.db.cursor()
self.cursor.execute('CREATE TABLE invoices (id INT PRIMARY KEY, client TEXT)')
self.cursor.execute('INSERT INTO invoices (client) VALUES ("Empresa A")')
self.db.commit()
def myget(self, id):
return self.cursor.execute('SELECT * FROM {}'.format(id)).fetchall()
class BudgetPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.SetSizer(sizer)
self.choices = ["Empresa A", "Empresa B"]
self.choice = wx.Choice(parent, id=wx.ID_ANY, choices = self.choices)
sizer.Add(self.choice)
class EmptyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent = parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
text = wx.StaticText(self, -1, 'Text', style = wx.ALIGN_CENTER | wx.TE_READONLY)
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"Empresa",
size=(800,600))
self.db = DB()
self.menu()
self.empty_panel = EmptyPanel(self)
self.budget_panel = BudgetPanel(self)
self.panels = {1000 : self.empty_panel, 2001 : self.budget_panel}
for key in self.panels.keys():
if key == 1000:
self.panels[key].Show()
else:
self.panels[key].Hide()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.empty_panel, 1, wx.EXPAND)
self.sizer.Add(self.budget_panel, 1, wx.EXPAND)
self.SetSizer(self.sizer)
def menu(self):
menubar = wx.MenuBar()
main_menu = wx.Menu()
budget_menu = main_menu.Append(2001, 'Presupuestos')
self.Bind(wx.EVT_MENU, self.change_panel, budget_menu)
menubar.Append(main_menu, '&GestiĆ³n')
self.SetMenuBar(menubar)
def change_panel(self, event):
for key in self.panels.keys():
if event.GetId() == key:
self.panels[key].Show()
else:
self.panels[key].Hide()
self.Layout()
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
'''

wxpython assign variable in an event handler

I would like to assign the value of a menu click to a class variable so I can use that value throughout multiple classes. Any help would be greatly appreciated!
import wx
class TrackPanel (wx.Panel):
""""""
def __init__(self, parent, *args, **kwargs):
"""Constructor"""
wx.Panel.__init__(self, parent, *args, **kwargs)
mainSizer =wx.BoxSizer(wx.VERTICAL)
track = MasterPage.track_selected # This is where i would like access the variable
title = wx.StaticText(self, wx.ID_ANY, label = track)
mainSizer.Add(title, 0,wx.ALL | wx.EXPAND | wx.CENTER)
self.SetSizerAndFit(mainSizer)
self.Layout()
class MasterPage (wx.Frame):
track_selected = ' '
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
menubar = wx.MenuBar()
tracksopt = wx.Menu()
track_command = wx.MenuItem(tracksopt, wx.ID_ANY, 'Atlanta')
tracksopt.Append(track_command)
self.Bind(wx.EVT_MENU, self.change_track, track_command)
track_command2 = wx.MenuItem(tracksopt, wx.ID_ANY, 'Texas')
tracksopt.Append(track_command2)
self.Bind(wx.EVT_MENU, self.change_track, track_command2)
menubar.Append(tracksopt, '&Tracks')
self.SetMenuBar(menubar)
def change_track (self, event):
"""Changes the tack_selected variable based on the menu item picked"""
id_selected = event.GetId()
obj = event.GetEventObject()
track_id= obj.GetLabel(id_selected)
MasterPage.track_selected = track_id
You must create a link to the master in the tracker in order for the tracker to access values in the master.
import wx
class Track (wx.Frame):
def __init__(self, link):
wx.Frame.__init__(self, None)
self.link = link
self.tc = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.bt = wx.Button(self, label="tracker (push to read master)")
self.bt.Bind(wx.EVT_BUTTON, self.on_read_master)
self.Title = 'Tracker'
sz_1 = wx.BoxSizer(wx.HORIZONTAL)
sz_2 = wx.BoxSizer(wx.VERTICAL)
sz_2.Add(self.tc, 1, wx.EXPAND, 0)
sz_2.Add(self.bt, 0, wx.EXPAND, 0)
sz_1.Add(sz_2, 1, wx.EXPAND, 0)
self.SetSizer(sz_1)
self.Layout()
self.SetSize((250, 100))
def on_read_master(self, evt):
self.tc.SetValue(self.link.track_selected) # here you access the variable)
class Master(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.track_selected = '0'
self.tc = wx.TextCtrl(self, wx.ID_ANY, "", style=wx.TE_MULTILINE)
self.bt = wx.Button(self, wx.ID_ANY, "master (push to send)")
self.bt.Bind(wx.EVT_BUTTON, self.change_track)
self.Title = 'Master'
self.tc.SetValue('enter value to be read by the tracker')
sz_1 = wx.BoxSizer(wx.HORIZONTAL)
sz_2 = wx.BoxSizer(wx.VERTICAL)
sz_2.Add(self.tc, 1, wx.EXPAND, 0)
sz_2.Add(self.bt, 0, wx.EXPAND, 0)
sz_1.Add(sz_2, 1, wx.EXPAND, 0)
self.SetSizer(sz_1)
self.Layout()
self.SetSize((250, 100))
def change_track (self, evt):
"""Changes variable based on text entered"""
self.track_selected = self.tc.GetValue()
if __name__ == "__main__":
app = wx.App(0)
master = Master()
track = Track(master)
master.Show()
track.Show()
app.MainLoop()
This is the fast and dirty method I generally use. If you want something more sophisticated, there is a dedicated library to pass messages between frames: pubsub. A good tutorial of how to use it can be found in the Mike Driscoll blog

wx.Frame error when calling one script from another

The following is a bit of copied code from another question, it works fine as a standalone app, but the pop-up right-click menu references frame_1 which is not available if "if name == 'main':" is false, as it is when the program is called by another. What should this reference be changed to?
Thanks.
import wx
import sys
sys.path.append("..")
from ObjectListView import ObjectListView, ColumnDefn
### 2. Launcher creates wxMenu. ###
menu_titles = [ "Open",
"Properties",
"Rename",
"Delete" ]
menu_title_by_id = {}
for title in menu_titles:
menu_title_by_id[ wx.NewId() ] = title
class Track(object):
"""
Simple minded object that represents a song in a music library
"""
def __init__(self, title, artist, album):
self.title = title
self.artist = artist
self.album = album
def GetTracks():
"""
Return a collection of tracks
"""
return [
Track("Sweet Lullaby", "Deep Forest", "Deep Forest"),
Track("Losing My Religion", "U2", "Out of Time"),
Track("En el Pais de la Libertad", "Leon Gieco", "Leon Gieco"),
]
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.Init()
def Init(self):
self.InitModel()
self.InitWidgets()
self.InitObjectListView()
def InitModel(self):
self.songs = GetTracks()
def InitWidgets(self):
panel = wx.Panel(self, -1)
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(panel, 1, wx.ALL | wx.EXPAND)
self.SetSizer(sizer_1)
self.myOlv = ObjectListView(panel, -1, style=wx.LC_REPORT | wx.SUNKEN_BORDER)
sizer_2 = wx.BoxSizer(wx.VERTICAL)
sizer_2.Add(self.myOlv, 1, wx.ALL | wx.EXPAND, 4)
panel.SetSizer(sizer_2)
self.Layout()
def InitObjectListView(self):
self.myOlv.SetColumns([
ColumnDefn("Title", "left", 120, "title"),
ColumnDefn("Artist", "left", 100, "artist"),
ColumnDefn("Album", "left", 100, "album")
])
self.myOlv.SetObjects(self.songs)
self.myOlv.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.RightClick)
def RightClick(self, event):
# record what was clicked
self.list_item_clicked = self.myOlv.GetSelectedObject()
menu = wx.Menu()
menu.Bind(wx.EVT_MENU, self.MenuSelectionCb)
for (id_, title) in menu_title_by_id.items():
### 3. Launcher packs menu with Append. ###
menu.Append(id_, title)
### 5. Launcher displays menu with call to PopupMenu, invoked on the source component, passing event's GetPoint. ###
# self.frame.PopupMenu( menu, event.GetPoint() )
frame_1.PopupMenu(menu, event.GetPoint())
menu.Destroy() # destroy to avoid mem leak
def MenuSelectionCb(self, event):
# do something
operation = menu_title_by_id[ event.GetId() ]
target = self.list_item_clicked.title
print 'Perform "%(operation)s" on "%(target)s."' % vars()
class MyPopupMenu(wx.Menu):
def __init__(self, parent):
super(MyPopupMenu, self).__init__()
self.parent = parent
mmi = wx.MenuItem(self, wx.NewId(), 'Minimize')
self.AppendItem(mmi)
self.Bind(wx.EVT_MENU, self.OnMinimize, mmi)
cmi = wx.MenuItem(self, wx.NewId(), 'Close')
self.AppendItem(cmi)
self.Bind(wx.EVT_MENU, self.OnClose, cmi)
def OnMinimize(self, e):
self.parent.Iconize()
def OnClose(self, e):
self.parent.Close()
if __name__ == '__main__':
app = wx.App(True)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "ObjectListView Track Test")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()
If you are going to import your code into another module, then you will just need to instantiate that frame in your new main application's code. Let's save your code as olv_tracks.py. Now import it like I do in the following code:
import wx
import olv_tracks
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Main App')
panel = wx.Panel(self)
self.Show()
# show other frame
frame = olv_tracks.MyFrame(None, -1, "ObjectListView Track Test")
frame.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
Now you just instantiate the frame the same way you did in your code.
Also, you need to change the reference to frame_1 in your RightClick method to self. So instead of frame_1.PopupMenu(menu, event.GetPoint()), it should be self.PopupMenu(menu, event.GetPoint())

Can't make buttons to appear (wxpython) on ubuntu

try:
import wx
except ImportError:
print 'Module not found'
class Frame(wx.Frame):
def __init__(parent,id ):
wx.Frame.__init__(self,parent,id,)
panel = wx.Panel(self)
button = wx.Button(panel,label = 'close',size = (50,50))
self.Bind(wx.EVT_BUTTON,self.OnCloseMe,button)
self.Bind(wx.EVT_CLOSE,self.OnCloseWindow)
def OnCloseMe(self,event):
self.Close(True)
def OncloseWindow(self,event):
self.Destroy()
if __name__ == '__main__':
app = wx.App()
frame = wx.Frame(parent = None, id =-1,title = 'Widget',size = (300,100))
frame.Show()
app.MainLoop()
Hi guys ,the code above is just to create a button in wxpython.but everytime i run the code ,only the Frame appears,no buttons or whatsoever inside,just blank.i tried to reinstall the wxpython module but no luck.
My second question is whenever i try to initialize say, title = 'widget', size= (300,100) in the frame constructor like wx.Frame.init(self,parent,id,title ='widget',size = (300,100) it does not work, i have to do it through this line:
frame = wx.Frame(parent = None, id =-1,title = 'Widget',size = (300,100))
why is that so.thaks
Here is a working code just add your events to it. Also consider using wxGlade to design graphically your gui instead of writing it:
import wx
import gettext
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, wx.ID_ANY)
self.button_1 = wx.Button(self.panel_1, wx.ID_ANY, _("button_1"))
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle(_("frame_1"))
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2.Add(self.button_1, 0, 0, 0)
self.panel_1.SetSizer(sizer_2)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
if __name__ == "__main__":
gettext.install("app")
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, wx.ID_ANY, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()

Categories