So, I'm trying to write a program using wxPython which will have a notebook with tabs that each have the user enter some data into textboxes. This is for a physics related project so I want each textbox to have a "units" label after it displaying what units it should be entered in. The problem is: When the program runs, I get a black rectangle in the upper left corner of each textctrl which is the size of the label that the units are in. The black box disappears forever immediately after any of the following:
press tab onto the textctrl
move the cursor onto the textctrl
switch tabs in the notebook
resize the window until the textctrl has to shrink with it.
functionally everything works fine, I just want to get rid of the black rectangles on startup.
This is my code:
#! usr/bin/python
import wx
class MainWindow(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, size=(400,300))
self.make_gui()
self.Show()
def make_gui(self):
self.panel=wx.Panel(self,wx.ID_ANY)
sizer=wx.BoxSizer(wx.VERTICAL)
ins=self.gui_inputs()
sizer.Add(ins)
self.panel.SetSizer(sizer)
def gui_inputs(self):
sizer=wx.BoxSizer(wx.HORIZONTAL)
simpleinputs=(("Page 1",(("Name",None),("Item","in"))),("sec page",tuple()))
simple=self.gui_inputs_make_simple(simpleinputs)
sizer.Add(simple)
return sizer
def gui_inputs_make_simple(self,simpleinputs):
sizer=wx.BoxSizer(wx.VERTICAL)
notebook=wx.Notebook(self.panel)
for tab in simpleinputs:
pan=wx.Panel(notebook,wx.ID_ANY)
siz=wx.BoxSizer(wx.VERTICAL)
for item in tab[1]:
it=self.gui_inputs_make_labeled_unitinput(pan,item[0],item[1])
siz.Add(it)
pan.SetSizer(siz)
notebook.AddPage(pan,tab[0])
sizer.Add(notebook)
return sizer
def gui_inputs_make_labeled_unitinput(self,par,label='',units='',validatenumber=False):
sizer=wx.BoxSizer(wx.HORIZONTAL)
lbl=wx.StaticText(par,label=label+':')
sizer.Add(lbl)
edit=wx.TextCtrl(par)
sizer.Add(edit)
if units!=None:
unit=wx.StaticText(par,label=units)
sizer.Add(unit)
return sizer
app = wx.App(False)
frame = MainWindow("GUI")
app.MainLoop()
and this is a screenshot of what I get:
The following are ways I was able to get the black rectangles to disappear (but none let me do what I need to do):
When I remove the code to put the units in, it works. When I remove the text from the units fields, it works. When I have only one tab, it works.
If anyone can tell me why this is happening, I would be greatly appreciative.
Thanks in advance!
This was an interesting one:
The black boxes in the TextCtrl instances disappear as soon the sizer cascades are set up properly. You can do this by updating the layout of your main sizer by:
sizer.Layout()
or
sizer.Fit(self)
at the end of your make_gui method.
This works for me but does of course not explain why the TextCtrl stays black in the first place without.
Related
I have frames designed in seperate scripts that are all called to this central gui script which shows them using QMainWindow as one would expect.
However, I am trying to redesign the Main Window in such a way that there is a permanently fixed frame on the right hand side, and a tabbed frame on the left.
Before I attempted to make this happen, I could display, using setlayout, all of the frames on the mainwindow at once, however, when I try to add several of these"sub-widget" frames to my new tabbed frame everything gets annoying...
... effectively, I am taking already functioning widgets (designed w/ QFrame) called from other scripts and trying to make 2 new widgets with them (QTabWidget, for the left frame, and QGroupBox for the right). However, when run, the window pops up with my groupbox and tabwidgets completely empty, and my "sub-widgets" are displayed in new windows.
Here is a snippet:
tab_frame = QWidget()
tab_frame.layout = QVBoxLayout() #-------------------- Layout
tab_frame.layout.addWidget(self.FRAME_FROM_OTHER_SCRIPT)
tab_frame.layout.addStretch(1)
#### Tabs Parent ####
tabs = QTabWidget()
tabs.layout = QVBoxLayout()
tabs.addTab(tab_frame,"Tab 1")
#### Fixed Frame ####
vbox_az = QGroupBox()
vbox_az.layout = Qt.QVBoxLayout() #------------------------ Layout
vbox_az.layout.addWidget(self.FRAME_FROM_OTHER_SCRIPT)
self.main_grid = QGridLayout() #--------------------------- Layout
self.main_grid.addWidget(tabs,0,0,1,2)
self.main_grid.addWidget(vbox_az,0,2,1,3)
self.main_grid.setColumnStretch(2,1)
self.main_grid.setColumnStretch(5,1)
self.main_window.setLayout(self.main_grid)
I am unsure of what is actually happening... are widgets unable to be nested within other widgets?
Because setLayout relies on a layout input and not a widget I am unsure of how else to attack this problem other than by grouping my "sub-widgets" into groupboxes but I do not know why it's going so wrong.
Here is a picture screen-grab to illustrate further:
(for the sake of my question I have asked to split my window into two portions though I am actually splitting it three times)
I'm taking the first steps to move from .NET to Python but I'm already having a few headaches regarding the GUI design.
For some reason, passing the size attribute to a wx.Button seems to be kind of ignored. And I say "kind of" because the actual space seems to change but the actual button keeps occupying the same space:
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.SetSize((800, 600))
self.SetTitle('Main Menu')
self.Centre()
self.Show(True)
''' Fill the form '''
self.lblUsername = wx.StaticText(self, size=(80, -1), pos=(20,20), label="Username:" )
self.txtUsername = wx.TextCtrl(self, size=(140, -1), pos=(100,20), style=wx.TE_PROCESS_ENTER)
self.lblPassword = wx.StaticText(self, size=(80, -1), pos=(20,50), label="Password:" )
self.txtPassword = wx.TextCtrl(self, size=(140, -1), pos=(100,50), style=wx.TE_PROCESS_ENTER)
self.btnOK = wx.Button( self, label="OK", pos=(260, 16), size=(50,50))
self.btnOK.Bind(wx.EVT_BUTTON, self.onClickOK)
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText('Ready')
def onClickOK(self, e):
print "Button triggered"
def main():
ex = wx.App()
Example(None)
ex.MainLoop()
if __name__ == '__main__':
main()
No matter what size I set, the Button won't stretch (it will be centered as if all the space was actually being used, but will still be small).
Can anyone spot what am I doing wrong?
This is a limit imposed by OSX. The way the native button widget is drawn only allows it to be stretched horizontally, and the vertical size is fixed. Or rather, as you've discovered, the widget itself can be larger than normal vertically, but it will only draw itself at a fixed height within that space. It seems less neccessary with modern versions of OSX, but if you look at buttons in OSX from a few years ago you can probably see why this is so. The esthetic graphical effect of the "tic-tack" or "capsule" buttons would be totally ruined if they were a non-standard vertical size, causing the images used to draw the buttons to be stretched. wxWidgets follows the native plaform look and feel standards where possible, in this case it happens that Apple's standard is imposed upon us and wx can't offer the same level of flexibility that it usually does.
You do have some options however if you really want taller than normal buttons. The native widgets have a few different standard sizes, which you can select using the SetWindowVariant method, although I don't think the variants would get as tall as you want. Or you could use a generic button widget instead of a native one, such as wx.lib.buttons.ThemedGenButton.
Same problem in my little Software EventSoundControl.
Just a workaround: Use a multiline label and sizes of wxButton will work as desired!
If you want the button to stretch when you resize the frame, then you cannot use static sizes and positioning. You will need to put your widgets in a sizer. Then the sizer will manage the position / size of the widget(s) as you change the size of the frame. There are many examples on the wxPython wiki that demonstrate how to use sizers. You might also find the following tutorial helpful:
http://zetcode.com/wxpython/layout/
I am trying to add a feature to a calculator i have made with wxpython, i want there to be a button , that when clicked changes the background colour (the panel). To show you my code i have made a smaller program, that should only change colour, and even in this one i get the same outcome:
the background colour doesn't change, nothing happens when i click the button, and i dont even receive any errormessahe.Actually, the calculator does change colour, but not in the way i want it to, it only changes the colour of the text (a wx.StaticText), and it's not really meant to do that.
Anyway, here is the code :
import wx
class calc(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,"Calculator",size=(400,400))
global panel
panel=wx.Panel(self)
a=wx.Button(panel,label="GO",pos=(100,100),size=(50,50))
self.Bind(wx.EVT_BUTTON, self.change, a)
def change(self,event):
panel.SetBackgroundColour("red")
if __name__=="__main__":
app=wx.App(False)
frame=calc(parent=None,id=-1)
frame.Show()
app.MainLoop()
when i run this, the frame with the button show up, and when i click on the button, nothing happens!! Does anybody know what is wrong with this?
Thanks in advice!!!
While your code worked for me on Xubuntu 14.04, wxPython 2.8.12 and Python 2.7, I went ahead and rewrote it slightly to remove the global and clean it up a bit:
import wx
class calc(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Calculator", size=(400,400))
self.panel = wx.Panel(self)
a = wx.Button(self.panel, label="GO", pos=(100,100), size=(50,50))
self.Bind(wx.EVT_BUTTON, self.change, a)
def change(self,event):
self.panel.SetBackgroundColour("red")
self.Refresh() # for windows
if __name__=="__main__":
app = wx.App(False)
frame = calc()
frame.Show()
app.MainLoop()
This also works for me.
I am using wxPython to make a gui. Currently I have a menubar, and three panels. I want to have a grid show up in the second panel when I click a button. However. When I click on the button, all I get is a small grey rectangle.
Here is the code for the button:
self.Bind(wx.EVT_BUTTON, self.OnCo, id=self.submit.GetId())
and here is the code for the "OnCo" event when the button is clicked:
def OnCo(self, e):
#to get rid of stuff that was previously in the panel
for child in self.panel2.GetChildren():
child.Destroy()
for child in self.panel3.GetChildren():
child.Destroy()
mygrid = gridlib.Grid(self.panel2, -1)
mygrid.CreateGrid(500,7)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(mygrid, -1, wx.EXPAND)
self.panel2.SetSizer(sizer)
mygrid.SetColLabelValue(0, 'S')
mygrid.SetColLabelValue(1, 'PB')
mygrid.SetColLabelValue(2, 'P')
mygrid.SetColLabelValue(3, 'T')
mygrid.SetColLabelValue(4, 'D')
Any help on how I can get my grid to show? Thanks.
It's possible that the grid is not sizing correctly; your items may be there, but it's not showing everything. After changing values in a grid, I always make sure to update the sizing of it. I usually just add a simple function to the class like this:
def SetGridSize(self):
self.mygrid.AutoSizeRows()
self.mygrid.AutoSizeColumns()
self.sizer.Fit(self)
and then call SetGridSize() whenever I change the values to make sure the whole thing shows on the screen instead of getting cut off.
Of course, you'll have to adapt it a bit to your names and whatnot. This implementation assumes the class is a wx.Frame object.
I have a label that I want right aligned and the text to be right aligned. But when my code runs through and the label updates it, the StaticText aligns to the left of a button object. My code is below
hbox14 = wx.BoxSizer(wx.HORIZONTAL)
self.buttonRemove = wx.Button(self.panel,label='Remove')
self.buttonRemove.Bind(wx.EVT_BUTTON,self.removeAccount) # Remove account from list
self.labelSecTic = wx.StaticText(self.panel,label='0.0',style=wx.TE_RIGHT|wx.EXPAND)
self.labelSecTic.SetForegroundColour('white')
self.labelSecTic.SetBackgroundColour('black')
hbox14.Add(self.buttonRemove,proportion=0)
hbox14.Add(self.labelSecTic,proportion=1,flag=wx.ALIGN_RIGHT|wx.TE_RIGHT|wx.EXPAND)
When the label is updated I call
self.gui.labelSecTic.SetLabel(str(self.diff))
Any suggestions on how to make the labelSecTic stay fixed to the right side of the panel? Thanks.
First a side note: the style wx.TE_RIGHT is for wx.TextCtrl, it probably does nothing with the static text. About your real issue, you should force layout of the hbox14 sizer. Not sure what is the sizer/panel structure of your window, you should call Layout on some ancestor of hbox14, it might be self.gui.panel or even self.gui (don't know what gui is), so for example:
self.gui.labelSecTic.SetLabel(str(self.diff))
self.gui.Layout()
or
self.gui.labelSecTic.SetLabel(str(self.diff))
self.gui.panel.Layout()