【问题标题】:Python/wxPython: How to reset wx.Choice to a default valuePython/wxPython:如何将 wx.Choice 重置为默认值
【发布时间】:2014-07-21 15:11:58
【问题描述】:

我正在使用 wxPython 中的多个 wx.Choice 控件,我需要重置按钮来恢复默认选项“-- Select --”。 我无法做到这一点,我最接近的是重置为空选项,这不是我们想要的。我希望在按下重置按钮时出现默认选项“--选择--”。请参阅下面的代码。

data.py

import wx

class MyFrame1 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 250,300 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        DataBox = wx.BoxSizer( wx.HORIZONTAL )

        gSizer2 = wx.GridSizer( 0, 2, 0, 0 )

        self.Color_Label = wx.StaticText( self, wx.ID_ANY, u"Color:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.Color_Label.Wrap( -1 )
        self.Color_Label.SetFont( wx.Font( 13, 70, 90, 90, False, wx.EmptyString ) )

        gSizer2.Add( self.Color_Label, 0, wx.ALL, 5 )

        Color_optionsChoices = [ u"-- Select --", u"Red", u"Green", u"Pink", u"Blue", u"Yellow", u"White", u"Brown" ]
        self.Color_options = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Color_optionsChoices, 0 )
        self.Color_options.SetSelection( 0 )
        gSizer2.Add( self.Color_options, 0, wx.ALL, 5 )

        self.Age = wx.StaticText( self, wx.ID_ANY, u"Age:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.Age.Wrap( -1 )
        self.Age.SetFont( wx.Font( 13, 70, 90, 90, False, wx.EmptyString ) )

        gSizer2.Add( self.Age, 0, wx.ALL, 5 )

        Age_optionsChoices = [ u"-- Select --", u"15", u"16", u"17", u"18", u"19", u"20", u"21", u"22", u"23", u"24", u"25", wx.EmptyString ]
        self.Age_options = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Age_optionsChoices, 0 )
        self.Age_options.SetSelection( 0 )
        gSizer2.Add( self.Age_options, 0, wx.ALL, 5 )

        self.Country = wx.StaticText( self, wx.ID_ANY, u"Country:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.Country.Wrap( -1 )
        self.Country.SetFont( wx.Font( 13, 70, 90, 90, False, wx.EmptyString ) )

        gSizer2.Add( self.Country, 0, wx.ALL, 5 )

        Country_optionsChoices = [ u"-- Select --", u"Mexico", u"Peru", u"India", u"USA", u"UK", u"Greece", u"Agentina", u"Greece", u"Brazil", u"Egypt" ]
        self.Country_options = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Country_optionsChoices, 0 )
        self.Country_options.SetSelection( 0 )
        gSizer2.Add( self.Country_options, 0, wx.ALL, 5 )

        self.Reset_button = wx.Button( self, wx.ID_ANY, u"Reset", wx.DefaultPosition, wx.DefaultSize, 0 )
        gSizer2.Add( self.Reset_button, 0, wx.ALL, 5 )

        self.Exit_button = wx.Button( self, wx.ID_ANY, u"Exit", wx.DefaultPosition, wx.DefaultSize, 0 )
        gSizer2.Add( self.Exit_button, 0, wx.ALL, 5 )


        DataBox.Add( gSizer2, 1, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 5 )


        self.SetSizer( DataBox )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.Reset_button.Bind( wx.EVT_BUTTON, self.OnResetButton )
        self.Exit_button.Bind( wx.EVT_BUTTON, self.OnExitButton )




    def OnResetButton( self, event ):

#       val = '-- Select --'  # NOT WORKING

        val = ' '  # THIS WORKS, BUT RESETS TO EMPTY CHOICE

        self.Color_options.SetLabel(val)
        self.Age_options.SetLabel(val)
        self.Country_options.SetLabel(val)


    def OnExitButton( self, event ):
        self.Close()







app = wx.App(0)
MyFrame1(None).Show()
app.MainLoop()

提前感谢您的宝贵时间。

【问题讨论】:

  • 在转储代码时,最好只转储代码中有问题的部分的最小示例,这样您更有可能获得帮助
  • 你不能在 OnResetButton 处理程序中调用 self.Age_options.SetSelection(0) 还是我误解了你真正想要实现的目标?
  • 感谢您的建议。调用“SetSelection(0)”起到了神奇的作用。谢谢

标签: python python-2.7 wxpython wxwidgets wxtextctrl


【解决方案1】:

请将您的源代码缩减为可运行的最小示例(如下所述)。

答案:我很惊讶wx.Choice.SetLabel(' ') 做了一些有用的事情。你想要做的是:

    self.Color_Options.SetStringSelection(val)

(对于wxChoice/wxItemContainer,请参阅wxWidgets documentation)。

备注u'--Select--' 在 Python 中不是 '--Select--'。碰巧的是,如果源编码设置为UTF-8,wxPython 不会抱怨和理解str UTF-8 编码以及u''

最小可运行示例:一如既往的重要:使示例尽可能小将告诉您哪些部分与您的问题相关,哪些不相关。在很多情况下,通过编写最小的示例,我经常自己找到答案。

最小化你的例子:

import wx

class MyFrame1(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)

        pnl = wx.Panel(self, wx.ID_ANY)
        szmain = wx.BoxSizer(wx.VERTICAL)
        color_choices = [u"-- Select --", u"Red", u"Green", u"Pink", u"Blue", u"Yellow", u"White", u"Brown"]
        self.color_options = wx.Choice(pnl, wx.ID_ANY, choices=color_choices)
        self.color_options.SetSelection(0)

        self.reset_button = wx.Button(pnl, wx.ID_ANY, u"Reset")

        szmain.Add(self.color_options, 0, wx.ALL|wx.EXPAND, 4)
        szmain.Add(self.reset_button, 0, wx.ALL|wx.EXPAND, 4)
        pnl.SetSizer(szmain)

        self.reset_button.Bind( wx.EVT_BUTTON, self.OnResetButton )

    def OnResetButton(self, event):
        val = '-- Select --'  # NOT WORKING
        #  val = ' '  # THIS WORKS, BUT RESETS TO EMPTY CHOICE

        self.color_options.SetLabel(val)

app = wx.App(0)
MyFrame1(None).Show()
app.MainLoop()

【讨论】:

  • 感谢您的意见。我会坚持下去。
  • 如果认为你的问题的答案是正确的,你可以接受它(在upvote/downvote按钮下方复选标记)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多