wxPython StaticText Issue in my Code - python

There's an issue in this bit of wxPython code I have, the StaticText causes everything else to seemingly disappear.
Note: This is my first time using wxPython and I'm very novice at programming in general, so please try to give a clear explanation. Thanks!
import wx
APP_EXIT = 1
pos1 = (150, 200)
class scoutingSet(wx.Frame):
def __init__(self, *args, **kwargs):
super(scoutingSet, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
############################################################
# MENUBARS AND MENUITEMS
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
fileMenu2 = wx.Menu()
openSheet = wx.MenuItem(fileMenu, wx.ID_ANY, 'Open')
openSheet.SetBitmap(wx.Bitmap('open.png'))
fileMenu.AppendItem(openSheet)
fileMenu.AppendSeparator()
saveSheet = wx.MenuItem(fileMenu, wx.ID_ANY, 'Save')
saveSheet.SetBitmap(wx.Bitmap('save.png'))
fileMenu.AppendItem(saveSheet)
fileMenu.AppendSeparator()
quitSheet = wx.MenuItem(fileMenu, APP_EXIT, 'Quit')
quitSheet.SetBitmap(wx.Bitmap('close.png'))
fileMenu.AppendItem(quitSheet)
self.Bind(wx.EVT_MENU, self.OnQuit, id=APP_EXIT)
fileMenu2.Append(100, '&About')
self.Bind(wx.EVT_MENU, self.aboutBox, id=100)
menuBar.Append(fileMenu, 'File')
menuBar.Append(fileMenu2, 'Information')
self.SetMenuBar(menuBar)
############################################################
# BUTTONS AND CONTROL
panel = wx.Panel(self)
closebutton = wx.Button(panel, label = 'Close\nClose', pos = (20, 30))
closebutton.Bind(wx.EVT_BUTTON, self.OnQuit)
############################################################
# STATIC TEXTS
############################################################
# TEXT CONTROL BOXES
wx.TextCtrl(panel, pos = pos1, size = (50, 50))
wx.TextCtrl(panel, pos = (300, 400), size = (50, 50))
############################################################
# SETTINGS
self.stuff(self)
self.Maximize()
self.SetTitle('Scouting Sheet')
self.Centre()
self.Show(True)
############################################################
# Quitting
def OnQuit(self, e):
self.Close()
# Info in
def aboutBox(self, e):
desc = """This is the SOTAbots 2014 scouting sheet for the FRC 2014 game Aerial Assist"""
infoInAbout = wx.AboutDialogInfo()
infoInAbout.SetIcon(wx.Icon('scouting.png', wx.BITMAP_TYPE_PNG))
infoInAbout.SetName('Scouting Sheet')
infoInAbout.SetVersion('1.0')
infoInAbout.SetDescription(desc)
infoInAbout.AddDeveloper('Franklin Lyon\nLucas Grillos')
wx.AboutBox(infoInAbout)
def stuff(self, e):
textpnl = wx.StaticText(self,-1 , label='Watermark', pos=(20, 30))
textpnl.SetForegroundColour('white')
textpnl.SetBackgroundColour('blue')
def main():
ex = wx.App()
scoutingSet(None)
ex.MainLoop()
if __name__ == '__main__':
main()
Note: I put the Static Text in a function, but even inside the InitUI function, the issue persists. It has to do with the StaticText being shown because if I comment out the call everything displays fine.
Thanks in advance.

I think wx.StaticText is put on self (wx.Frame) rather than the panel, although wx.TextCtrl are on the panel. What happens if you put wx.StaticText on the panel?
Also, I think you need a Sizer (ex. wx.BoxSizer) to manage the layout. You can find a tutorial about wx.BoxSizer at http://zetcode.com/wxpython/layout/. In initUI, I will do something like:
txtctrl1 = wx.TextCtrl(panel, pos = pos1, size = (50, 50))
txtctrl2 = wx.TextCtrl(panel, pos = (300, 400), size = (50, 50))
textpnl = wx.StaticText(panel,-1 , label='Watermark', pos=(20, 30))
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.add(txtctrl1, 1, wx.EXPAND | wx.ALL, 5)
vbox.add(txtctrl2, 1, wx.EXPAND | wx.ALL, 5)
vbox.add(textpnl , 1, wx.EXPAND | wx.ALL, 5)
vbox.add(closebutton, 0, wx.EXPAND | wx.ALL, 5)
panel.SetSizer(vbox)
i hope it helps.

You GUI components are not properly laid out. I recommend you to use sizers for a proper layout. BoxSizer are simple to play with. Here is a nice tutorial on Layout management.
Your code will work when you provide the size of the panel. Use this line panel = wx.Panel(self, -1, size=(800,800)) now you shall see all your components! I also changed the position of your static text, because it was being overlapped with the button.
Please note that you should discourage using names like panel in your code. Instead use something like myPanel or panelA etc.
Working code: Tested on Windows 8, wxPython v3.0
import wx
APP_EXIT = 1
pos1 = (150, 200)
class scoutingSet(wx.Frame):
def __init__(self, *args, **kwargs):
super(scoutingSet, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
############################################################
# MENUBARS AND MENUITEMS
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
fileMenu2 = wx.Menu()
openSheet = wx.MenuItem(fileMenu, wx.ID_ANY, 'Open')
openSheet.SetBitmap(wx.Bitmap('open.png'))
fileMenu.AppendItem(openSheet)
fileMenu.AppendSeparator()
saveSheet = wx.MenuItem(fileMenu, wx.ID_ANY, 'Save')
saveSheet.SetBitmap(wx.Bitmap('save.png'))
fileMenu.AppendItem(saveSheet)
fileMenu.AppendSeparator()
quitSheet = wx.MenuItem(fileMenu, APP_EXIT, 'Quit')
quitSheet.SetBitmap(wx.Bitmap('close.png'))
fileMenu.AppendItem(quitSheet)
self.Bind(wx.EVT_MENU, self.OnQuit, id=APP_EXIT)
fileMenu2.Append(100, '&About')
self.Bind(wx.EVT_MENU, self.aboutBox, id=100)
menuBar.Append(fileMenu, 'File')
menuBar.Append(fileMenu2, 'Information')
self.SetMenuBar(menuBar)
############################################################
# BUTTONS AND CONTROL
panel = wx.Panel(self, -1, size=(800,800))
closebutton = wx.Button(panel, label = 'Close\nClose', pos = (20, 30))
closebutton.Bind(wx.EVT_BUTTON, self.OnQuit)
############################################################
# STATIC TEXTS
############################################################
# TEXT CONTROL BOXES
wx.TextCtrl(panel, pos = pos1, size = (50, 50))
wx.TextCtrl(panel, pos = (300, 400), size = (50, 50))
############################################################
# SETTINGS
self.stuff(self)
self.Maximize()
self.SetTitle('Scouting Sheet')
self.Centre()
self.Show(True)
############################################################
# Quitting
def OnQuit(self, e):
self.Close()
# Info in
def aboutBox(self, e):
desc = """This is the SOTAbots 2014 scouting sheet for the FRC 2014 game Aerial Assist"""
infoInAbout = wx.AboutDialogInfo()
infoInAbout.SetIcon(wx.Icon('scouting.png', wx.BITMAP_TYPE_PNG))
infoInAbout.SetName('Scouting Sheet')
infoInAbout.SetVersion('1.0')
infoInAbout.SetDescription(desc)
infoInAbout.AddDeveloper('Franklin Lyon\nLucas Grillos')
wx.AboutBox(infoInAbout)
def stuff(self, e):
textpnl = wx.StaticText(self,-1 , label='Watermark', pos=(100, 100))
textpnl.SetForegroundColour('white')
textpnl.SetBackgroundColour('blue')
def main():
ex = wx.App()
scoutingSet(None)
ex.MainLoop()
if __name__ == '__main__':
main()
I hope this was helpful.

Related

Wxpython panel is cropped with only a small box shown at the top left hand corner

I am using Hide() and Show() from wx to do the "next page" effect by hiding a panel and showing the next one but in the same frame (not very sure if I am doing it correctly though). At certain pages, the panel is just a small cropped version at the top left corner while some other panels can work normally (display the full thing). How do I solve this problem?
I saw something on stackoverflow about child/parent of the panel or frame and tried changing my code but it does not work, not very sure about how to do it correctly.
class MyPanel(wx.Panel):
def __init__(self, parent):
#Constructor
wx.Panel.__init__(self, parent=parent)
#self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
#This is for older versions of wx
self.frame = parent
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Layout()
def OnEraseBackground(self, evt):
#Add background pic
#From ColourDB.py
dc = evt.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("RszHive.jpg")
dc.DrawBitmap(bmp, 0, 0)
class StartPage(wx.Frame):
def __init__(self, current_dt):
#Settings for frame
super().__init__(parent=None, title='Test', size=(850,790))
#setting up main panel (home page)
self.panel = MyPanel(self)
self.current_dt = current_dt
#so that frame will be in the center of the screen
self.Center()
self.vert_sizer = wx.BoxSizer(wx.VERTICAL)
from Database import DataBase, OperatingHours, GetDayTime, GetMenuByDayTime
dDataBase = DataBase("Full_Menu_Database.txt")
dOperatingHours = OperatingHours("Operating Hours.txt")
# Convert to a tuple needed for the functions
tDayTime = GetDayTime(self.get_dt())
# Function to get menu dictionary by date and time
# Will return an empty dictionary if no food/stores are available
self.stores_open = GetMenuByDayTime(dDataBase, dOperatingHours, tDayTime)
if self.stores_open == {}:
self.ophours = wx.StaticText(self.panel, -1, style=wx.ALIGN_CENTER)
self.ophours.SetLabel("Test")
self.ophours_font = wx.Font(19, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
self.ophours.SetFont(self.ophours_font)
self.vert_sizer.Add(self.ophours, 0, wx.ALL | wx.CENTER, 10)
else:
self.store_names, self.stores_ = [], []
for onestorename in self.stores_open.keys():
self.store_names.append(onestorename)
self.stores_.append(self.stores_open[onestorename])
#btn for store1
store_btn1 = wx.Button(self.panel, label= self.store_names[0])
store_btn1.Bind(wx.EVT_BUTTON, self.click_store1)
self.vert_sizer.Add(store_btn1, 0, wx.ALL | wx.CENTER, 5)
#btn for store2 if have
if len(self.store_names) > 1:
store_btn2 = wx.Button(self.panel, label=self.store_names[1])
store_btn2.Bind(wx.EVT_BUTTON, self.click_store2)
self.vert_sizer.Add(store_btn2, 0, wx.ALL | wx.CENTER, 5)
# btn for store3 if have
if len(self.store_names) > 2:
store_btn3 = wx.Button(self.panel, label=self.store_names[2])
store_btn3.Bind(wx.EVT_BUTTON, self.click_store3)
self.vert_sizer.Add(store_btn3, 0, wx.ALL | wx.CENTER, 5)
# btn for store4 if have
if len(self.store_names) > 3:
store_btn4 = wx.Button(self.panel, label=self.store_names[3])
store_btn4.Bind(wx.EVT_BUTTON, self.click_store4)
self.vert_sizer.Add(store_btn4, 0, wx.ALL | wx.CENTER, 5)
# btn for store5 if have
if len(self.store_names) > 4:
store_btn5 = wx.Button(self.panel, label=self.store_names[4])
store_btn5.Bind(wx.EVT_BUTTON, self.click_store5)
self.vert_sizer.Add(store_btn5, 0, wx.ALL | wx.CENTER, 5)
self.SetSizer(self.vert_sizer)
self.Layout()
self.Show()
Picture of what the panel looks like when i run the code
#igor is correct a call to Layout will get the job done.
Here is an example:
Click on the displayed panel to swap to the other one.
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.panel = wx.Panel(self)
self.btn = wx.Button(self.panel, label="Panel 1", size=(250,75))
self.btn.Bind(wx.EVT_BUTTON, self.switch)
vbox1 = wx.BoxSizer(wx.VERTICAL)
vbox1.Add(self.btn)
self.panel.SetSizer(vbox1)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.panel)
self.SetSizer(vbox)
self.Show()
def switch(self, event):
self.parent.Swap()
class MyOtherPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.panel = wx.Panel(self)
self.btn = wx.Button(self.panel, label="Panel 2", size=(175,250))
self.btn.Bind(wx.EVT_BUTTON, self.switch)
vbox1 = wx.BoxSizer(wx.VERTICAL)
vbox1.Add(self.btn)
self.panel.SetSizer(vbox1)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.panel)
self.SetSizer(vbox)
self.Show()
self.panel.Hide()
def switch(self, event):
self.parent.Swap()
class PanelSwitcher(wx.Frame):
def __init__(self):
super().__init__(None)
vbox = wx.BoxSizer(wx.VERTICAL)
self.panel1 = MyPanel(self)
self.panel2 = MyOtherPanel(self)
vbox.Add(self.panel1)
vbox.Add(self.panel2)
self.SetSizer(vbox)
self.Show()
def Swap(self):
if self.panel1.panel.IsShown():
self.panel1.panel.Hide()
self.panel2.panel.Show()
else:
self.panel2.panel.Hide()
self.panel1.panel.Show()
self.Layout()
if __name__ == "__main__":
app = wx.App()
PanelSwitcher()
app.MainLoop()
I also had the problem a very long time and did not know the solution. The sizers did not work (as I expected)
For me, the problem was, that the panel had no (or the incorrect size). The solution was eiter:
panel.Fit()
or
panel.SetSize(x,y)
Another possibility was, to first add the panel into a sizer. And then set them to the frame.
Afterwards put the buttons into the sizer - and add them to the panel.
This also solves the incorrect size of the panel.

wxPython: How to make sizer fill its parent entirely

I have a problem where BoxSizer doesn't fill its parent.
In the above screenshot I mean the sizer containing yellow and purple panels. I want this sizer and the panels to fill the entire panel in Main tab.
The only way I found to accomplish this is to SetMinSize() on the sizer to some big value. I can't set it to panel's actual size because GetSize() on the panel returns very small and definitely not real values.
Here's the relevant code:
import wx
class App(wx.Frame):
"""Main app window wrapping around everything else.
"""
def __init__(self):
super(App, self).__init__(None, title='TSP Visual', size=(1200, 900))
self.init_ui()
self.Centre()
self.Show()
def init_ui(self):
# Menubar
menu_bar = wx.MenuBar()
file_menu = wx.Menu()
exit_mi = file_menu.Append(wx.ID_EXIT, 'Exit', 'Exit application')
menu_bar.Append(file_menu, 'File')
self.SetMenuBar(menu_bar)
# Main layout
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
# Title
title = wx.StaticText(panel, label='No instance loaded')
title_font = wx.Font(wx.FontInfo(18))
title.SetFont(title_font)
title.SetMinSize(title.GetTextExtent(title.Label))
sizer.Add(title, 0, wx.EXPAND | wx.ALL, 10)
# Tabs
notebook = wx.Notebook(panel)
main_tab = MainTab(notebook)
stats_tab = StatsTab(notebook)
notebook.AddPage(main_tab, 'Main')
notebook.AddPage(stats_tab, 'Stats')
sizer.Add(notebook, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, 10)
panel.SetSizerAndFit(sizer)
# Event bindings
self.Bind(wx.EVT_MENU, lambda e: self.Close(), exit_mi)
class MainTab(wx.Panel):
"""Main tab of the app, solver controls and tsp view.
"""
def __init__(self, parent):
super(MainTab, self).__init__(parent)
self.init_ui()
def init_ui(self):
# Panel sizer
sizer = wx.BoxSizer(wx.HORIZONTAL)
# Solver controls and TSP view
controls = SolverControls(self)
controls.SetBackgroundColour('yellow')
tsp_view = TSPView(self)
tsp_view.SetBackgroundColour('purple')
sizer.Add(controls, 1, wx.EXPAND)
sizer.Add(tsp_view, 1, wx.EXPAND)
self.SetSizerAndFit(sizer)
class StatsTab(wx.Panel):
"""Second tab, graphs and statistics
"""
def __init__(self, parent):
super(StatsTab, self).__init__(parent)
self.init_ui()
def init_ui(self):
pass
class TSPView(wx.Panel):
def __init__(self, parent):
super(TSPView, self).__init__(parent)
self.init_ui()
def init_ui(self):
self.SetBackgroundColour('white')
class SolverControls(wx.Panel):
def __init__(self, parent):
super(SolverControls, self).__init__(parent)
self.init_ui()
def init_ui(self):
sizer = wx.GridBagSizer()
text = wx.StaticText(self, label='Test text')
sizer.Add(text, (0, 0), (1, 1), wx.ALL, 5)
button1 = wx.Button(self, label='Button 1')
sizer.Add(button1, (1, 0), (1, 1), wx.ALL, 5)
button2 = wx.Button(self, label='Button 2')
sizer.Add(button2, (2, 0), (1, 1), wx.ALL, 5)
self.SetSizer(sizer)
if __name__ == '__main__':
app = wx.App()
App()
app.MainLoop()
EDIT:
I've changed my code sample so it's self contained and runnable.
You shouldn't call SetSizerAndFit() in MainTab.init_ui: by calling this function, you change the size of MainTab to be just big enough to fit its contents. The yellow and purple panels should still get resized properly once you resize the parent window (if they don't, it means that there is another problem somewhere else, which I've missed), but to make it work from the beginning, just use SetSizer() instead.
So it turned out the problem was somehow related to i3wm window manager. Since manually resizing the window fixes the problem I came up with a solution where I SetSize() of the window after Show()ing it. My __init__() method of App looks like this:
def __init__(self):
super(App, self).__init__(None, title='TSP Visual')
self.init_ui()
self.Show()
self.SetSize(1200, 900)
self.Centre()

How to show a text from wx module?

Yesterday found out that my router can be controlled by telnet, and today I was looking for some qt4,pygtk or wx to store all the router telnet commands in a gui. Less than 15 minutes ago I found this website - zetcode(dot)com/wxpython/advanced/ , which got the right information that I need. Unfortunatelly I don't understand how to include text in wx modules, because I am using it for a first time. Can you tell me how to assign text to the left window, because once I start the module it shows me a grey screen with a buttons in it's menu. The grey area that is the most left must contain around 10 telnet commands, where the "help" contains all of the commands saved in a html file.
import wx
import wx.html as html
class HelpWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(570, 400))
toolbar = self.CreateToolBar()
toolbar.AddLabelTool(1, 'Exit', wx.Bitmap('icons/exit.png'))
toolbar.AddLabelTool(2, 'Help', wx.Bitmap('icons/help.png'))
toolbar.Realize()
self.splitter = wx.SplitterWindow(self, -1)
self.panelLeft = wx.Panel(self.splitter, -1, style=wx.BORDER_SUNKEN)
self.panelRight = wx.Panel(self.splitter, -1)
vbox2 = wx.BoxSizer(wx.VERTICAL)
header = wx.Panel(self.panelRight, -1, size=(-1, 20))
header.SetBackgroundColour('#6f6a59')
header.SetForegroundColour('WHITE')
hbox = wx.BoxSizer(wx.HORIZONTAL)
st = wx.StaticText(header, -1, 'Help', (5, 5))
font = st.GetFont()
font.SetPointSize(9)
st.SetFont(font)
hbox.Add(st, 1, wx.TOP | wx.BOTTOM | wx.LEFT, 5)
close = wx.BitmapButton(header, -1, wx.Bitmap('icons/fileclose.png', wx.BITMAP_TYPE_PNG),
style=wx.NO_BORDER)
close.SetBackgroundColour('#6f6a59')
hbox.Add(close, 0)
header.SetSizer(hbox)
vbox2.Add(header, 0, wx.EXPAND)
help = html.HtmlWindow(self.panelRight, -1, style=wx.NO_BORDER)
help.LoadPage('wx.html')
vbox2.Add(help, 1, wx.EXPAND)
self.panelRight.SetSizer(vbox2)
self.panelLeft.SetFocus()
self.splitter.SplitVertically(self.panelLeft, self.panelRight)
self.splitter.Unsplit()
self.Bind(wx.EVT_BUTTON, self.CloseHelp, id=close.GetId())
self.Bind(wx.EVT_TOOL, self.OnClose, id=1)
self.Bind(wx.EVT_TOOL, self.OnHelp, id=2)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
self.CreateStatusBar()
self.Centre()
self.Show(True)
def OnClose(self, event):
self.Close()
def OnHelp(self, event):
self.splitter.SplitVertically(self.panelLeft, self.panelRight)
self.panelLeft.SetFocus()
def CloseHelp(self, event):
self.splitter.Unsplit()
self.panelLeft.SetFocus()
def OnKeyPressed(self, event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_F1:
self.splitter.SplitVertically(self.panelLeft, self.panelRight)
self.panelLeft.SetFocus()
app = wx.App()
HelpWindow(None, -1, 'HelpWindow')
app.MainLoop()
Found it...
wx.StaticText(self.panelLeft, -1, 'thetextgoeshere', (15, 5))

How to write a "&" in button text in wxpython

This looks simple but I have not found any documentation. Tried with &&, which does not work. Want a button like this:
Button1=wx.Button(self, 5, "abc&abc", (130, 230))
Works for me with this:
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(200,-1))
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.buttons = [
wx.Button(self,-1,"Button &One"),
wx.Button(self,-1,"Button &&Two"),
]
for btn in self.buttons:
self.sizer.Add(btn,1,wx.EXPAND)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
self.Show()
app = wx.App(False)
frame = MainWindow(None,"Hello Ampersand")
app.MainLoop()
Your code working MattH but my problem is actually i was showing button in click event of menubar at that time && not works..check out below code...what should be done in such a case
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(699, 570))
fileMenu= wx.Menu()
folder=wx.MenuItem(fileMenu, 1, "&Start","Click here to start..")
fileMenu.AppendItem(folder)
about = wx.MenuItem(fileMenu, 2, '&About',"Test")
fileMenu.AppendItem(about)
quit = wx.MenuItem(fileMenu, 3, '&Quit',"Terminate the program")
fileMenu.AppendItem(quit)
menuBar = wx.MenuBar()
menuBar.Append(fileMenu,"&File")
self.Bind(wx.EVT_MENU, self.ShowButton, folder)
self.SetMenuBar(menuBar)
pndSummaryButton = wx.Button(self, 3, "Button &&Two", (130, 146))
pndSummaryButton.name="pndSummary"
self.printArea2 = wx.TextCtrl(self,pos = (290, 146), size = (255, 25),style = wx.TE_READONLY)
pndSummaryButton.SetFont(wx.Font(11, wx.SWISS, wx.NORMAL,wx.LIGHT))
self.printArea2.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL,wx.LIGHT))
pndSummaryButton.SetBackgroundColour(wx.Colour(153,0,0))
pndSummaryButton.SetForegroundColour(wx.Colour(255,255,255))
pndSummaryButton.SetSize(pndSummaryButton.GetBestSize())
self.Show()
def ShowButton(self,event):
pndSummaryButton = wx.Button(self, 3, "Button &&Two", (130, 176))
pndSummaryButton.name="pndSummary1"
self.printArea2 = wx.TextCtrl(self,pos = (290, 176), size = (255, 25),style = wx.TE_READONLY)
pndSummaryButton.SetFont(wx.Font(11, wx.SWISS, wx.NORMAL,wx.LIGHT))
self.printArea2.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL,wx.LIGHT))
pndSummaryButton.SetBackgroundColour(wx.Colour(153,0,0))
pndSummaryButton.SetForegroundColour(wx.Colour(255,255,255))
pndSummaryButton.SetSize(pndSummaryButton.GetBestSize())
app = wx.App(False)
frame = MainWindow(None,"Hello Ampersand")
app.MainLoop()

wxPython display a login box

I'm trying to build a small application in wxPython (absolute beginner) in which I display a login box before showing the content. I created a frame, inside the frame a panel with a flexigrid to put the login form inside but it doesn't show. If I launch the application the login form is invisible. If I resize the application the login box shows. Any idea why? Here's my code so far:
import wx
class AP_App(wx.App):
def OnInit(self):
frame = AP_MainFrame("Test application", (0, 0), (650, 350))
frame.Show()
self.SetTopWindow(frame)
loginPanel = AP_LoginPanel(frame)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
return True
def OnCloseWindow(self, event):
self.Destroy()
class AP_MainFrame(wx.Frame):
def __init__(self, title, pos, size):
wx.Frame.__init__(self, None, -1, title, pos, size)
self.CreateStatusBar()
class AP_LoginPanel(wx.Panel):
def __init__(self, frame):
self.panel = wx.Panel(frame)
self.frame = frame
self.frame.SetStatusText("Authentification required!")
self.showLoginBox()
def showLoginBox(self): #Create the sizer
sizer = wx.FlexGridSizer(rows = 3, cols = 2, hgap = 5, vgap = 15)
# Username
self.txt_Username = wx.TextCtrl(self.panel, 1, size = (150, -1))
lbl_Username = wx.StaticText(self.panel, -1, "Username:")
sizer.Add(lbl_Username,0, wx.LEFT|wx.TOP| wx.RIGHT, 50)
sizer.Add(self.txt_Username,0, wx.TOP| wx.RIGHT, 50)
# Password
self.txt_Password = wx.TextCtrl(self.panel, 1, size=(150, -1), style=wx.TE_PASSWORD)
lbl_Password = wx.StaticText(self.panel, -1, "Password:")
sizer.Add(lbl_Password,0, wx.LEFT|wx.RIGHT, 50)
sizer.Add(self.txt_Password,0, wx.RIGHT, 50)
# Submit button
btn_Process = wx.Button(self.panel, -1, "&Login")
self.panel.Bind(wx.EVT_BUTTON, self.OnSubmit, btn_Process)
sizer.Add(btn_Process,0, wx.LEFT, 50)
self.panel.SetSizer(sizer)
def OnSubmit(self, event):
UserText = self.txt_Username.GetValue()
PasswordText = self.txt_Password.GetValue()
if __name__ == '__main__':
app = AP_App()
app.MainLoop()
I just discovered I'm calling frame.Show() too soon. :)

Categories

Resources