猿问

wxpython组合框值参数未正确设置初始值

我正在用下面的代码编写一个脚本,但它不起作用,因为设置value参数应该在组合框上设置一个初始值,如代码和图片所示。有人对此有解决方案吗?


self.algorithms = {...}

self.algorithm = wx.ComboBox(self.pnl, value='--Select--', choices=list(self.algorithms.keys()),

                                     style=wx.CB_READONLY)

self.algorithm.SetPosition((50, 80))

慕娘9325324
浏览 120回答 1
1回答

largeQ

keys如果字典中的 是字符串,那应该可以工作。如果不是,您应该从以下位置收到错误:choices=list(self.algorithms.keys())choices应该list是strings在这种情况下尝试:choices=[str(k) for k in self.algorithms]反而。编辑风格wx.CB_READONLY字符串必须在组合框选项列表中这不是其他样式的情况因此,如果将索引用于该选择,则必须小心这是一些应该可以工作的简单代码:import wxclass Myframe(wx.Frame):    def __init__(self):        wx.Frame.__init__(self, None)        self.my_choices = {"Choose a sport":0,"Board Sports":1,"Ball Sports":2,"Fight Sports":3}        #self.my_choices = ["Board Sports","Ball Sports","Fight Sports"]        self.my_subchoices = [[],["Skate boarding","Surfing","Ski boarding"],["Football","Cricket","Rugby"],["Boxing","Wrestling","Karate"]]        pan = wx.Panel(self)        self.cbx = wx.ComboBox(pan, -1, value = "Choose a sport", pos=(10,30), size=(300,30),choices=list(self.my_choices),style=wx.CB_DROPDOWN | wx.CB_READONLY )        self.cbx.Bind(wx.EVT_COMBOBOX, self.on_selection)    def on_selection(self, evt):        Choice = self.cbx.GetSelection()        msg = ""        #        # Ignore choice 0 because of style wx.CB_READONLY or include a blank entry        # for choice 0 in my_subchoices        for x in self.my_subchoices[Choice]:            msg += x+"\n"        self.cbx.SetToolTip(msg)if __name__ == "__main__":    App = wx.App()    Myframe().Show()    App.MainLoop()
随时随地看视频慕课网APP

相关分类

Python
我要回答