【问题标题】:Resolve a problem ComboBox and a TextCtrl [closed]解决问题 ComboBox 和 TextCtrl [关闭]
【发布时间】:2019-05-18 08:18:31
【问题描述】:

我想在 ComboBox 中选择一个元素时打印一条位于 TextCtrl 元组中的消息,具体取决于我选择的项目

当我按照我调查的方式进行操作时,我抛出了一个错误。

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()

【问题讨论】:

  • 什么错误?我们看不到您的屏幕,我们无法在您的脑海中阅读。不要指望我们会运行代码来看到这个错误(除了它可以在我们的计算机上正常工作)。始终将完整的错误消息(完整的 Traceback)放在有问题的地方(作为文本,而不是屏幕截图)。还有其他有用的信息。

标签: python python-2.7 wxpython


【解决方案1】:

第一个错误是缩进错误!
问题是您不是 BindingComboBox 事件,即当您做出选择时,将触发一个事件,必须捕获并采取行动。
您需要捕获该事件并将当前选择的文本从组合框(或其他)放入 textctrl。目前您没有尝试这样做。

这就是我假设你想要的。

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()

【讨论】:

  • 我有缩进错误,因为当我写帖子时,我写错了代码。实际上想打印元组(Info_Mods)中的消息。但多亏了你,我已经明白我该怎么做。
猜你喜欢
  • 2016-05-03
  • 2012-12-15
  • 2021-04-17
  • 2016-10-22
  • 2020-10-29
  • 2021-06-12
  • 2021-11-05
  • 2023-04-06
相关资源
最近更新 更多