Fitting a wxWindows frame to a grid - python

In my wxPython GUI app I want to show a dialog with a grid on it and just two buttons below that grid. The grid may be initialised with a number of rows, which is not known in advance. My goal is to automatically let the dialog window become as large as necessary in order to include the whole grid (i.e. I would like to avoid the scrollbars of the grid).
However, I can't get that to work, even when using sizers and the wx.EXPAND flag. Any ideas what I am doing wrong here? This is a fully working example of my problem:
import wx
import wx.grid
class MyApp(wx.Frame):
def __init__(self):
super(MyApp, self).__init__(None, title="wxPython Sizers", style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
self.panel = wx.Panel(self)
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
# Keras model to product / pipe type mapping grid
logtab = wx.grid.Grid(self.panel)
logtab.CreateGrid(25, 3)
logtab.SetColLabelValue(0, "One")
logtab.SetColLabelValue(1, "Two")
logtab.SetColLabelValue(2, "Three")
grid_sizer = wx.BoxSizer(wx.VERTICAL)
grid_sizer.Add(logtab, wx.ID_ANY, wx.EXPAND)
self.panel.SetSizer(grid_sizer)
# Dialog buttons
btns = wx.BoxSizer(wx.HORIZONTAL)
save_button = wx.Button(self, label="Save")
cancel_button = wx.Button(self, label="Cancel")
btns.Add(save_button)
btns.Add(cancel_button)
# Set final dialog layout
self.main_sizer.Add(self.panel, proportion=1, flag=wx.ALL | wx.EXPAND, border=15)
self.main_sizer.Add(btns, flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=15)
self.SetSizerAndFit(self.main_sizer)
self.Layout()
self.Center()
self.Show()
if __name__ == "__main__":
wxapp = wx.App()
myApp = MyApp()
wxapp.MainLoop()

Related

Make TextCtrl Scale with Window

I've just gotten into wxpython for Python 3.6 and I've hit a roadblock. I just can't work out how to make elements/widgets scale with the screen! I know you have to use sizers but that's about it, I'm still fairly new to programming so just reading the documentation didn't help. If someone could just sample some code that works I'd be very thankful as I could then read through it and work out what I was doing wrong. The code that draws out the GUI I want to scale with window size is below, the key idea is that the TextCtrl scales, other elements don't really need scaling.
def createGUI(self):
panel = wx.Panel(self)
menuBar = wx.MenuBar()
menuButton = wx.Menu()
newItem = wx.MenuItem(menuButton, wx.ID_NEW, 'New Note\tCtrl+N')
delItem = wx.MenuItem(menuButton, wx.ID_DELETE, 'Delete Note\tCtrl+Backspace')
saveItem = wx.MenuItem(menuButton, wx.ID_SAVE, 'Save\tCtrl+S')
exitItem = wx.MenuItem(menuButton, wx.ID_EXIT, 'Quit\tCtrl+Q')
menuButton.Append(newItem)
menuButton.Append(saveItem)
menuButton.Append(delItem)
menuButton.Append(exitItem)
menuBar.Append(menuButton, 'Menu')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.new, newItem)
self.Bind(wx.EVT_MENU, self.delete, delItem)
self.Bind(wx.EVT_MENU, self.save, saveItem)
self.Bind(wx.EVT_MENU, self.onExit, exitItem)
self.noteText = wx.TextCtrl(panel)
self.noteText.AppendText(self.notecontent)
self.Bind(wx.EVT_CLOSE, self.onExit)
self.SetTitle(f'Welcome {self.username}! You are working on {self.notepath}')
self.Centre()
self.Show(True)
To begin with it's best to equate sizers to something familiar and I usually think of storage boxes or a chest of drawers.
When we define widgets, they are all dumped into a container, the parent object, often the ubiquitous self or self.panel.
If we do not assign a size and pos to each item, it's just a jumbled mess, a pile of widgets.
The sizer, there are many types, are the virtual drawers in our chest of drawers, that herds this pile of widgets into order.
The widgets are assigned to the appropriate sizer, note sizers can go into other sizers, and eventually when everything has been assigned a place or drawer in our chest of drawers, the sizers do their magic, arranging and sizing all of widgets into a coherent screen for display or arranging the drawers contents and relative positions in the chest of drawers.
Below, I've used the simplest sizer a boxsizer.
One will arrange things vertically and the other horizontally.
The horizontal sizer is for the buttons and the vertical sizer, will be the main sizer, into which I place not only the TectCtrl but also the buttons, prearranged in their horizontal sizer.
For a better and more comprehensive description of sizers and their controls see: https://docs.wxpython.org/sizers_overview.html and for details on the actual sizers available see the detailed documenation on each.
A loose approximation of your code:
#!/usr/bin/python
import wx
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,
size=(450, 350))
self.panel = wx.Panel(self, -1)
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.noteText = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE)
self.Button_close = wx.Button(self.panel, -1, label="Quit")
self.Button_1 = wx.Button(self.panel, -1, label="Btn1")
self.Button_2 = wx.Button(self.panel, -1, label="Btn2")
self.Bind(wx.EVT_CLOSE, self.onExit)
self.Button_close.Bind(wx.EVT_BUTTON, self.onExit)
self.Button_1.Bind(wx.EVT_BUTTON, self.onButton)
self.Button_2.Bind(wx.EVT_BUTTON, self.onButton)
# Place buttons within their own horizontal sizer
self.button_sizer.Add(self.Button_close,proportion=0, flag=wx.ALL, border=10)
self.button_sizer.Add(self.Button_1,proportion=0, flag=wx.ALL, border=10)
self.button_sizer.Add(self.Button_2,proportion=0, flag=wx.ALL, border=10)
# Add textctrl and the button sizer to the main sizer (vertical)
self.main_sizer.Add(self.noteText,proportion=1, flag=wx.EXPAND|wx.ALL, border=10)
self.main_sizer.Add(self.button_sizer, 0, 0, 0)
self.panel.SetSizer(self.main_sizer)
self.Show()
def onExit(self, event):
self.Destroy()
def onButton(self, event):
print("A button was pressed")
if __name__ == '__main__':
app = wx.App()
Example(None, title="Example")
app.MainLoop()
Edit:
Here's the same code without using sizers, although they are recommended.
#!/usr/bin/python
import wx
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,
size=(450, 350))
self.panel = wx.Panel(self, -1)
self.noteText = wx.TextCtrl(self.panel, -1, pos=(10,10), size=(400,280), style=wx.TE_MULTILINE)
self.Button_close = wx.Button(self.panel, -1, label="Quit", pos=(10,290), size=(50,30))
self.Button_1 = wx.Button(self.panel, -1, label="Btn1", pos=(70,290), size=(50,30))
self.Button_2 = wx.Button(self.panel, -1, label="Btn2", pos=(130,290), size=(50,30))
self.Bind(wx.EVT_CLOSE, self.onExit)
self.Button_close.Bind(wx.EVT_BUTTON, self.onExit)
self.Button_1.Bind(wx.EVT_BUTTON, self.onButton)
self.Button_2.Bind(wx.EVT_BUTTON, self.onButton)
self.Show()
def onExit(self, event):
self.Destroy()
def onButton(self, event):
print("A button was pressed")
if __name__ == '__main__':
app = wx.App()
Example(None, title="Example")
app.MainLoop()

Padding in Panel and static text wrap in wxpython

I would like to have a layout something like this
There is a Main Panel just inside the frame with blank padding in the left and right which another Sub Panel located in the middle. A Static Text element located at the top of Sub Panel and it should wrap properly when changing the window size.
My code snippet listed as following
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
super(MyPanel, self).__init__(parent)
self.hsizer = wx.BoxSizer()
self.hsizer.AddStretchSpacer()
self.panel = wx.Panel(self)
self.panel.SetSize((350, -1))
self.vsizer = wx.BoxSizer(wx.VERTICAL)
label = (
"This is a long scentence that I want it wrapped properly, "
"but it doesn't seem to work at work. "
"I prefer if you guys can give any sugeestions or help. "
"For Long Long Long Long Long scentence."
)
style = wx.ALIGN_LEFT
self.static_text = wx.StaticText(self, label=label, style=style)
self.vsizer.Add(self.static_text, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
self.static_text.Wrap(self.panel.Size[0])
self.static_text.SetAutoLayout(True)
self.panel.SetSizer(self.vsizer)
self.hsizer.Add(self.panel, flag=wx.ALIGN_CENTER)
self.hsizer.AddStretchSpacer()
self.SetSizer(self.hsizer)
self.Layout()
print self.Size
print self.panel.Size
if __name__ == '__main__':
app = wx.App()
frame = wx.Frame(None)
sizer = wx.BoxSizer()
sizer.Add(MyPanel(frame), flag=wx.EXPAND)
frame.SetSizer(sizer)
frame.Show()
app.MainLoop()
Currently, not only the Sub Panel is not located in the middle, but also the text in static text doesn't wrap properly when changing the window size. Any suggestions to make it work?
It is often helpful to set some background colours while trying to implement a layout with sizers, that way it's easy to visualize where the boundaries are, and how they behave while resizing things. Another helpful tool is the Widget Inspection Tool. For example:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
super(MyPanel, self).__init__(parent)
self.SetBackgroundColour('pink')
self.hsizer = wx.BoxSizer()
self.hsizer.AddStretchSpacer()
self.panel = wx.Panel(self)
self.panel.SetSize((350, -1))
self.panel.SetBackgroundColour('green')
self.vsizer = wx.BoxSizer(wx.VERTICAL)
label = (
"This is a long scentence that I want it wrapped properly, "
"but it doesn't seem to work at work. "
"I prefer if you guys can give any sugeestions or help. "
"For Long Long Long Long Long scentence."
)
style = wx.ALIGN_LEFT
self.static_text = wx.StaticText(self, label=label, style=style)
self.static_text.SetBackgroundColour('yellow')
self.vsizer.Add(self.static_text, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
self.static_text.Wrap(self.panel.Size[0])
self.static_text.SetAutoLayout(True)
self.panel.SetSizer(self.vsizer)
self.hsizer.Add(self.panel, flag=wx.ALIGN_CENTER)
self.hsizer.AddStretchSpacer()
self.SetSizer(self.hsizer)
if __name__ == '__main__':
app = wx.App()
frame = wx.Frame(None)
frame.SetBackgroundColour('sky blue')
sizer = wx.BoxSizer()
sizer.Add(MyPanel(frame), flag=wx.EXPAND)
frame.SetSizer(sizer)
frame.Show()
import wx.lib.inspection
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
After running that we can see that things don't really match your sketch of the layout, that things like the text don't change size or position when the frame is resized, and that the MyPanel (the pink one) is also not resizing to fill the frame. Also, it appears from the code that you intend the text to be the child of the green panel (self.panel) but looking at the colours at runtime it obviously is not. Looking again at the code we see that it is being created with self as the parent instead of self.panel.
So after a few more tweaks to address those issues, we end up with this code:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
super(MyPanel, self).__init__(parent)
self.SetBackgroundColour('pink')
self.hsizer = wx.BoxSizer()
self.hsizer.AddStretchSpacer()
self.panel = wx.Panel(self)
self.panel.SetSize((350, -1))
self.panel.SetBackgroundColour('green')
self.vsizer = wx.BoxSizer(wx.VERTICAL)
label = (
"This is a long scentence that I want it wrapped properly, "
"but it doesn't seem to work at work. "
"I prefer if you guys can give any sugeestions or help. "
"For Long Long Long Long Long scentence."
)
style = wx.ALIGN_LEFT
self.static_text = wx.StaticText(self.panel, label=label, style=style)
self.static_text.SetBackgroundColour('yellow')
self.vsizer.Add(self.static_text,
flag=wx.EXPAND | wx.ALL | wx.ALIGN_TOP, border=5)
self.static_text.Wrap(self.panel.Size[0])
self.panel.SetSizer(self.vsizer)
self.hsizer.Add(self.panel, 1, flag=wx.EXPAND)
self.hsizer.AddStretchSpacer()
self.SetSizer(self.hsizer)
if __name__ == '__main__':
app = wx.App()
frame = wx.Frame(None)
frame.SetBackgroundColour('sky blue')
sizer = wx.BoxSizer()
sizer.Add(MyPanel(frame), 1, flag=wx.EXPAND)
frame.SetSizer(sizer)
frame.Show()
import wx.lib.inspection
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
Finally, if you want the text to be rewrapped when the size gets narrower than 350 then you'll need to do something like catch EVT_SIZE events and call Wrap again with a different size when needed.

wx.Grid and wx.StockCursor

I've created wx.Grid widget inside my frame and I want to change my type of cursor if the user is using the grid widget. I've managed to do that with wx.StockCursor and .SetCursor methods but my cursor keeps returning to standard cursor if the user moves the cursor above the intersections of cell and row borders. What is causing this?
import wx
import wx.grid as Gridw
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, 'Data selection', size=(785, 540))
self.Centre()
#------------------------------------------------------------------------------
panel = wx.Panel(self, wx.ID_ANY)
#------------------------------------------------------------------------------
self.grid = Gridw.Grid(panel)
self.grid.CreateGrid(250, 250)
self.grid.EnableDragGridSize(0)
self.grid.DisableDragColSize()
self.grid.DisableDragRowSize()
self.grid.SetColMinimalWidth(0, 100)
#------------------------------------------------------------------------------
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer_v = wx.BoxSizer(wx.VERTICAL)
sizer_v.Add(wx.Button(panel, -1, 'Button'), 1, wx.CENTER | wx.ALL, 5)
sizer.Add(self.grid, 1, wx.EXPAND, 5)
sizer.Add(sizer_v, 0)
panel.SetSizer(sizer)
#------------------------------------------------------------------------------
self.CreateStatusBar()
self.Show(True)
#------------------------------------------------------------------------------
cross_c = wx.StockCursor(wx.CURSOR_CROSS)
self.grid.SetCursor(cross_c)
if __name__ == '__main__':
app = wx.App()
frame = Frame().Show()
app.MainLoop()
Looks like the problem is related to that you've disabled grid resizing via EnableDragGridSize(0), DisableDragColSize() and DisableDragRowSize(). This can somewhat explain why you are seeing standard cursor on the cell borders.
Not sure if it'll help you since I don't know what OS are you using, but this works for me on linux:
cross_c = wx.StockCursor(wx.CURSOR_CROSS)
self.grid.GetGridWindow().SetCursor(cross_c)
One more option is to listen for EVT_MOTION and set cursor in the event listener:
self.cross_c = wx.StockCursor(wx.CURSOR_CROSS)
self.grid.GetTargetWindow().SetCursor(self.cross_c)
wx.EVT_MOTION(self.grid.GetGridWindow(), self.OnMouseMotion)
def OnMouseMotion(self, evt):
self.grid.GetTargetWindow().SetCursor(self.cross_c)
evt.Skip()
Hope that helps.

wxPython: wx.Panel of AuiManager has empty space of gray

I am trying to make a window with 2 panels. One panel is just a notebook panel. The second panel contains a toolbar on top and a text control on the bottom. I want to arrange this panel in my frame using wx.aui.AuiManager.
The problem is that I get a big empty space of grey in my custom panel.
Here is my code:
import wx
import wx.aui
import images # contains toolbar icons
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"AUI Tutorial",
size=(600,400))
self._mgr = wx.aui.AuiManager()
self._mgr.SetManagedWindow(self)
notebook = wx.aui.AuiNotebook(self)
nb_panel = TabPanel(notebook)
my_panel = MyPanel(self)
notebook.AddPage(nb_panel, "First Tab", False)
self._mgr.AddPane(notebook,
wx.aui.AuiPaneInfo().Name("notebook-content").
CenterPane().PaneBorder(False))
self._mgr.AddPane(my_panel,
wx.aui.AuiPaneInfo().Name("txtctrl-content").
CenterPane().PaneBorder(False))
self._mgr.GetPane("notebook-content").Show().Top().Layer(0).Row(0).Position(0)
self._mgr.GetPane("txtctrl-content").Show().Bottom().Layer(1).Row(0).Position(0)
self._mgr.Update()
class MyPanel(wx.Panel):
"""
My panel with a toolbar and richtextctrl
"""
def __init__(self,parent):
wx.Panel.__init__(self,parent=parent,id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
toolbar = wx.ToolBar(self,-1)
toolbar.AddLabelTool(wx.ID_EXIT, '', images._rt_smiley.GetBitmap())
self.Bind(wx.EVT_TOOL, self.OnExit, id=wx.ID_EXIT)
toolbar.Realize()
sizer.Add(toolbar,proportion=0,flag=wx.ALL | wx.ALIGN_TOP)
text = ""
txtctrl = wx.TextCtrl(self,-1, text, wx.Point(0, 0), wx.Size(150, 90),
wx.NO_BORDER | wx.TE_MULTILINE | wx.TE_READONLY|wx.HSCROLL)
sizer.Add(txtctrl,proportion=0,flag=wx.EXPAND)
self.SetSizer(sizer)
def OnExit(self,event):
self.Close()
class TabPanel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent=parent,id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(txtOne, 0, wx.ALL, 5)
sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
app.MainLoop()
So, how do I fix my code so that I don't have that grey block taking up MyPanel? Also, my toolbar button doesn't seem to run self.OnExit(). Why is that?
Thank you for your help.
Take out the line:
self._mgr.GetPane("notebook-content").Show().Top().Layer(0).Row(0).Position(0)
As for the OnExit() handler, it is firing!
If you want to exit the application, replace it with app.Exit()

wxPython - Redrawing Error when replacing wxFrame's Panel

I'm creating a small wxPython utility for the first time, and I'm stuck on a problem.
I would like to add components to an already created frame. To do this, I am destroying the frame's old panel, and creating a new panel with all new components.
1: Is there a better way of dynamically adding content to a panel?
2: Why, in the following example, do I get a a strange redraw error in which in the panel is drawn only in the top left hand corner, and when resized, the panel is drawn correctly?
(WinXP, Python 2.5, latest wxPython)
Thank you for the help!
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'TimeTablr')
#Variables
self.iCalFiles = ['Empty', 'Empty', 'Empty']
self.panel = wx.Panel(self, -1)
self.layoutElements()
def layoutElements(self):
self.panel.Destroy()
self.panel = wx.Panel(self, -1)
#Buttons
self.getFilesButton = wx.Button(self.panel, 1, 'Get Files')
self.calculateButton = wx.Button(self.panel, 2, 'Calculate')
self.quitButton = wx.Button(self.panel, 3, 'Quit Application')
#Binds
self.Bind(wx.EVT_BUTTON, self.Quit, id=3)
self.Bind(wx.EVT_BUTTON, self.getFiles, id=1)
#Layout Managers
vbox = wx.BoxSizer(wx.VERTICAL)
#Panel Contents
self.ctrlsToDescribe = []
self.fileNames = []
for iCalFile in self.iCalFiles:
self.ctrlsToDescribe.append(wx.TextCtrl(self.panel, -1))
self.fileNames.append(wx.StaticText(self.panel, -1, iCalFile))
#Add Components to Layout Managers
for i in range(0, len(self.ctrlsToDescribe)):
hboxtemp = wx.BoxSizer(wx.HORIZONTAL)
hboxtemp.AddStretchSpacer()
hboxtemp.Add(self.fileNames[i], 1, wx.EXPAND)
hboxtemp.AddStretchSpacer()
hboxtemp.Add(self.ctrlsToDescribe[i], 2, wx.EXPAND)
hboxtemp.AddStretchSpacer()
vbox.Add(hboxtemp)
finalHBox = wx.BoxSizer(wx.HORIZONTAL)
finalHBox.Add(self.getFilesButton)
finalHBox.Add(self.calculateButton)
finalHBox.Add(self.quitButton)
vbox.Add(finalHBox)
self.panel.SetSizer(vbox)
self.Show()
def Quit(self, event):
self.Destroy()
def getFiles(self, event):
self.iCalFiles = ['Example1','Example1','Example1','Example1','Example1','Example1']
self.layoutElements()
self.Update()
app = wx.App()
MainFrame()
app.MainLoop()
del app
1) I beleive the Sizer will let you insert elements into the existing ordering of them. That would probably be a bit faster.
2) I don't see the behavior you're describing on OSX, but at a guess, try calling self.Layout() before self.Show() in layoutElements?
I had a similar problem where the panel would be squished into the upper-right corner. I solved it by calling panel.Fit().
In your example, you should call self.panel.Fit() after self.panel.SetSizer(vbox)

Categories