I can't figure out what I'm doing wrong. I just made the jump from Tkinter to wxPython and I'm trying to figure out BoxSizers. I'd look this question up, but I don't even know what to look up. This panel is filling the space of a Frame, it's supposed to show a line of text with a progressbar underneath it and that's all supposed to take up the bottom 1/5 of the panel or so, centered horizontally (eventually I'm going to add a background image behind it). But what happens is I only see the text and only about 40% down from the top, aligned to the left edge of the window. Here's the code:
class KhPanel(wx.Panel):
def __init__(self, parent, configSet, selectWindow):
wx.Panel.__init__(self, parent=parent)
self.frame = parent
self.configSet = configSet
whichWindow = getattr(self, selectWindow)
whichWindow()
def configWindow(self):
gaugeWidth = (1/5)*self.configSet["width"]
gaugeHeight = (1/10)*self.configSet["height"]
gaugeMax = 100
topBuffer = (8/10)*self.configSet["height"]
itemSep = (1/16)*self.configSet["height"]
vSizer = wx.BoxSizer(wx.VERTICAL)
textSizer = wx.BoxSizer(wx.HORIZONTAL)
progressSizer = wx.BoxSizer(wx.HORIZONTAL)
configText = wx.StaticText(self, label="STUFF", style=wx.ALIGN_CENTER)
configProgressBar = wx.Gauge(self, range=gaugeMax, size=(gaugeWidth, gaugeHeight))
textSizer.Add(configText, 1, wx.ALIGN_CENTER, 0)
progressSizer.Add(configProgressBar, 1, wx.ALIGN_CENTER, 1)
vSizer.Add(textSizer, 1, wx.TOP, topBuffer)
vSizer.Add(progressSizer, 1, wx.TOP, itemSep)
self.SetSizer(vSizer)
vSizer.Fit(self)
return
If you need the info, configSet.width and height are the width and height of the parent window (currently 340 x 270). And selectWindow, in this case, is "configWindow"
Running this code, the gaugeWidth and gaugeHeight are both getting set to zero, which is why the progressbar is not showing. This is due to the fact that you are doing integer math here, so 1 divided by 5 is 0. Same with 1/10. Just change those lines to:
gaugeWidth = (1/5.0)*self.configSet["width"]
gaugeHeight = (1/10.0)*self.configSet["height"]
Then the gauge will appear. Here's some fully runnable code, slightly modified from your unrunnable original:
import wx
class KhPanel(wx.Panel):
def __init__(self, parent, configSet):
wx.Panel.__init__(self, parent=parent)
self.frame = parent
self.configSet = configSet
self.configWindow()
def configWindow(self):
gaugeWidth = (1/5.0)*self.configSet["width"]
gaugeHeight = (1/10.0)*self.configSet["height"]
gaugeMax = 100
topBuffer = (8/10)*self.configSet["height"]
itemSep = (1/16)*self.configSet["height"]
vSizer = wx.BoxSizer(wx.VERTICAL)
textSizer = wx.BoxSizer(wx.HORIZONTAL)
progressSizer = wx.BoxSizer(wx.HORIZONTAL)
configText = wx.StaticText(self, label="STUFF", style=wx.ALIGN_CENTER)
configProgressBar = wx.Gauge(self, range=gaugeMax, size=(gaugeWidth, gaugeHeight))
textSizer.Add(configText, 1, wx.ALIGN_CENTER, 0)
progressSizer.Add(configProgressBar, 1, wx.ALIGN_CENTER, 1)
vSizer.Add(textSizer, 1, wx.TOP, topBuffer)
vSizer.Add(progressSizer, 1, wx.TOP, itemSep)
self.SetSizer(vSizer)
vSizer.Fit(self)
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Test")
config = {'width':340, 'height':270}
panel = KhPanel(self, config)
self.Show()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame()
app.MainLoop()
Incorporating Mike Driscoll's correction above, I changed the way I spaced it and it works out to be less work. I took out the horizontal sizers as I discovered they weren't needed unless I was going to space two things on the same row and I added stretch spacers instead of enormous borders. Here's the new code and it looks exactly how I wanted it:
def configWindow(self):
gaugeWidth = (4/5.0)*self.configSet["width"]
gaugeHeight = (1/10.0)*self.configSet["height"]
gaugeMax = 100
vSizer = wx.BoxSizer(wx.VERTICAL)
configText = wx.StaticText(self, label="STUFF")
configProgressBar = wx.Gauge(self, range=gaugeMax, size=(gaugeWidth, gaugeHeight))
vSizer.AddStretchSpacer(7)
vSizer.Add(configText, 1, wx.ALIGN_CENTER, 0)
vSizer.Add(configProgressBar, 1, wx.ALIGN_CENTER, 0)
vSizer.AddStretchSpacer(1)
self.SetSizer(vSizer)
return
Related
I am working with wxPython v3.0 and python v2.7 on Windows 8 OS.
The code is provided below.
Problem: If you scroll the vertical scroll bar completely to the bottom and scroll the horizontal scroll bar completely to the right side, then you shall notice empty spaces at the bottom of the panels and at the right side of the panels as shown in the image below. How to avoid this? Is this the default behavior or am I doing something wrong? It would be great if someone can test this on his/her system and confirm if this is a normal behavior?
Code:
import wx
import wx.lib.scrolledpanel
class GUI(wx.Frame):
def __init__(self, parent, id, title):
screenWidth = 700
screenHeight = 400
screenSize = (screenWidth, screenHeight)
wx.Frame.__init__(self, None, id, title, size=screenSize)
myFont = wx.Font(15, wx.MODERN, wx.NORMAL, wx.BOLD)
mainSizer = wx.BoxSizer(wx.VERTICAL)
panelsSizer = wx.BoxSizer(wx.HORIZONTAL)
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer2 = wx.BoxSizer(wx.VERTICAL)
mainPanel = wx.lib.scrolledpanel.ScrolledPanel(self, -1,style=wx.SIMPLE_BORDER)
mainPanel.SetupScrolling()
panel1 = wx.lib.scrolledpanel.ScrolledPanel(mainPanel, -1, style=wx.SIMPLE_BORDER)
panel1.SetupScrolling(scroll_y=False)
panel2 = wx.lib.scrolledpanel.ScrolledPanel(mainPanel, -1, style=wx.SIMPLE_BORDER)
panel1.SetBackgroundColour('#cccFFF')
panel2.SetBackgroundColour('#FFFaaa')
panelsSizer.Add(panel1, 1, wx.ALL|wx.EXPAND)
panelsSizer.Add(panel2, 2, wx.ALL|wx.EXPAND)
mainPanel.SetSizer(panelsSizer)
k = 0
#Populating the panel-1 with panels to show scroll bars
for i in range(1,20):
lPanels = 'lPanel'+str(k)
lPanels = wx.Panel(panel1)
label0 = str(k+1)+ '. '+'Panel-1 #############################'
text0 = wx.StaticText(lPanels, -1, label0)
text0.SetFont(myFont)
sizer1.Add(lPanels, 0, wx.ALL|wx.EXPAND, 5)
sizer1.Add(wx.StaticLine(panel1), 0, wx.ALL|wx.EXPAND, 0)
k += 1
k = 0
#Populating the panel-2 with panels to show the scroll bars
for i in range(1,20):
sPanel ='sPanel' +str(k)
sPanel = wx.Panel(panel2)
label = str(k)+ '. Panel-2#############################'
text = wx.StaticText(sPanel, -1, label)
text.SetFont(myFont)
sizer2.Add(sPanel, 0, wx.ALL|wx.EXPAND, 5)
sizer2.Add(wx.StaticLine(panel2), 0, wx.ALL|wx.EXPAND, 0)
k += 1
panel1.SetSizer(sizer1)
panel2.SetSizer(sizer2)
mainSizer.Add(mainPanel, 15, wx.EXPAND|wx.ALL)
self.SetSizer(mainSizer)
if __name__=='__main__':
app = wx.App()
frame = GUI(parent=None, id=-1, title="Test")
frame.Show()
app.MainLoop()
Thank you for your time!
It is normal. wx.ScrolledWindow rounds the virtual size up to the next multiple of the scroll rate, so you end up with between 0 and scroll rate extra pixels.
I'm trying to set-up a display to show the gamertag and avatar of users added to a text file, it most of the way there but I can't get them to position properly.
A quick mock-up of what I want: here.
Here is what I currently have on start: here
EDIT: I've switched from using a BoxSizer to using a GridSizer and that seems to have fixed the position issue, they no longer overlap, the shifting problem is still present however.
The sizer containing the users shouldn't be overlapping with the input sizer at the top, I don't know what is causing this.
And what happens when it updates to check for new users: here
Might not be that easy to see but in the second image the lowest user is shifted down, it gets further and further down as the program runs, each time it is moved down by it's own height.
The relevant code areas:
Creating the starting sizers
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
self.widget_sizer = wx.BoxSizer(wx.VERTICAL)
#Holds input for gamertags and addition
self.input_sizer = wx.BoxSizer(wx.HORIZONTAL)
#Content to be added immediately.
self.gamer_tag_textbox = wx.TextCtrl(self, -1)
self.gamer_tag_textbox.SetFocus()
self.add_gamer_tag = wx.Button(self, -1, 'Add Friend')
#Contains the displayed content
self.user_sizer = wx.BoxSizer(wx.VERTICAL)
#Add objects to sizers
self.input_sizer.Add(self.gamer_tag_textbox, 0)
self.input_sizer.Add(self.add_gamer_tag, 0)
#Set up the sizers
self.widget_sizer.Add(self.input_sizer, 0)
self.widget_sizer.Add(self.user_sizer, 0)
self.main_sizer.Add(self.widget_sizer, 0)
self.SetSizer(self.main_sizer)
Adding sizers created for each user to the main user_sizer.
def display_user_content(self, details):
self.user_sizer.Clear(True)
#This is different to the original code, it originally used boxsizers in the for each loop.
self.single_user_sizer = wx.GridSizer(cols=2)
for each in details:
#Create sizer to contain user information
#Get username
username = each[0]
#Get location of image file
location = each[-1]
#Create static text to contain username
stat = wx.StaticText(self, -1, 'username')
#Load image from location and convert to bitmap.
png = wx.Image(location, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#Create bitmap
avatar = wx.StaticBitmap(self, -1, png)
#Add to sizer
self.single_user_sizer.Add(avatar, 1)
self.single_user_sizer.Add(stat, 1)
#Add each users sizer to main user sizer
self.user_sizer.Add(self.single_user_sizer, 1)
#Add main user sizer to widget sizer
self.widget_sizer.Add(self.user_sizer, 0)
self.frame.Fit()
Full code (minus classes): here
Maybe this is similar to what you would like to achieve?
import wx
NUMBER = 3
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.windowSizer = wx.BoxSizer()
self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)
self.sizer = wx.GridBagSizer(vgap=5, hgap=5)
self.text = wx.TextCtrl(self.panel, size=(0, 0))
self.button = wx.Button(self.panel)
self.sizer.Add(self.text, (0, 0), flag=wx.EXPAND)
self.sizer.Add(self.button, (0, 1))
self.icons = []
self.stats = []
for i in range(NUMBER):
icon = wx.Panel(self.panel, size=(50, 50))
icon.SetBackgroundColour(wx.RED)
stat = wx.Panel(self.panel, size=(200, -1))
stat.SetBackgroundColour(wx.BLUE)
self.sizer.Add(icon, (i+1, 0))
self.sizer.Add(stat, (i+1, 1), flag=wx.EXPAND)
self.icons.append(icon)
self.stats.append(stat)
self.sizer.AddGrowableCol(1)
self.border = wx.BoxSizer()
self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizerAndFit(self.border)
self.SetSizerAndFit(self.windowSizer)
self.Show()
app = wx.App(False)
win1 = MainWindow(None)
app.MainLoop()
Or maybe more like this?
import wx
NUMBER = 3
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.windowSizer = wx.BoxSizer()
self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)
self.sizer = wx.GridBagSizer(vgap=5, hgap=5)
self.toolbar_sizer = wx.BoxSizer()
self.text = wx.TextCtrl(self.panel)
self.button = wx.Button(self.panel)
self.toolbar_sizer.Add(self.text, 0, flag=wx.CENTER)
self.toolbar_sizer.Add(self.button, 0)
self.sizer.Add(self.toolbar_sizer, (0, 0), span=(1, 2), flag=wx.EXPAND)
self.icons = []
self.stats = []
for i in range(NUMBER):
icon = wx.Panel(self.panel, size=(50, 50))
icon.SetBackgroundColour(wx.RED)
stat = wx.Panel(self.panel, size=(200, -1))
stat.SetBackgroundColour(wx.BLUE)
self.sizer.Add(icon, (i+1, 0))
self.sizer.Add(stat, (i+1, 1), flag=wx.EXPAND)
self.icons.append(icon)
self.stats.append(stat)
self.sizer.AddGrowableCol(1)
self.border = wx.BoxSizer()
self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizerAndFit(self.border)
self.SetSizerAndFit(self.windowSizer)
self.Show()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
At the end of the display_user_content function I was adding the user_sizer to the widget_sizer each time, this was unnecessary and was causing a doubling of the number of results, I have removed that line and my code now works.
The fixed code:
def display_user_content(self, details):
self.user_sizer.Clear(True)
self.single_user_sizer = wx.GridSizer(cols=2, hgap=5, vgap=5)
for each in details:
#Get username
self.username_sizer = wx.BoxSizer(wx.HORIZONTAL)
username = each[0]
#Get location of image file
location = each[-1]
#Create static text to contain username
stat = wx.StaticText(self, -1, 'username')
#Load image from location and convert to bitmap.
png = wx.Image(location, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#Create bitmap
avatar = wx.StaticBitmap(self, -1, png)
#Add to sizer
self.single_user_sizer.Add(avatar, 0)
self.username_sizer.Add(stat, 0)
self.single_user_sizer.Add(self.username_sizer, 0)
#Add each users sizer to main user sizer
self.user_sizer.Add(self.single_user_sizer, 0)
#Add main user sizer to widget sizer
#self.widget_sizer.Add(self.user_sizer, 0)
self.Fit()
I am trying to figure out the position of two buttons in wxpython. I have a gui with a small vertical panel on the left and large panel on the right.
wx.Frame.__init__(self, *args, **kwds)
self.Splitter = wx.SplitterWindow(self, -1)#, style=wx.SP_NOSASH)
self.Panel1 = wx.Panel(self.Splitter, -1)
self.Panel3 = wx.Panel(self.Splitter, -1)
self.Splitter.SplitVertically(self.Panel1,self.Panel3,350)
In in the right panel (a notebook) I use a Vertical Sizer to stack three panels:
self.Notebook3 = wx.Notebook(self.Panel3, -1)
self.OptPane = scrolled.ScrolledPanel(self.Notebook3, -1)
self.pane1 = wx.Panel(self.OptPane,-1, style=wx.NO_BORDER)
self.pane2 = wx.Panel(self.OptPane,-1, style=wx.RAISED_BORDER)
self.pane3= wx.Panel(self.OptPane,-1, style=wx.NO_BORDER)
My pane 3 contains three buttons that are organized using a gridsizer (one row, three columns). It all looks great. Now, I want to be able to get the screen position of the three buttons (they change based on resolution of screen, person adjusting the size of the gui, etc.).
My screen size is (1920,1080) which is derived from wx.GetDisplaySize(). I have tried self.button1.GetScreenPosition() and self.pane3.GetScreenPosition and self.button1.GetPosition(). The first returns the position (77,93), second returns (61,95) and the last one gives me (0,0). After testing with testtext = wx.StaticText(self.Notebook3, -1, 'X marks spot',pos=(240,820)), I figured out the position of the button I want returned is (240,820) -- approximately. This is the number I want to return.
Does anyone know what I am doing wrong? Thanks!
*EDIT*
My Full Code - should be runnable -- I am running this on a 1920x1080 (for the text 'x marks the spot').
import wx
import wx.lib.scrolledpanel as scrolled
class TMainForm(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.Splitter = wx.SplitterWindow(self, -1)
self.Panel1 = wx.Panel(self.Splitter, -1)
self.Panel3 = wx.Panel(self.Splitter, -1)
self.Splitter.SplitVertically(self.Panel1,self.Panel3,350)
self.Notebook2 = wx.Notebook(self.Panel1, -1)
self.Notebook3 = wx.Notebook(self.Panel3, -1)
self.OptPane = scrolled.ScrolledPanel(self.Notebook3, -1)
self.OptPane.SetupScrolling()
self.Opt_Info = wx.Panel(self.OptPane,-1, style=wx.NO_BORDER)
self.Opt_Middle = wx.Panel(self.OptPane,-1, style=wx.RAISED_BORDER)
self.Opt_Buttons = wx.Panel(self.OptPane,-1, style=wx.NO_BORDER)
self.Button1 = wx.Button(self.Opt_Buttons,-1,'Button1',size=(-1,-1))
self.Button2 = wx.Button(self.Opt_Buttons,-1,'Button2',size=(-1,-1))
self.Button3 = wx.Button(self.Opt_Buttons,-1,'Button3',size=(-1,-1))
self.MainMenu = wx.MenuBar()
self.FileMenu = wx.Menu()
self.FileOpenItem = wx.MenuItem(self.FileMenu, 103, "&Open\tCtrl+O", "Open a Previous Session", wx.ITEM_NORMAL)
self.FileMenu.AppendItem(self.FileOpenItem)
self.FileSaveItem = wx.MenuItem(self.FileMenu, 102, "&Save\tCtrl+S", "Save the data", wx.ITEM_NORMAL)
self.FileMenu.AppendItem(self.FileSaveItem)
self.FileQuitItem = wx.MenuItem(self.FileMenu, wx.ID_EXIT, "&Quit\tCtrl+Q", "Quit the program", wx.ITEM_NORMAL)
self.FileMenu.AppendItem(self.FileQuitItem)
self.MainMenu.Append(self.FileMenu, "&File")
self.SetMenuBar(self.MainMenu)
self.__set_properties()
self.__do_layout()
print self.Button1.GetScreenPosition()
testtext = wx.StaticText(self.Notebook3, -1, 'X marks spot',pos=(240,840))
def __set_properties(self):
self.SetTitle("My Program")
screen_x = 95 * wx.GetDisplaySize()[0]/100
screen_y = 90 * wx.GetDisplaySize()[1]/100
self.SetSize((screen_x, screen_y))
self.SetFocus()
def __do_layout(self , call_fit = True, set_sizer = True):
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
hbox1.Add(self.Opt_Info, 1, wx.EXPAND|wx.ALL, 3)
hbox2.Add(self.Opt_Middle, 1, wx.EXPAND|wx.ALL, 3)
hbox3.Add(self.Opt_Buttons, 1, wx.EXPAND|wx.ALL, 3)
box_bot = wx.GridSizer(1,3,2,2)
box_bot.Add(self.Button1, 1, wx.ALIGN_CENTER| wx.LEFT | wx.RIGHT, 55)
box_bot.Add(self.Button2, 1, wx.ALIGN_CENTER| wx.LEFT | wx.RIGHT, 55)
box_bot.Add(self.Button3, 1, wx.ALIGN_CENTER| wx.LEFT | wx.RIGHT, 55)
self.Opt_Buttons.SetSizer(box_bot)
vbox.Add(hbox1, 0, wx.EXPAND|wx.TOP, 20)
vbox.Add(hbox2, 1, wx.EXPAND|wx.TOP, 50)
vbox.Add(hbox3, 0, wx.EXPAND|wx.ALL, 20)
self.OptPane.SetSizer(vbox)
self.Notebook3.AddPage(self.OptPane,"Page1")
#Sizer for Panel 2
sizer_P2 = wx.BoxSizer(wx.VERTICAL)
sizer_P2.Add(self.Notebook2, 1, wx.EXPAND, 0)
self.Panel1.SetSizer(sizer_P2)
#Sizer for Panel 3
sizer_P3 = wx.BoxSizer(wx.VERTICAL)
sizer_P3.Add(self.Notebook3, 1, wx.EXPAND, 0)
self.Panel3.SetSizer(sizer_P3)
# Split Panel Sizer
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.Splitter,1,wx.EXPAND)
self.SetSizer(sizer)
self.Layout()
self.Centre()
# Code to Execute File
class TApplication(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
MainForm = TMainForm(None, -1,"")
self.SetTopWindow(MainForm)
MainForm.Show()
return 1
if __name__ == "__main__":
Application = TApplication(0)
Application.MainLoop()
I think you're asking for the position too soon. You'll want to get the position after everything is rendered to the screen, so you'll probably need to use wxPython's CallAfter. Create a method that looks something like this:
def printLocation(self):
""""""
print self.Button1.GetScreenPosition()
Then at the end of your init, add the following line:
wx.CallAfter(self.printLocation)
When I ran it on my system, I got (155, 211), which is probably closer to what you're looking for. I have a rather weird resolution here.
You might also want to look at this thread which talks about the scrolled window's CalcUnscrolledPosition method.
https://groups.google.com/forum/#!topic/wxpython-users/0VlpIcBYs04
When I only hade one TxtCtrl, it's right side would expand all the way to the windows edge. But after I created a vertical BoxSizer to and added the horizontal one to that, the TxtCtrol only had a width of about 100 pixels. Why is this?
import wx
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, style=wx.SIMPLE_BORDER)
top_box = wx.BoxSizer(wx.VERTICAL)
box1 = wx.BoxSizer(wx.HORIZONTAL)
box2 = wx.BoxSizer(wx.HORIZONTAL)
textureName = wx.TextCtrl(self, 1)
texturePath = wx.TextCtrl(self, 1)
box1.Add(wx.StaticText(self, 1, "Name: "), 0, wx.LEFT|wx.RIGHT|wx.TOP, 5)
box1.Add(textureName, 1, wx.ALIGN_LEFT|wx.RIGHT|wx.TOP, 5)
box2.Add(wx.StaticText(self, 1, "Path: "), 0, wx.LEFT|wx.RIGHT|wx.TOP, 5)
box2.Add(texturePath, 1, wx.ALIGN_LEFT|wx.RIGHT|wx.TOP, 5)
top_box.Add(box1)
top_box.Add(box2)
self.SetSizer(top_box)
class MainFrame(wx.Frame):
def __init__(self, parent, id):
title = "Exporter"
wx.Frame.__init__(self, parent, wx.ID_ANY, title,
size=(500, 420),
style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
self.panel = MainPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MainFrame(None, -1)
frame.Centre()
app.MainLoop()
pass
You need to use wx.EXPAND in the style.
top_box.Add(box1, 0, wx.EXPAND)
top_box.Add(box2)
From here: "When an item is Add'ed with the wxEXPAND flag, the item will be resized to fill its alloted area in the opposite orientation."
A simplified version of the code is posted below (white space, comments, etc. removed to reduce size - but the general format to my program is kept roughly the same).
When I run the script, the static text correctly wraps as it should, but the other items in the panel do not move down (they act as if the statictext is only one line and thus not everything is visible).
If I manually resize the window/frame, even just a tiny amount, everything gets corrected, and displays as it is should.
Why doesn't it display correctly to begin with? I've tried all sorts of combination's of GetParent().Refresh() or Update() and GetTopLevelParent().Update() or Refresh(). I've also tried everything I can think of but cannot get it to display correctly without manually resizing the frame/window. Once re-sized, it works exactly as I want it to.
Information:
Windows XP
Python 2.5.2
wxPython 2.8.11.0 (msw-unicode)
My Code:
#! /usr/bin/python
import wx
class StaticWrapText(wx.PyControl):
def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER,
validator=wx.DefaultValidator, name='StaticWrapText'):
wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name)
self.statictext = wx.StaticText(self, wx.ID_ANY, label, style=style)
self.wraplabel = label
#self.wrap()
def wrap(self):
self.Freeze()
self.statictext.SetLabel(self.wraplabel)
self.statictext.Wrap(self.GetSize().width)
self.Thaw()
def DoGetBestSize(self):
self.wrap()
#print self.statictext.GetSize()
self.SetSize(self.statictext.GetSize())
return self.GetSize()
class TestPanel(wx.Panel):
def __init__(self, *args, **kwargs):
# Init the base class
wx.Panel.__init__(self, *args, **kwargs)
self.createControls()
def createControls(self):
# --- Panel2 -------------------------------------------------------------
self.Panel2 = wx.Panel(self, -1)
msg1 = 'Below is a List of Files to be Processed'
staticBox = wx.StaticBox(self.Panel2, label=msg1)
Panel2_box1_v1 = wx.StaticBoxSizer(staticBox, wx.VERTICAL)
Panel2_box2_h1 = wx.BoxSizer(wx.HORIZONTAL)
Panel2_box3_v1 = wx.BoxSizer(wx.VERTICAL)
self.wxL_Inputs = wx.ListBox(self.Panel2, wx.ID_ANY, style=wx.LB_EXTENDED)
sz = dict(size=(120,-1))
wxB_AddFile = wx.Button(self.Panel2, label='Add File', **sz)
wxB_DeleteFile = wx.Button(self.Panel2, label='Delete Selected', **sz)
wxB_ClearFiles = wx.Button(self.Panel2, label='Clear All', **sz)
Panel2_box3_v1.Add(wxB_AddFile, 0, wx.TOP, 0)
Panel2_box3_v1.Add(wxB_DeleteFile, 0, wx.TOP, 0)
Panel2_box3_v1.Add(wxB_ClearFiles, 0, wx.TOP, 0)
Panel2_box2_h1.Add(self.wxL_Inputs, 1, wx.ALL|wx.EXPAND, 2)
Panel2_box2_h1.Add(Panel2_box3_v1, 0, wx.ALL|wx.EXPAND, 2)
msg = 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
msg += 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
msg += 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
msg += 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
staticMsg = StaticWrapText(self.Panel2, label=msg)
Panel2_box1_v1.Add(staticMsg, 0, wx.ALL|wx.EXPAND, 2)
Panel2_box1_v1.Add(Panel2_box2_h1, 1, wx.ALL|wx.EXPAND, 0)
self.Panel2.SetSizer(Panel2_box1_v1)
# --- Combine Everything -------------------------------------------------
final_vbox = wx.BoxSizer(wx.VERTICAL)
final_vbox.Add(self.Panel2, 1, wx.ALL|wx.EXPAND, 2)
self.SetSizerAndFit(final_vbox)
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
# Init the base class
wx.Frame.__init__(self, *args, **kwargs)
panel = TestPanel(self)
self.SetClientSize(wx.Size(500,500))
self.Center()
class wxFileCleanupApp(wx.App):
def __init__(self, *args, **kwargs):
# Init the base class
wx.App.__init__(self, *args, **kwargs)
def OnInit(self):
# Create the frame, center it, and show it
frame = TestFrame(None, title='Test Frame')
frame.Show()
return True
if __name__ == '__main__':
app = wxFileCleanupApp()
app.MainLoop()
Using Mike Driscoll's code as a baseline, I hope this demonstrates my issue. There are two different versions of using "txt". Here are three things I want you to try:
Run it as-is. With my StaticWrapText. It displays wrong at first, but re-size the window and it works EXACTLY as I want. There is no blank/wasted space below the text before the "button"
Change these two lines (change the comments):
txt = wx.StaticText(panel, label=text)
#txt = StaticWrapText(panel, label=text)
Now you will see there is no wrapping and the text is always on only one line. Definitely not what we want. This is because of "sizer.Add(txt, 0, wx.EXPAND, 5) "...so going on to Part 3...
Keep the change from Part 2 and also change:
sizer.Add(txt, 0, wx.EXPAND, 5)
to:
sizer.Add(txt, 1, wx.EXPAND, 5)
So now the statictext will expand. This is CLOSE to working...BUT I don't want all that wasted space between the text and the button. If you make the window large, there is a lot of wasted space. See Part 1 after the window is re-sized to see the difference.
Code:
import wx
class StaticWrapText(wx.PyControl):
def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER,
validator=wx.DefaultValidator, name='StaticWrapText'):
wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name)
self.statictext = wx.StaticText(self, wx.ID_ANY, label, style=style)
self.wraplabel = label
#self.wrap()
def wrap(self):
self.Freeze()
self.statictext.SetLabel(self.wraplabel)
self.statictext.Wrap(self.GetSize().width)
self.Thaw()
def DoGetBestSize(self):
self.wrap()
#print self.statictext.GetSize()
self.SetSize(self.statictext.GetSize())
return self.GetSize()
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
text = "I'm subclasses the statictext because I want it to act exactly like a static text, but correctly wordwrap as needed. I've found several examples of it on the web, but none that worked how I wanted. The wordwrap makes it look much nicer when the user may decide to re-size the window, so I would definitely like to have it be wordwrapped. I know about the wx.lib.wordwrap, but chose to use the built in Wrap function of the statictext control instead. It basically does the same thing from what I understand."
#txt = wx.StaticText(panel, label=text)
txt = StaticWrapText(panel, label=text)
wxbutton = wx.Button(panel, label='Button', size=wx.Size(120,50))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(txt, 0, wx.EXPAND, 5)
sizer.Add(wxbutton, 1, wx.EXPAND, 5)
panel.SetSizer(sizer)
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
EDIT:
AHHH...finally! I tried using the Layout() method on virtually every level of the program, but I actually needed to use Layout() on the SIZER which is found with the method GetSizer() - or you can send SendSizeEvent() to the panel (commented in code below). Thus, the following now does EXACTLY what I want! Thanks for the help. The only other change was to store the panel with self.panel in the frame class. As a note, I had to put this statement AFTER the frame.Show() or it didn't work correctly.
Code:
import wx
class StaticWrapText(wx.PyControl):
def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER,
validator=wx.DefaultValidator, name='StaticWrapText'):
wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name)
self.statictext = wx.StaticText(self, wx.ID_ANY, label, style=style)
self.wraplabel = label
#self.wrap()
def wrap(self):
self.Freeze()
self.statictext.SetLabel(self.wraplabel)
self.statictext.Wrap(self.GetSize().width)
self.Thaw()
def DoGetBestSize(self):
self.wrap()
#print self.statictext.GetSize()
self.SetSize(self.statictext.GetSize())
return self.GetSize()
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
# Add a panel so it looks the correct on all platforms
self.panel = wx.Panel(self, wx.ID_ANY)
text = "I'm subclasses the statictext because I want it to act exactly like a static text, but correctly wordwrap as needed. I've found several examples of it on the web, but none that worked how I wanted. The wordwrap makes it look much nicer when the user may decide to re-size the window, so I would definitely like to have it be wordwrapped. I know about the wx.lib.wordwrap, but chose to use the built in Wrap function of the statictext control instead. It basically does the same thing from what I understand."
txt = StaticWrapText(self.panel, label=text)
wxbutton = wx.Button(self.panel, label='Button', size=wx.Size(120,50))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(txt, 0, wx.EXPAND, 5)
sizer.Add(wxbutton, 1, wx.EXPAND, 5)
self.panel.SetSizer(sizer)
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm()
frame.Show()
#frame.panel.SendSizeEvent()
frame.panel.GetSizer().Layout()
app.MainLoop()
As a final note, in my original program posted, the following line needs to be added just before or after frame.Show():
frame.panel.Panel2.GetSizer().Layout()
Interestingly...with that original example this can be before or after frame.Show() but the other example requires that it be after frame.Show(). I'm not sure why, but just put it after and you're safe.
I use
width = 200 # panel width
txt = wx.StaticText(panel, label=text)
txt.Wrap(width)
This works great and the next widgets are positioned correctly. You can easily do the txt.Wrap(width) dynamically.
Why are you subclassing it? Do you need wordwrap? If so, there's a module for that in wx.lib.wordwrap that you can use.
In answer the the OP's comment, check this out:
import wx
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
text = "I'm subclasses the statictext because I want it to act exactly like a static text, but correctly wordwrap as needed. I've found several examples of it on the web, but none that worked how I wanted. The wordwrap makes it look much nicer when the user may decide to re-size the window, so I would definitely like to have it be wordwrapped. I know about the wx.lib.wordwrap, but chose to use the built in Wrap function of the statictext control instead. It basically does the same thing from what I understand."
txt = wx.StaticText(panel, label=text)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(txt, 1, wx.EXPAND, 5)
panel.SetSizer(sizer)
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
I used the OP's comment for the text. Anyway, this works fine for me on Windows XP, Python 2.5 and wxPython 2.8.10.1.
I found what I think is a much easier and automatic way to handle this issue.
After creating the StaticText control, bind the control's wx.EVT_SIZE to a handler that calls the StaticText's Wrap() function with the event's GetSize()[0] as an argument (and then skips the event).
An example:
class MyDialog(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent = parent, title = "Test Dialog", style = wx.CAPTION)
bigstr = "This is a really long string that is intended to test the wrapping functionality of the StaticText control in this dialog. If it works correctly, it should appear as multiple lines of text with a minimum of fuss."
self.__label__ = wx.StaticText(parent = self, label = bigstr)
self.__actionbutton__ = wx.Button(parent = self, label = "Go")
self.__label__.Bind(wx.EVT_SIZE, self.__WrapText__)
self.__actionbutton__.Bind(wx.EVT_BUTTON, self.__OnButton__)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.__label__, flag = wx.ALL | wx.EXPAND, border = 5)
sizer.Add(self.__actionbutton__, flag = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.CENTER, border = 0)
self.SetSizer(sizer)
self.Layout()
def __OnButton__(self, event):
self.EndModal(wx.ID_OK)
def __WrapText__(self, event):
self.__label__.Wrap(event.GetSize()[0])
event.Skip()
This is what it looks like on my system (MSW, Python 2.7.5, wx 2.8.12.1):