我正在将旧的tkinter程序转换为wxPython。我使用tk的一件事是tk.IntVar()等。 wx中有没有提供类似功能的东西?wxPython中是否有类似于tk.IntVar()的方法?
具体而言,我希望能够定义模块级别的变量,如myvar = tk.StringVar()
。然后,当这些变量被更新,都基于一个或多个用户界面元素的更新上新的变量值,就像将与发生什么:
self.score = tk.Entry(self, textvariable=myvar.get())
我正在将旧的tkinter程序转换为wxPython。我使用tk的一件事是tk.IntVar()等。 wx中有没有提供类似功能的东西?wxPython中是否有类似于tk.IntVar()的方法?
具体而言,我希望能够定义模块级别的变量,如myvar = tk.StringVar()
。然后,当这些变量被更新,都基于一个或多个用户界面元素的更新上新的变量值,就像将与发生什么:
self.score = tk.Entry(self, textvariable=myvar.get())
这里是你如何通常会安排您的应用程序....全局趋向是一个坏主意
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 http://pubsub.sourceforge.net/ - 他们有专门针对wx的示例,因此很容易)。 – dthor 2014-10-30 19:06:05
没有theres没有......但它没有必要......只是像你的stringvar或任何'my_text.SetValue(“asdasdad”)'' – 2014-10-29 17:07:58
对待你的小工具好吧,但如果小部件深深的帧/面板层次结构,命名空间的东西会不会是一种痛苦?像'MainFrame.Panel1.Panel2.SubPanel.my_text.SetValue(“asasdasd”)'? – dthor 2014-10-29 17:16:18
虽然我想我可以设置'my_subpanel = MainFrame.Panel1.Panel2.SubPanel',然后执行'my_subpanel.my_text.SetValue(“asasdasd”)' – dthor 2014-10-29 17:18:08