Resolve a problem ComboBox and a TextCtrl [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to make that when selecting an element in the ComboBox print a message that is in the tuple in the TextCtrl, depending on the item I chose
When I did it the way I investigated, I throw an error.
import wx
#Mods
Nom_Mods = ["Alca v3", "Barcelone v1", "Anti-Fed (French)", "Elegance v3"]
Info_Mods = ["(ZL + Joystick2) To Open Menu\n(B) To close Menu\nCreate by KillerGamer81"]
#EndMods
class PageOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
sz = wx.BoxSizer(wx.VERTICAL)
#Controls
self.Listade_Menus = wx.ComboBox(self, -1, pos=(10,80), size=(173,22), choices = Nom_Mods, style= wx.CB_READONLY)
Cuadro_de_info = wx.TextCtrl(self, -1, "", pos=(200,80), size=(175,80), style = wx.TE_MULTILINE|wx.TE_NO_VSCROLL|wx.TE_READONLY)
class MainFrame(wx.Frame):
def __init__(self):
no_sys_menu = wx.DEFAULT_FRAME_STYLE & (~wx.RESIZE_BORDER) & (~wx.MAXIMIZE_BOX)
wx.Frame.__init__(self, None, title="ProyectoU", style=no_sys_menu, size=(400,225))
ico = wx.Icon('Recursos/icono.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(ico)
# Here we create a panel and a notebook on the panel
p = wx.Panel(self)
nb = wx.Notebook(p)
# create the page windows as children of the notebook
page1 = PageOne(nb)
# add the pages to the notebook with the label to show on the tab
nb.AddPage(page1, "Inyec/Conec")
# finally, put the notebook in a sizer for the panel to manage
# the layout
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()

The first error, would be a indentation error!
The problem is that you are not Binding to the ComboBox event i.e. when you make a selection an event will fire, which must be caught and acted upon.
You need to catch that event and put the currently selected text from the combobox (or whatever) into the textctrl. Currently you are making no attempt to do that.
Here is what I assume you want.
import wx
#Mods
Nom_Mods = ["Alca v3", "Barcelone v1", "Anti-Fed (French)", "Elegance v3"]
Info_Mods = ["(ZL + Joystick2) To Open Menu\n(B) To close Menu\nCreate by KillerGamer81"]
#EndMods
class PageOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
sz = wx.BoxSizer(wx.VERTICAL)
#Controls
self.Listade_Menus = wx.ComboBox(self, -1, pos=(10,80), size=(173,22), choices = Nom_Mods, style= wx.CB_READONLY)
#Bind a callback to the event emitted by the Combobox selection
self.Listade_Menus.Bind(wx.EVT_COMBOBOX, self.Nom_Mods_Selected)
self.Cuadro_de_info = wx.TextCtrl(self, -1, "", pos=(200,80), size=(175,80), style = wx.TE_MULTILINE|wx.TE_NO_VSCROLL|wx.TE_READONLY)
# When a selection is made populate the textctrl with the selected text
def Nom_Mods_Selected(self, event):
self.Cuadro_de_info.SetValue(self.Listade_Menus.GetStringSelection())
class MainFrame(wx.Frame):
def __init__(self):
no_sys_menu = wx.DEFAULT_FRAME_STYLE & (~wx.RESIZE_BORDER) & (~wx.MAXIMIZE_BOX)
wx.Frame.__init__(self, None, title="ProyectoU", style=no_sys_menu, size=(400,225))
ico = wx.Icon('Recursos/icono.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(ico)
# Here we create a panel and a notebook on the panel
p = wx.Panel(self)
nb = wx.Notebook(p)
# create the page windows as children of the notebook
page1 = PageOne(nb)
# add the pages to the notebook with the label to show on the tab
nb.AddPage(page1, "Inyec/Conec")
# finally, put the notebook in a sizer for the panel to manage
# the layout
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()

Related

wxPython : Issues on windows using multiple sizers, but works well on linux

I am writing a small app that works very well on linux, but I have some trouble on windows. Here is the code sample:
import wx
#####################################################################
class Main(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="StackOverflow", pos=wx.DefaultPosition, size=(800,600))
self.SetMinSize( self.GetSize() )
p = wx.Panel(self)
nb = wx.Notebook(p)
page1 = AddToCollection(nb)
page2 = CollectionStatistics(nb)
nb.AddPage(page1, "Page 1")
nb.AddPage(page2, "Page 2")
# finally, put the notebook in a sizer for the panel to manage
# the layout
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
#########################################################################
class CollectionStatistics(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
#########################################################################
class AddToCollection(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.v1_qty_list = [str(x) for x in range(9)]
self.v2_qty_list = [str(x) for x in range(9)]
self.sizername = wx.GridBagSizer(5, 5)
self.sizername.AddGrowableCol(0,0)
self.name_txt = wx.StaticText(self, label="Enter Name :")
self.sizername.Add(self.name_txt,(2,0),(1,1),wx.EXPAND)
self.name = wx.TextCtrl(self,style=wx.TE_PROCESS_ENTER,value=u"")
self.sizername.Add(self.name,(3,0),(1,1),wx.EXPAND)
self.Bind(wx.EVT_TEXT_ENTER, self.OnPressEnter, self.name)
self.SetSizerAndFit(self.sizername)
self.SetSizeHints(-1,self.GetSize().y,-1,self.GetSize().y )
##########################################################################
def OnPressEnter(self,event):
self.selected_name = self.name.GetValue()
self.AddToCol()
##########################################################################
def AddToCol(self):
self.sizerAdd = wx.GridBagSizer(5, 5)
self.sizerAdd.AddGrowableCol(0, 0)
self.name.Enable(False)
### Expansion
self.expansion = wx.Choice(self, -1, choices=['test 1', 'test 2'])
self.expansion.SetSelection(0)
self.sizerAdd.Add(self.expansion,(5,0),(1,6),wx.EXPAND)
### Quantities txt
self.v1_txt = wx.StaticText(self, label="V1 Quantity :")
self.sizerAdd.Add(self.v1_txt,(7,0),(1,1),wx.EXPAND)
self.v2_txt = wx.StaticText(self, label="V2 Quantity :")
self.sizerAdd.Add(self.v2_txt,(8,0),(1,1),wx.EXPAND)
### Quantities choices
self.v1_qty = wx.Choice(self, -1, choices=self.v1_qty_list)
self.v1_qty.SetSelection(0)
self.sizerAdd.Add(self.v1_qty,(7,5),(1,1),wx.EXPAND)
self.v2_qty = wx.Choice(self, -1, choices=self.v1_qty_list)
self.v2_qty.SetSelection(0)
self.sizerAdd.Add(self.v2_qty,(8,5),(1,1),wx.EXPAND)
### Ok Button
self.Add_btn = wx.Button(self, -1, "Add")
self.Add_btn.Bind(wx.EVT_BUTTON, self.OnAdd)
self.sizerAdd.Add(self.Add_btn,(9,5),(1,1),wx.EXPAND)
### Reset Button
self.Reset_btn = wx.Button(self, -1, "Reset")
self.Reset_btn.Bind(wx.EVT_BUTTON, self.OnResetPanel)
self.sizerAdd.Add(self.Reset_btn,(9,4),(1,1),wx.EXPAND)
self.SetSizerAndFit(self.sizerAdd)
self.SetSizeHints(-1,self.GetSize().y,-1,self.GetSize().y )
######################################################################
def OnResetPanel(self,event):
### Kill all children
self.expansion.Destroy()
self.v1_txt.Destroy()
self.v1_qty.Destroy()
self.v2_txt.Destroy()
self.v2_qty.Destroy()
self.Add_btn.Destroy()
self.Reset_btn.Destroy()
### Reinitialise sizer
self.name.Enable(True)
self.name.SetValue("")
######################################################################
def OnAdd(self,event):
print 'Add'
self.OnResetPanel(self)
######################################################################
######################################################################
if __name__ == "__main__":
app = wx.App()
Main().Show()
app.MainLoop()
Basically, I have a TextCtrl in a first sizer which is waiting for an entry. Once the user hits enter, several objects appear in a second sizer.
The issue on windows seems to come from the use of the two gridbagsizers (sizername and sizerAdd). After pressing enter (waited event in the __init__), the objects defined within the sizerAdd do not appear. When I extend the window where the script is running, these objects appear magically !
Any idea ?
EDIT : The code is now runnable
I think the problem in your code is these two lines at the end of your AddToCol method:
self.SetSizerAndFit(self.sizerAdd)
self.SetSizeHints(-1,self.GetSize().y,-1,self.GetSize().y )
At this point, you're changing the sizer of the AddToCollection panel from self.sizername to self.sizerAdd. The Enter Name: label and the textbox however are still within the self.sizername sizer. However, this sizer isn't the sizer for any window, nor has it been added to any other sizer.
Generally, in wxPython, every sizer should be set as the sizer for a window, or be added to another sizer. This other sizer would then be the sizer for a window, or be contained within another sizer, and so on. In your case, your self.sizername sizer ends up being neither, and in this situation I would expect unpredictable behaviour. If your code works on Linux then I would say that it happens to work by accident.
I can think of a few things you could do here:
Add self.sizerAdd as a child of self.sizername. This can be done by replacing the two lines above with
self.sizername.Add(self.sizerAdd,(4,0),(1,1),wx.EXPAND)
self.sizername.Layout()
In AddToCol, add the widgets directly to the self.sizername sizer instead of adding them to self.sizerAdd.
Create a wx.BoxSizer() with vertical orientation, set that to be the sizer for the AddToCollection panel, and add the self.sizername and self.sizerAdd sizers to your BoxSizer.
In all three cases, after creating the new widgets you will need to call the Layout() method on the top-level sizer, be it either self.sizername or the top-level BoxSizer. The code snippet under option 1 includes this line already.
Additionally, you may need to modify your OnResetPanel() method. If you chose options 1 or 3, you will need to remove the self.sizerAdd sizer from whichever sizer you added it to. For example, in option 1, you would add the line
self.sizername.Remove(self.sizerAdd)
Another approach would be for your AddToCol method to create all the widgets within a Panel and add that to the main panel at the end. Your AddToCol method would then need to create a child panel, add the extra controls as children of this panel instead of the main panel (self), set the sizer of the child panel to self.sizerAdd and finally add this panel to the self.sizername sizer.
def AddToCol(self):
self.sizerAdd = wx.GridBagSizer(5, 5)
self.sizerAdd.AddGrowableCol(0, 0)
self.name.Enable(False)
self.child_panel = wx.Panel(self)
### Expansion
self.expansion = wx.Choice(self.child_panel, -1, choices=['test 1', 'test 2'])
self.expansion.SetSelection(0)
self.sizerAdd.Add(self.expansion,(5,0),(1,6),wx.EXPAND)
# Create other widgets as before but with the parent set to self.child_panel
# instead of self.
self.child_panel.SetSizer(self.sizerAdd)
self.sizername.Add(self.child_panel,(4,0),(1,1),wx.EXPAND)
self.sizername.Layout()
You would then also need to replace the line
self.sizername.Remove(self.sizerAdd)
in OnResetPanel() with the two lines:
self.sizername.Remove(self.child_panel)
self.child_panel.Destroy()
One thing which bugged me about my approach 1 above was that I saw the widgets briefly appear in the top-left corner before appearing in the correct place. This adaptation fixes this problem and so makes the GUI behave itself a bit better. I couldn't reproduce your black area issue you mention in your comment, but hopefully this approach fixes your problem as well.

Can't go back to wxpython main frame after creating and destroying Dialogue using ShowModal via Pubsub

I am programming an application using wxpython and wx.lib.pubsub. in python 2.7.3
1- There is a Frame with a menu item. When this menu is clicked, a message is published by pubsub.
2- This message destroys (if possible) and creates a "first level" dialogue.
3- "First Level" dialogue has an list of valules and an "add value" button. (NOTE: Such list of variables can be modified so I am trying to update this list)
4- When the "add value" button is clicked, another message is published by pubsub.
5- This message creates a "Second Level" dialogue, so a new name for the new variable can be written.
6- There is a "continue" button in this "second level" dialogue which has two consequences:
First one: Self.Destroy();
Second one: goes to step 2, i.e. destroys the "first level" dialogue and creates it again.
To that point the program seems to work fine, however, when I finish "adding" variables to the "first level" dialogue I Destroy it and then I cannot go back to the main Frame stated in step 1.
Why is this happening?
All the Dialogues are shown via ShowModal(). However if I use only Show() it seems to work fine but, since the program has many menus and items, ShowModal() is preferred.
Any idea why it works with Show() but not with ShowModal()?
If there is a simpler way to perform the task I want to do, it would be appreciated.
import wx
from wx.lib.pubsub import Publisher as pub
class itemReceiver(object):
def __init__(self):
pub.subscribe(self.__OnShowDialog, 'show.dialog')
def __OnShowDialog(self, message):
self.dlgParent = message.data[0]
print str(self.dlgParent)
self.valuesToShow = message.data[1]
print self.valuesToShow
#try to destroy dialog before creating a new one
try:
self.manageParametersDialog.Destroy()
except:
pass
self.manageParametersDialog = manageParamsDialog(self.dlgParent, self.valuesToShow)
print "ready to show first level dialogue"
self.manageParametersDialog.ShowModal() #if .Show() instead, there is no problem
class secondaryReceiver(object):
def __init__(self):
pub.subscribe(self.__OnShowDialog, 'add.item')
def __OnShowDialog(self, message):
dlgParent = message.data[0]
dlgGrandParent = message.data[1]
self.variableList = message.data[2]
editParameterDialog = editParamDlg(dlgParent, dlgGrandParent, self.variableList)
editParameterDialog.ShowModal()
class manageParamsDialog (wx.Dialog):
def __init__(self, parent, valueList):
self.valueList = valueList
self.parent = parent
wx.Dialog.__init__(self, parent, -1, "first level dialogue", style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
sizer=wx.BoxSizer(wx.VERTICAL)
self.optionList = wx.ListBox(self, -1, size=(200, 70), choices = valueList)
sizer.Add(self.optionList)
addButton = wx.Button(self, -1, 'Add New')
self.Bind(wx.EVT_BUTTON, self.OnButton, addButton)
sizer.Add(addButton)
cancelButton = wx.Button(self, -1, 'Cancel')
self.Bind(wx.EVT_BUTTON, self.OnCancel, cancelButton)
sizer.Add(cancelButton)
self.SetSizer(sizer)
self.Fit()
def OnButton (self, e):
pub.sendMessage('add.item', [self, self.parent, self.valueList])
def OnCancel(self,e):
self.Destroy()
class editParamDlg(wx.Dialog):
def __init__(self, parent, grandParent, variableList):
self.variableList = variableList
self.grandParent = grandParent
wx.Dialog.__init__(self, parent, -1, "second level dialogue", style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
hboxSizer = wx.BoxSizer(wx.HORIZONTAL)
self.textInput = wx.TextCtrl(self, -1)
hboxSizer.Add(self.textInput)
addButton = wx.Button(self, -1, 'Continue')
self.Bind(wx.EVT_BUTTON, self.OnAdd, addButton)
hboxSizer.Add(addButton)
cancelButton = wx.Button(self, -1, 'Cancel')
self.Bind(wx.EVT_BUTTON, self.OnCancel, cancelButton)
hboxSizer.Add(cancelButton)
self.SetSizer(hboxSizer)
self.Fit()
def OnAdd(self, e):
self.variableList.append(self.textInput.GetValue())
self.Destroy()
pub.sendMessage('show.dialog',[self.grandParent, self.variableList])
def OnCancel(self,e):
self.Destroy()
class ToolbarFrame(wx.Frame):
#this ToolbarFrame is the main window, with a Toolbar and a white panel below.
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, "this is a frame", size=(480, 320))
myPanel = wx.Panel(self)
myPanel.SetBackgroundColour("White")
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
menuItem = wx.MenuItem(fileMenu, -1, "menu item", "opens dialog via pubsub")
self.Bind(wx.EVT_MENU, self.OnMenuItem, menuItem)
fileMenu.AppendItem(menuItem)
menuBar.Append(fileMenu, "File")
self.SetMenuBar(menuBar)
def OnMenuItem(self, e):
pub.sendMessage('show.dialog', [self, ["one", "two", "three"]])
app = wx.PySimpleApp()
frame = ToolbarFrame(parent=None, id=-1)
frame.Show()
newItemListener = itemReceiver()
editParameterListener = secondaryReceiver()
app.MainLoop()
try changing secondaryReciever as follows
class secondaryReceiver(object):
def __init__(self):
pub.subscribe(self.__OnShowDialog, 'add.item')
def __OnShowDialog(self, message):
dlgParent = message.data[0]
dlgGrandParent = message.data[1]
self.variableList = message.data[2]
editParameterDialog = editParamDlg(dlgParent, dlgGrandParent, self.variableList)
editParameterDialog.ShowModal()
#this line will not execute till the dialog closes
self.dlgParent.optionList.SetItems(editParameterDialog.variableList)
editParameterDialog.Destroy()
and also change editParamDlg
def OnAdd(self, e):
self.variableList.append(self.textInput.GetValue())
self.Close()
the problem was that you would call the show.modal from that OnAdd ... which would try to destroy the existing window and then open a new one... but the old one wasnt destroyed ... this left weird remnants that caused you errors ... and really all you want to do is update the item list ...

wx.CollapsiblePane in a wx.Notebook resizing

I am trying to create a wx.CollapsiblePane on a wx.Notebook page which is successfully done using the minimal code example below. However, when the pane is unfolded, the frame fails to resize. However, if instead of creating a Notebook I make a simple Panel (see the commented line in the code), the frame does resize as expected. Any ideas why it doesn't work on a Notebook?
EDIT: I had a chance to test it on Windows and it works (wx 3.0.0.0 and 2.8). There only seems to be a problem in Linux (Ubuntu 13.10 with wx 2.8.12.1).
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.panel = wx.Panel(self)
# Notebook setup
self.nb = wx.Notebook(self.panel)
#self.nb = wx.Panel(self.panel) # uncomment this for a working example
self.pagepanel = wx.Panel(self.nb)
self.nb.AddPage(self.pagepanel, 'page')
# add something to the Notebook page
checkbox = wx.CheckBox(self.pagepanel, -1, 'box 1')
# add CollapsiblePane to the Notebook page
colpane = wx.CollapsiblePane(self.pagepanel)
colpane.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnPaneChanged)
# add something to the CollapsiblePane
win = colpane.GetPane()
checkbox2 = wx.CheckBox(win, -1, 'box 2')
# Set CollapsiblePane sizer
colpanesizer = wx.BoxSizer(wx.VERTICAL)
colpanesizer.Add(checkbox2, -1)
win.SetSizer(colpanesizer)
colpanesizer.SetSizeHints(win)
colpane.Collapse()
# set Notebook page sizer
pagesizer = wx.BoxSizer(wx.VERTICAL)
pagesizer.Add(checkbox)
pagesizer.Add(colpane)
self.pagepanel.SetSizerAndFit(pagesizer)
# set the whole Panel sizer
panelsizer = wx.BoxSizer()
panelsizer.Add(self.nb)
self.panel.Layout()
self.panel.SetSizerAndFit(panelsizer)
# set Frame size
self.Fit()
def OnPaneChanged(self, event):
# redo the layout
self.pagepanel.Layout()
self.pagepanel.Fit()
self.panel.Layout()
self.panel.Fit()
self.Fit()
app = wx.App()
frame = MyFrame()
frame.Center()
frame.Show()
app.MainLoop()
colpanesizer.Add(checkbox2, -1) should be colpanesizer.Add(checkbox2)
Try:
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.panel = wx.Panel(self)
# Notebook setup
self.nb = wx.Notebook(self.panel)
#self.nb = wx.Panel(self.panel) # uncomment this for a working example
self.pagepanel = wx.Panel(self.nb)
self.nb.AddPage(self.pagepanel, 'page')
# add something to the Notebook page
checkbox = wx.CheckBox(self.pagepanel, -1, 'box 1')
# add CollapsiblePane to the Notebook page
colpane = wx.CollapsiblePane(self.pagepanel)
colpane.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnPaneChanged)
# add something to the CollapsiblePane
win = colpane.GetPane()
checkbox2 = wx.CheckBox(win, -1, 'box 2')
# Set CollapsiblePane sizer
colpanesizer = wx.BoxSizer(wx.VERTICAL)
colpanesizer.Add(checkbox2)
win.SetSizer(colpanesizer)
colpanesizer.SetSizeHints(win)
colpane.Collapse()
# set Notebook page sizer
pagesizer = wx.BoxSizer(wx.VERTICAL)
pagesizer.Add(checkbox)
pagesizer.Add(colpane)
self.pagepanel.SetSizerAndFit(pagesizer)
# set the whole Panel sizer
panelsizer = wx.BoxSizer()
panelsizer.Add(self.nb)
self.panel.Layout()
self.panel.SetSizerAndFit(panelsizer)
# set Frame size
self.Fit()
def OnPaneChanged(self, event):
# redo the layout
self.pagepanel.Layout()
self.pagepanel.Fit()
self.panel.Layout()
self.panel.Fit()
self.Fit()
app = wx.App()
frame = MyFrame()
frame.Center()
frame.Show()
app.MainLoop()

wxPython: TextCtrl in pop up window

I have created a pop up window, but the TextCtrl is not fully expanded to fill up the window. It works great if I use StaticText instead, (but if content too large then I would need the scroll bar, that is why I am using TextCtrl now). Please provide some guidance.
self.description = WindowPopup(self, wx.SIMPLE_BORDER, content)
btn = event.GetEventObject()
dw = wx.DisplaySize()[0]
width = self.description.GetSize()[0]
y = btn.ClientToScreen((0,0))[1]
height = btn.GetSize()[1]
x = dw - width - 20 - 10
self.description.Position((x, y), (0, height))
self.description.Show(True)
class WindowPopup(wx.PopupWindow):
""" Pops up a window to provide description for the selection """
def __init__(self, parent, style, content):
wx.PopupWindow.__init__(self, parent, style)
self.SetSize((700, 287))
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
st = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE | wx.TE_READONLY)
st.SetValue(content)
sizer.Add(st, 0, wx.EXPAND)
panel.SetSizer(sizer)
I suspect your problem is that the panel is not as big as the popupwindow ... so even though the textfield is expanding to fill its sizer area it is not filling the popup its self.
try using something like
def __init__(...):
...
self.SetMinSize((700,287))
sizer2 = wx.BoxSizer()
sizer2.Add(panel)
self.SetSizer(sizer2)
also make sure that you are calling layout on it at some point (note this is totally untested... so it may need some tweeks, or even worse just be wrong...)
The actual answer is:
sizer = wx.BoxSizer(wx.VERTICAL)
st = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE | wx.TE_READONLY, size = (500, 174))
st.SetValue(content)
self.SetSize((500, 174))
sizer.Add(st, 0, wx.EXPAND)
self.SetSizer(sizer)
self.Layout()
self.Show(True)
Credits to Joran for noticing Layout().
PopupWindow does not require an additional panel, because the window itself can have sizer set to it. This has been realized by using the wxPython Widget Inspection Tool.
Make sure TextCtrl and PopupWindow have the same size.

Passing a tuple between two Panels in wxPython

I am trying to pass the tuple of the the checked strings in one panel to the textCtrl widget in another panel. I thought that if I polled the panel class containing the checklistbox widget for the checked boxes and set that equal to an attribute of the panel class containing the TextCtrl widget I could do things in the TextCtrl widget based on the boxes that are checked
I also tried using pubsub but I was having difficulty and wasn't sure if this was the case that you would use it or not.
As a disclaimer I am new to object oriented programming and python. I am making progress in learning object oriented programming but there are still things that are a bit fuzzy for me.
Here is my code:
#! /usr/bin/python
# Trying to do things in a separate panel based on
# events that happen in another panel
import wx
# ----- Functions
# ----- Classes
class chkbxPanel(wx.Panel):
# This class defines the panel that will
# contain the checkbox
def __init__(self, parent):
wx.Panel.__init__(self,parent=parent, id=-1)
# Widgets
List = ['one','two','three']
Prompt = 'Please Make a Choice'
self.ChkBx = wx.CheckListBox(self,id=-1,choices=List,size=(-1,200))
self.PromptChkBx = wx.StaticText(self,id=-1,label=Prompt)
# Page Sizers
self.panel_vertSizer=wx.BoxSizer(wx.VERTICAL)
self.panel_vertSizer.Add(self.PromptChkBx,proportion=0,flag=wx.ALIGN_LEFT)
self.panel_vertSizer.Add(self.ChkBx,proportion=0,flag=wx.ALIGN_LEFT)
# Invoke Sizer
self.SetSizer(self.panel_vertSizer)
# Make 'self' (the panel) shrink to the minimum size
# required by the controls
self.Fit()
# end __init__
# ----- Functions
class resultsPanel(wx.Panel):
# This class define the panel that will
# contain the textctrl
def __init__ (self,parent):
wx.Panel.__init__(self,parent=parent,id=-1)
# Widgets
ResultsPanelTitle = 'Results:'
self.ResultsTitle = wx.StaticText(self, id=-1, label= ResultsPanelTitle)
self.Results = wx.TextCtrl(self, id=-1,size=(300,500),style=(wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP))
self.CheckedBxs = ()
lenofcb = len(self.CheckedBxs)
typeofcb = type(self.CheckedBxs)
self.Results.AppendText('How Many Boxes are Checkd:\n')
self.Results.AppendText(str(lenofcb)+'\n')
self.Results.AppendText(str(typeofcb)+'\n')
# Page Sizer
self.panel_vertSizer = wx.BoxSizer(wx.VERTICAL)
self.panel_vertSizer.Add(self.ResultsTitle,proportion=0, flag=wx.ALIGN_LEFT)
self.panel_vertSizer.Add(self.Results, proportion=0, flag=wx.ALIGN_LEFT)
# Invoke Sizer
self.SetSizer(self.panel_vertSizer)
# Make 'self' (the panel) shrink to the minimum size
# required by the controls
self.Fit()
# end __init__
# ----- Functions
# ----- Main Frame
class MainFrame(wx.Frame):
# A 2-Control Class With BoxSizers
def __init__(self):
# Configure the Figure
titleText = 'Wx Question'
wx.Frame.__init__( self, None, title=titleText
,size=(600,300), style=wx.DEFAULT_FRAME_STYLE)
# First Frame Control automatically expands to the
# Frame's client size
frame_panel = wx.Panel(self)
# Create the Controls
LeftPanel = chkbxPanel(frame_panel)
RightPanel = resultsPanel(frame_panel)
RightPanel.CheckedBxs = LeftPanel.ChkBx.GetCheckedStrings()
RightPanel.CheckedBxs = ('one','two')
# Create Sizers and add the controls
panelCtrls_horzSizer = wx.BoxSizer(wx.HORIZONTAL)
panelCtrls_horzSizer.Add(LeftPanel)
panelCtrls_horzSizer.Add(RightPanel)
framePanel_vertSizer = wx.BoxSizer(wx.VERTICAL)
framePanel_vertSizer.Add(panelCtrls_horzSizer)
frame_panel.SetSizer(framePanel_vertSizer)
frame_panel.Fit()
self.SetSize((600,600))
# Main if statement
if __name__ == '__main__':
app = wx.PySimpleApp(redirect=False)
appFrame = MainFrame().Show()
app.MainLoop()
Thank you for any help
You could do your event binding in the parent class as it has access to both panels
#! /usr/bin/python
# Trying to do things in a separate panel based on
# events that happen in another panel
import wx
# ----- Functions
# ----- Classes
class ChkbxPanel(wx.Panel):
# This class defines the panel that will
# contain the checkbox
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=-1)
# Widgets
choices = ['one', 'two', 'three']
prompt = 'Please Make a Choice'
self.chkBx = wx.CheckListBox(self, choices=choices, size=(-1, 200))
self.PromptChkBx = wx.StaticText(self, label=prompt)
# Page Sizers
self.panel_vertSizer = wx.BoxSizer(wx.VERTICAL)
self.panel_vertSizer.Add(self.PromptChkBx, proportion=0,
flag=wx.ALIGN_LEFT)
self.panel_vertSizer.Add(self.chkBx, proportion=0, flag=wx.ALIGN_LEFT)
# Invoke Sizer
self.SetSizer(self.panel_vertSizer)
# Make 'self' (the panel) shrink to the minimum size
# required by the controls
self.Fit()
# end __init__
# ----- Functions
class ResultsPanel(wx.Panel):
# This class define the panel that will
# contain the textctrl
def __init__ (self, parent):
wx.Panel.__init__(self, parent=parent, id=-1)
# Widgets
resultsPanelTitle = 'Results:'
self.resultsTitle = wx.StaticText(self, label=resultsPanelTitle)
self.results = wx.TextCtrl(self, size=(300, 500),
style=(wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_DONTWRAP))
self.CheckedBxs = ()
lenofcb = len(self.CheckedBxs)
typeofcb = type(self.CheckedBxs)
self.results.AppendText('How Many Boxes are Checkd:\n')
self.results.AppendText(str(lenofcb) + '\n')
self.results.AppendText(str(typeofcb) + '\n')
# Page Sizer
self.panel_vertSizer = wx.BoxSizer(wx.VERTICAL)
self.panel_vertSizer.Add(self.resultsTitle, proportion=0, flag=wx.ALIGN_LEFT)
self.panel_vertSizer.Add(self.results, proportion=0, flag=wx.ALIGN_LEFT)
# Invoke Sizer
self.SetSizer(self.panel_vertSizer)
# Make 'self' (the panel) shrink to the minimum size
# required by the controls
self.Fit()
# end __init__
# ----- Functions
# ----- Main Frame
class MainFrame(wx.Frame):
# A 2-Control Class With BoxSizers
def __init__(self):
# Configure the Figure
titleText = 'Wx Question'
wx.Frame.__init__(self, None, title=titleText
, size=(600, 300), style=wx.DEFAULT_FRAME_STYLE)
# First Frame Control automatically expands to the
# Frame's client size
frame_panel = wx.Panel(self)
# Create the Controls
leftPanel = ChkbxPanel(frame_panel)
self.rightPanel = ResultsPanel(frame_panel)
# Create Sizers and add the controls
panelCtrls_horzSizer = wx.BoxSizer(wx.HORIZONTAL)
panelCtrls_horzSizer.Add(leftPanel)
panelCtrls_horzSizer.Add(self.rightPanel)
framePanel_vertSizer = wx.BoxSizer(wx.VERTICAL)
framePanel_vertSizer.Add(panelCtrls_horzSizer)
frame_panel.SetSizer(framePanel_vertSizer)
frame_panel.Fit()
self.SetSize((600, 600))
leftPanel.chkBx.Bind(wx.EVT_CHECKLISTBOX, self.onCheckBox)
def onCheckBox(self, event):
checked = event.EventObject.CheckedStrings
self.rightPanel.results.AppendText(
'Checked: {0}, Qty checked: {1}\n'.format(checked, len(checked)))
# Main if statement
if __name__ == '__main__':
app = wx.App(redirect=False)
appFrame = MainFrame().Show()
app.MainLoop()
I removed all comments, whitespace to reduces lines. And added comments where I changed/added code. (1),(2),(3)
import wx
class chkbxPanel(wx.Panel):
def __init__(self, parent, resultsPanel):
wx.Panel.__init__(self,parent=parent, id=-1)
List = ['one','two','three']
Prompt = 'Please Make a Choice'
self.ChkBx = wx.CheckListBox(self,id=-1,choices=List,size=(-1,200))
### (1) Bind CHECKLISTBOX event (which is triggered when checkbox is toggled: checked/unchecked)
self.ChkBx.Bind(wx.EVT_CHECKLISTBOX, lambda e: resultsPanel.changed(self.ChkBx.GetCheckedStrings()))
###
self.PromptChkBx = wx.StaticText(self,id=-1,label=Prompt)
self.panel_vertSizer=wx.BoxSizer(wx.VERTICAL)
self.panel_vertSizer.Add(self.PromptChkBx,proportion=0,flag=wx.ALIGN_LEFT)
self.panel_vertSizer.Add(self.ChkBx,proportion=0,flag=wx.ALIGN_LEFT)
self.SetSizer(self.panel_vertSizer)
self.Fit()
class resultsPanel(wx.Panel):
def __init__ (self,parent):
wx.Panel.__init__(self,parent=parent,id=-1)
ResultsPanelTitle = 'Results:'
self.ResultsTitle = wx.StaticText(self, id=-1, label= ResultsPanelTitle)
self.Results = wx.TextCtrl(self, id=-1,size=(300,500),style=(wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP))
self.panel_vertSizer = wx.BoxSizer(wx.VERTICAL)
self.panel_vertSizer.Add(self.ResultsTitle,proportion=0, flag=wx.ALIGN_LEFT)
self.panel_vertSizer.Add(self.Results, proportion=0, flag=wx.ALIGN_LEFT)
self.SetSizer(self.panel_vertSizer)
self.Fit()
### (2): Called when checkbox is toggled with checkbox strings as parameter. See (1)
def changed(self, checked_strings):
self.Results.AppendText('How Many Boxes are Checkd:\n')
self.Results.AppendText(str(len(checked_strings))+'\n')
self.Results.AppendText(str(checked_strings)+'\n')
###
class MainFrame(wx.Frame):
def __init__(self):
titleText = 'Wx Question'
wx.Frame.__init__(self, None, title=titleText ,size=(600,300), style=wx.DEFAULT_FRAME_STYLE)
frame_panel = wx.Panel(self)
### (3): Changed creation order. Passed resultsPanel instance to chkbxPanel creator
RightPanel = resultsPanel(frame_panel)
LeftPanel = chkbxPanel(frame_panel, RightPanel)
###
RightPanel.CheckedBxs = LeftPanel.ChkBx.GetCheckedStrings()
RightPanel.CheckedBxs = ('one','two')
panelCtrls_horzSizer = wx.BoxSizer(wx.HORIZONTAL)
panelCtrls_horzSizer.Add(LeftPanel)
panelCtrls_horzSizer.Add(RightPanel)
framePanel_vertSizer = wx.BoxSizer(wx.VERTICAL)
framePanel_vertSizer.Add(panelCtrls_horzSizer)
frame_panel.SetSizer(framePanel_vertSizer)
frame_panel.Fit()
self.SetSize((600,600))
if __name__ == '__main__':
app = wx.PySimpleApp(redirect=False)
appFrame = MainFrame().Show()
app.MainLoop()

Categories

Resources