简体   繁体   English

wxPython中是否有tk.IntVar()的类似物?

[英]Is there an analog to tk.IntVar() in wxPython?

I'm converting an old tkinter program to wxPython. 我正在将一个旧的tkinter程序转换为wxPython。 One of the things from tk that I used liberally was tk.IntVar() and the like. 我大量使用的tk之一是tk.IntVar()等。 Is there anything in wx that provides similar functionality? wx中是否有提供类似功能的东西?

Specifically, I'd like to be able to define module-level variables such as myvar = tk.StringVar() . 具体来说,我希望能够定义模块级变量,例如myvar = tk.StringVar() Then when those variables are updated, have one or more UI elements update based on the new variable value just like what would happen with: 然后,在更新这些变量时,就像新的变量值一样,让一个或多个UI元素更新:

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

here is how you would normally organize your app .... globals tend to be a bad idea 这是通常情况下组织应用程序的方式..全局变量通常不是一个好主意

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

although you can make helper functions 虽然您可以使助手功能

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

and then use them like this https://gist.github.com/joranbeasley/37becd81ff2285fcc933 然后像这样使用它们https://gist.github.com/joranbeasley/37becd81ff2285fcc933

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM