我有wxPython应用程序,用户提供了一个文件作为输入,然后我的程序应该为文件中的每一行生成2个下拉列表。问题是在用户将文件作为输入后没有生成按钮,这在菜单中完成,self.content获取它的项目。我试图在用户提供文件后调用myButtons方法,但它也不起作用。
以下是按钮的简化代码:
class size:
def __init__(self, id, name):
self.id = id
self.name = name
class type:
def __init__(self, id, name):
self.id = id
self.name = name
class GUI(wx.Frame):
def __init__(self, parent, id, title):
self.dirname = ''
self.content = ''
wx.Frame.__init__(self, parent, id, title, size=(700, 400))
#UI stuff
panelControl = wx.Panel(self, 1, style=wx.MAXIMIZE)
sizerControl = wx.GridBagSizer(hgap=4,vgap = 4)
self.menu()
#buttons
if not self.content == '':
self.myButtons()
def myButtons(self):
listOfObjects = self.content #list if lists
#content of the dropdown lists
sizes = [size(0, 'very small'), size(1, 'small'), size(2, 'medium'), size(3, 'large'), size(4,'very large')]
types = [type(0, 'UI'), type(1, 'text'), type(2, 'I/O'), type(3, 'calculation'),
type(4, 'set-up'), type(5, 'logic'), type(6, 'data')]
for i in listOfObjects:
lista = [] #save the dropdown list selections
panel = wx.Panel(self, wx.ID_ANY)
self.labelthing = wx.StaticText(panel, label="Object name: %s LOC: %s Method amount: %s Mean: %s"%(i[0], i[1], i[2], i[3]))
self.sizeDrop = wx.ComboBox(panel,
size=wx.DefaultSize,
choices=lista)
self.typeDrop = wx.ComboBox(panel,
size=wx.DefaultSize,
choices=lista)
self.widgetMaker(self.sizeDrop, self.typeDrop)
self.Show()
def widgetMaker(self, widget1, widget2):
widget1.Bind(wx.EVT_COMBOBOX, self.onSelect)
widget2.Bind(wx.EVT_COMBOBOX, self.onSelect)
def onSelect(self, event):
#wait for the both dropdown list values, then do stuff
pass
def menu(self):
#do other stuff too
self.content = [['foo1', 1 , 'bar1', 1],['foo2', 1 , 'bar2', 1]]
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = GUI(None, -1, "")
frame.Show(1)
app.MainLoop()
答案 0 :(得分:1)
你有几个问题。首先,在每次迭代中覆盖循环中的小部件。该面板不会被覆盖,但组合框和静态文本都可以覆盖。因此,对前两个组合框的绑定可能无法按预期的方式工作,因为它们绑定到小部件的第二个实例。您还使用了不推荐使用的wx.PySimpleApp
。您应该使用wx.App(False)
代替。
另请注意,您的代码将无法运行,因为您从未导入wx
。
我编辑了一些代码以使其正常工作,但我没有解决所有问题:
import wx
class size:
def __init__(self, id, name):
self.id = id
self.name = name
class type:
def __init__(self, id, name):
self.id = id
self.name = name
class GUI(wx.Frame):
def __init__(self, parent, id, title):
self.dirname = ''
self.content = ''
wx.Frame.__init__(self, parent, id, title, size=(700, 400))
self.panel = wx.Panel(self)
#UI stuff
#panelControl = wx.Panel(self, 1, style=wx.MAXIMIZE)
sizerControl = wx.BoxSizer(wx.VERTICAL)
self.menu()
#buttons
if not self.content == '':
self.myButtons(sizerControl)
self.panel.SetSizer(sizerControl)
self.panel.Layout()
self.Show()
def myButtons(self, sizerControl):
listOfObjects = self.content #list if lists
#content of the dropdown lists
sizes = [size(0, 'very small'), size(1, 'small'), size(2, 'medium'), size(3, 'large'), size(4,'very large')]
types = [type(0, 'UI'), type(1, 'text'), type(2, 'I/O'), type(3, 'calculation'),
type(4, 'set-up'), type(5, 'logic'), type(6, 'data')]
for i in listOfObjects:
lista = [] #save the dropdown list selections
panel = wx.Panel(self.panel, wx.ID_ANY)
nested_sizer = wx.BoxSizer(wx.VERTICAL)
self.labelthing = wx.StaticText(panel, label="Object name: %s LOC: %s Method amount: %s Mean: %s"%(i[0], i[1], i[2], i[3]))
nested_sizer.Add(self.labelthing, 0, wx.ALL, 5)
self.sizeDrop = wx.ComboBox(panel,
size=wx.DefaultSize,
choices=lista)
nested_sizer.Add(self.sizeDrop, 0, wx.ALL, 5)
self.typeDrop = wx.ComboBox(panel,
size=wx.DefaultSize,
choices=lista)
nested_sizer.Add(self.typeDrop, 0, wx.ALL, 5)
panel.SetSizer(nested_sizer)
#self.widgetMaker(self.sizeDrop, self.typeDrop)
sizerControl.Add(panel, 1, wx.EXPAND, 5)
def widgetMaker(self, widget1, widget2):
widget1.Bind(wx.EVT_COMBOBOX, self.onSelect)
widget2.Bind(wx.EVT_COMBOBOX, self.onSelect)
def onSelect(self, event):
#wait for the both dropdown list values, then do stuff
pass
def menu(self):
#do other stuff too
self.content = [['foo1', 1 , 'bar1', 1],['foo2', 1 , 'bar2', 1]]
if __name__ == "__main__":
app = wx.App(False)
frame = GUI(None, -1, "")
app.MainLoop()
你会注意到我将GridBagSizer替换为常规BoxSizer,以便更简单地演示。我使用Widget检查工具来帮助我弄清楚发生了什么。你应该看看: