Create objects obj0 through objN in Python, for arbitrary N? - python

I'm working on a GUI and I'd like to create a variable number of buttons, depending on what is input by user. How should I go about this?
More precisely, the user opens a data file with a number of datasets, N. I would like to create N buttons with the ith button having the name of the ith dataset.
Something like... (conceptually, obviously this won't work)
import wx
# create sizer
hbox1=wx.GridBagSizer(4,4)
# create file button and add to sizer
fbtn = wx.Button(dv, label=file)
hbox1.Add(fbtn, pos=(jf,0))
# read file to get dataset names
dsetnames=[<dataset names for file>]
# create bottons for datasets found in file
jd=0 # datasets
for ds in dsetnames:
self.dbtn<jd> = wx.Button(dv, label=ds)
hbox1.Add(self.dbtn<jd>, pos=(jd,1))
jd+=1
Thank you.
I found a solution but I don't have the privilege yet to answer questions, so I'll add it here.
Generally, the solution here is to create a dictionary with the object names as the keys and the objects as the values.
import wx
dv=wx.Panel(self)
# create sizer
hbox1=wx.GridBagSizer(4,4)
# create file button and add to sizer
# filename file input by user
fbtn = wx.Button(dv, label=file)
hbox1.Add(fbtn, pos=(jf,0))
# read file to get dataset names
dsetnames=[<dataset names for file>]
# create empty dictionary for objects
self.dbtn=dict()
# create bottons for datasets found in file
jd=0 # datasets
for ds in dsetnames:
objname=ds
self.dbtn.update({objname:wx.Button(dv, label=ds)}) # update dictionary with individual objects
hbox1.Add(self.dbtn[objname], pos=(jd,1))
jd+=1
I believe the following may be related.
Create Hash for Arbitrary Objects?

I prefer using a list so that the items read from the file are in the correct order. Dictionaries do not have a standard order, although you could use an OrderedDict from the collections library. Anyway, here's an approach that uses a list:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
btn_list = self.getButtons()
btn_sizer = wx.BoxSizer(wx.VERTICAL)
for index, btn in enumerate(btn_list):
name = "button%i" % index
b = wx.Button(self, label=btn, name=name)
b.Bind(wx.EVT_BUTTON, self.onButton)
btn_sizer.Add(b, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(btn_sizer)
#----------------------------------------------------------------------
def getButtons(self):
"""
Here you would put code to get a list of button labels from a file
"""
return ["alpha", "bravo", "charlie"]
#----------------------------------------------------------------------
def onButton(self, event):
""""""
btn = event.GetEventObject()
print "The button's label is '%s'" % btn.GetLabel()
print "The button's name is '%s'" % btn.GetName()
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Buttons")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
You might find the following articles helpful as well:
http://www.blog.pythonlibrary.org/2012/05/05/wxpython-adding-and-removing-widgets-dynamically/
http://www.blog.pythonlibrary.org/2011/09/20/wxpython-binding-multiple-widgets-to-the-same-handler/

Related

wx.Hypertreelist - How add widgets to it? / How make texts editable?

Recently i found Hypertreelist, it seems to be what i Need, but there arent any good examples. So i have a few questions:
How do I add wx.Widgets to it? Like placing a Combobox
How do i make the Texts inside a row editable? Vie Doubleclick would be good
Here is my Code so far:
import wx
import wx.lib.agw.hypertreelist as HTL
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "HyperTreeList Demo")
tree_list = HTL.HyperTreeList(self)
tree_list.AddColumn("First column")
root = tree_list.AddRoot("Root")
parent = tree_list.AppendItem(root, "First child")
child = tree_list.AppendItem(parent, "First Grandchild")
tree_list.AppendItem(root, "Second child")
app = wx.App(0)
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
You could try it with a HitTest and EditLabel. The code could look like this:
pt = event.GetPosition()
item, flags, column = self.tree.HitTest(pt)
if "CP" in item.GetText():
if column > 1:
self.tree.EditLabel(item, column)
event.Skip()

Python TypeError: CommandEvent.GetString(): too many arguments

I am currently making a UI in python using wx. I am trying to have multiple tabs active at the same time, but at the same time I am trying to keep either a list or int value active and showing in the bottom right corner on any tab at all times. Unfortunately I seem to have run into a road block. When I run the code below I get the error:
i = parent.GetString(Points.a)
TypeError: CommandEvent.GetString(): too many arguments
Honestly I am only a year into coding practice and I don't really know what this error means. If possible, could someone please explain it to me and possible give me some tips on how to solve the issue?
Thanks in advance.
import wx
class Points:
c = 14
a = [14]
b = "{}\n" .format(a)
class MOC(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.poi = wx.StaticText(self, label=Points.b, pos=(400, 400))
select = wx.Button(self, wx.ID_ANY, '+1', size=(90, 30))
select.Bind(wx.EVT_BUTTON, self.add)
def add(self, parent):
i = parent.GetString(Points.a)
Points.a.remove(Points.c)
Points.c += 1
Points.a.append(Points.c)
self.poi.SetLabel(i)
class TOS(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
wx.StaticText(self, label=Points.b, pos=(400, 400))
class UIFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, size = (500,500), title = "Mind")
p = wx.Panel(self)
nb = wx.Notebook(p)
page1 = MOC(nb)
page2 = TOS(nb)
nb.AddPage(page1, "Means")
nb.AddPage(page2, "Types")
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
if __name__ == "__main__":
app = wx.App()
UIFrame().Show()
app.MainLoop()
In the line i = parent.GetString(Points.a) you are passing the argument Points.a but GetString has no arguments because it is used to get the string from an item i.e. self.Item.GetString().
Points.a is not a wx defined item, it is in fact a python list, to access that you should change the offending line above with
i = str(Points.a[0]) or
i = Points.a[0] or
i = Points.a or
i = str(Points.a) depending on your requirements.
Depending on which access method you choose, you may have to alter the
self.poi.SetLabel(i) as well, as i could be a list or an int rather than the required str
Running with the first option works without further changes

How to find an item in a panel

I am using Python WX to make a large GUI containing maybe 100 CheckBoxes. I want to read the value of each checkbox and append these values to a list. I can do this with 100 lines of code but prefer to use a loop. In the loop, how can I identify or select the specific checkbox I want to get the value from?
self.Box1 = wx.CheckBox(self.panel, id = 1, label='first box', pos=(10, 25), size=(30,22))
self.Box2 = wx.CheckBox(self.panel, id = 2, label='second box', pos=(20, 25), size=(30,22))
.
.
.
self.Box100 = wx.CheckBox(self.panel, id = 100, label='100th box', pos=(100, 25), size=(30,22))
Looking for something like:
MyList = []
for N in range (1, 101):
MyList.append(self.Box + N.Value)
The more generic question here is "how to select an object name in a loop"
I have searched all day with no luck. I am not a programming expert and hope this is worthy of someone's answer.
Rather than having 100 almost-identical lines of code, which is error-prone, inefficient and unattractive, actually build the CheckBoxes in a loop and hold them in a list:
self.boxes = []
for i in range(1, 101):
self.boxes.append(wx.CheckBox(self.panel, id=i,
label="Box {0}".format(i)
pos=(10, 25), size=(30,22)))
Then getting all of the values is similarly simple:
for i, box in enumerate(self.boxes, 1):
...
as is accessing a single one:
box = self.boxes[i-1]
If you really want "first", "second", "100th" write a helper function to process i into a string representation.
I personally like to use widget names. For example:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.sizer = wx.BoxSizer(wx.VERTICAL)
for i in range(5):
txt = "Checkbox #%s" % i
chk = wx.CheckBox(self, label=txt, name=txt)
self.sizer.Add(chk, 0, wx.ALL|wx.CENTER, 5)
button = wx.Button(self, label="Get check")
button.Bind(wx.EVT_BUTTON, self.onButton)
self.sizer.Add(button, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(self.sizer)
#----------------------------------------------------------------------
def onButton(self, event):
""""""
widget = self.FindWindowByName("Checkbox #0")
print widget
print widget.GetValue()
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Checkboxes")
panel = MyPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
This code will create a set of 5 wx.Checkboxes that each have a unique name. Then you can look them up by name using wx.FindWindowByName.
You could also create a dictionary using the names as the keys and the values as the CheckBox widgets which gives the advantage of being a faster lookup.

Best way to show truncated data in ObjectListView cell

One of the columns in my ObjectListView contains data that is too long to display completely, so it's automatically truncated. I would like a way to display all the data in the cell. I've already implemented the suggestion in this tutorial, but I'm not very happy with the result because the tooltip is for the entire list, not just that list item. Ideally, I'd like to have exactly what's pictured in the first image of this other question.
That post mentions that the expansion-on-hover behavior happens automatically, but I don't see any way of recreating that behavior. Perhaps it only happens on Windows? I'm running on GTK.
You could always implement an on right click pop-up window that you can populate with whatever you would like - see the wxPython Demo for Pop-up windows as a starting point.
Alternatively you could switch from ObjectListView to wx.lib.agw.UltimateListCtrl or DataViewControl - both of these seem to support tooltips that are aware of which row and column you are hovering over.
I messed around with the code in my tutorial using the answer you had mentioned and came up with the following:
import wx
from ObjectListView import ObjectListView, ColumnDefn
########################################################################
class Book(object):
"""
Model of the Book object
Contains the following attributes:
'ISBN', 'Author', 'Manufacturer', 'Title'
"""
#----------------------------------------------------------------------
def __init__(self, title, author, isbn, mfg):
self.isbn = isbn
self.author = author
self.mfg = mfg
self.title = title
########################################################################
class MainPanel(wx.Panel):
#----------------------------------------------------------------------
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.products = [Book("wxPython in Action", "Robin Dunn",
"1932394621", "Manning"),
Book("Hello World", "Warren and Carter Sande",
"1933988495", "Manning")
]
self.dataOlv = ObjectListView(self, wx.ID_ANY,
style=wx.LC_REPORT|wx.SUNKEN_BORDER)
self.dataOlv.Bind(wx.EVT_MOTION, self.updateTooltip)
self.setBooks()
# Allow the cell values to be edited when double-clicked
self.dataOlv.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK
# create an update button
updateBtn = wx.Button(self, wx.ID_ANY, "Update OLV")
updateBtn.Bind(wx.EVT_BUTTON, self.updateControl)
# Create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(self.dataOlv, 1, wx.ALL|wx.EXPAND, 5)
mainSizer.Add(updateBtn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(mainSizer)
#----------------------------------------------------------------------
def updateControl(self, event):
"""
Update the control
"""
print "updating..."
#product_dict = [{"title":"Core Python Programming", "author":"Wesley Chun",
#"isbn":"0132269937", "mfg":"Prentice Hall"},
#{"title":"Python Programming for the Absolute Beginner",
#"author":"Michael Dawson", "isbn":"1598631128",
#"mfg":"Course Technology"},
#{"title":"Learning Python", "author":"Mark Lutz",
#"isbn":"0596513984", "mfg":"O'Reilly"}
#]
product_list = [Book("Core Python Programming", "Wesley Chun",
"0132269937", "Prentice Hall"),
Book("Python Programming for the Absolute Beginner",
"Michael Dawson", "1598631128", "Course Technology"),
Book("Learning Python", "Mark Lutz", "0596513984",
"O'Reilly")
]
data = self.products + product_list
self.dataOlv.SetObjects(data)
#----------------------------------------------------------------------
def setBooks(self, data=None):
"""
Sets the book data for the OLV object
"""
self.dataOlv.SetColumns([
ColumnDefn("Title", "left", 220, "title"),
ColumnDefn("Author", "left", 200, "author"),
ColumnDefn("ISBN", "right", 100, "isbn"),
ColumnDefn("Mfg", "left", 180, "mfg")
])
self.dataOlv.SetObjects(self.products)
#----------------------------------------------------------------------
def updateTooltip(self, event):
"""
"""
pos = wx.GetMousePosition()
mouse_pos = self.dataOlv.ScreenToClient(pos)
item_index, flag = self.dataOlv.HitTest(mouse_pos)
print flag
if flag == wx.LIST_HITTEST_ONITEMLABEL:
msg = "%s is a good book!" % self.dataOlv.GetItemText(item_index)
self.dataOlv.SetToolTipString(msg)
else:
self.dataOlv.SetToolTipString("")
event.Skip()
########################################################################
class MainFrame(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,
title="ObjectListView Demo", size=(800,600))
panel = MainPanel(self)
#----------------------------------------------------------------------
def main():
"""
Run the demo
"""
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
#----------------------------------------------------------------------
if __name__ == "__main__":
main()
I also updated my tutorial to include the code above. Please note that the tooltip only updates when you mouse through the cells in the first column. I haven't found a good way around that.

How do you get checkbox selections from a CustomTreeCtrl

I'm working with a CustomTreeCtrl with checkboxes and I can't figure out how to determine which checkboxes are selected. I looked at http://xoomer.virgilio.it/infinity77/wxPython/Widgets/wx.TreeCtrl.html#GetSelection and put this together:
import string
import os
import sys
import wx
import wx.lib.agw.customtreectrl as CT
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "CustomTreeCtrl Demo")
custom_tree = CT.CustomTreeCtrl(self, agwStyle=wx.TR_DEFAULT_STYLE)
root = custom_tree.AddRoot("The Root Item")
for y in range(5):
last = custom_tree.AppendItem(root, "item %d" % y)
for z in range(5):
item = custom_tree.AppendItem(last, "item %d" % z, ct_type=1)
self.Bind(CT.EVT_TREE_ITEM_CHECKED, self.ItemChecked)
def ItemChecked(self, event):
print("Somebody checked something")
print(event.GetSelections())
app = wx.PySimpleApp()
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
When I check a box, I get the Traceback: "AttributeError: 'TreeEvent' object has no attribute 'GetSelections'" Any suggestions on how to read which boxes are selected would be great!
The event object in question doesn't have a GetSelections method. It does have a GetSelection, which will tell you which item was selected at that event. If you want to get all of the selected items inside ItemChecked, rename custom_tree to self.custom_tree and then you're allowed to call self.custom_tree.GetSelections() inside ItemChecked.
If in future you want to know what kind of methods are available for some event object, you can put print(dir(event)) in your handler.
The custom tree control doesn't have a method to get the checked items. One thing that you could do is create a self.checked_items list in your frame, and maintain it in your ItemChecked method. This list could hold either the string values for the items or the items themselves. For instance,
class MyFrame(wx.Frame):
def __init__(self, parent):
# ....
self.checked_items = []
# ....
def ItemChecked(self, event):
if event.IsChecked():
self.checked_items.append(event.GetItem())
# or to store the item's text instead, you could do ...
# self.checked_items.append(self.custom_tree.GetItemText(event.GetItem()))
else:
self.checked_items.remove(event.GetItem())
# or ...
# self.checked_items.remove(self.custom_tree.GetItemText(event.GetItem()))

Categories

Resources