【问题标题】:Is there an analog to tk.IntVar() in wxPython?wxPython 中是否有 tk.IntVar() 的类似物?
【发布时间】:2014-10-29 17:04:02
【问题描述】:

我正在将一个旧的 tkinter 程序转换为 wxPython。我大量使用的 tk 中的一件事是 tk.IntVar() 等。 wx 中有没有提供类似功能的东西?

具体来说,我希望能够定义模块级变量,例如myvar = tk.StringVar()。然后当这些变量更新时,根据新的变量值更新一个或多个 UI 元素,就像会发生的情况一样:

self.score = tk.Entry(self, textvariable=myvar.get())

【问题讨论】:

  • 不,没有……但没有必要……只需将您的小部件视为您的字符串变量或任何my_text.SetValue("asdasdad")
  • 好的,但是如果小部件位于框架/面板层次结构的深处,命名空间的东西会不会很痛苦?喜欢MainFrame.Panel1.Panel2.SubPanel.my_text.SetValue("asasdasd")
  • 虽然我想我可以设置my_subpanel = MainFrame.Panel1.Panel2.SubPanel然后做my_subpanel.my_text.SetValue("asasdasd")
  • 不,这通常不是你这样做的......(虽然它会起作用)

标签: python tkinter wxpython program-structure


【解决方案1】:

这是您通常组织应用程序的方式....全局变量往往不是一个好主意

class MyNestedPanel(wx.Panel):
     def __init__(self,*a,**kw):
         ...
         self.user = wx.TextCtrl(self,-1)
      def SetUser(self,username):
         self.user.SetValue(username)

class MyMainPanel(wx.Panel):
      def __init__(self,*a,**kw):
          ...
          self.userpanel = MyNestedPanel(self,...)
      def SetUsername(self,username):
           self.userpanel.SetUser(username)

class MainFrame(wx.Frame):
      def __init__(self,*a,**kw):
           ...
           self.mainpanel = MyMainPanel(self,...)
      def SetUsername(self,username):
           self.mainpanel.SetUsername(username)

a = wx.App()
f = MainFrame(...)
f.Show()
a.MainLoop()

虽然你可以制作辅助函数

def set_widget_value(widget,value):
    if hasattr(widget,"SetWidgetValue"):
        return widget.SetWidgetValue(value) 
    if isinstance(widget,wx.Choice):
       return widget.SetStringSelection(value)
    if hasattr(widget,"SetValue"):
        return widget.SetValue(value)
    if hasattr(widget,"SetLabel"):
        return widget.SetLabel(value)
    else:
       raise Exception("Unknown Widget Type : %r"%widget)

def get_widget_value(widget):
     if hasattr(widget,"GetWidgetValue"):
        return widget.GetWidgetValue() 
     if isinstance(widget,wx.Choice):
        return widget.GetStringSelection()
     if hasattr(widget,"GetValue"):
        return widget.GetValue()
     if hasattr(widget,"GetLabel"):
        return  widget.GetLabel()
     else:
       raise Exception("Unknown Widget Type : %r"%widget)

class WidgetManager(wx.Panel):
      def __init__(self,parent):
         self._parent = parent
         wx.Panel.__init__(self,parent,-1)
         self.CreateWidgets()
      def CreateWidgets(self):
         #create all your widgets here
         self.widgets = {}
      def SetWidgetValue(self,value):
         if isinstance(value,dict):
            for k,v in value.items():
               set_widget_value(self.widgets.get(k),v)
         else:
            raise Exception("Expected a dictionary but got %r"%value)
      def GetWidgetValue(self):
          return dict([(k,get_widget_value(v))for k,v in self.widgets])

然后像这样使用它们https://gist.github.com/joranbeasley/37becd81ff2285fcc933

【讨论】:

  • 谢谢,这似乎接近我想要的。然而,经过进一步研究,我决定采用发布者/订阅者设计模式(特别是来自 PyPubSub pubsub.sourceforge.net - 他们有专门针对 wx 的示例,所以很容易)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多