[英]How to find an item in a panel
I am using Python WX to make a large GUI containing maybe 100 CheckBoxes. 我正在使用Python WX制作一个包含100个CheckBox的大型GUI。 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.
我可以使用100行代码执行此操作,但更喜欢使用循环。 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 CheckBox
es in a loop and hold them in a list: 而不是拥有100条几乎完全相同的代码行,这些代码行容易出错,效率低且缺乏吸引力,实际上是在循环中构建
CheckBox
并将它们保存在列表中:
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. 如果你真的想要
"first"
, "second"
, "100th"
写一个辅助函数来将i
处理成一个字符串表示。
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. 此代码将创建一组5个wx.Checkbox,每个都具有唯一的名称。 Then you can look them up by name using wx.FindWindowByName .
然后,您可以使用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. 您还可以使用名称作为键创建字典,将值作为CheckBox小部件创建,这样可以更快地查找。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.