有一个代码示例
class Example(wx.Frame):
def __init__(self, *args, **kw):
super(Example, self).__init__(*args, **kw)
self.InitUI()
def InitUI(self):
pnl = wx.Panel(self)
sld = wx.Slider(pnl, value=0, minValue=0, maxValue=200, pos=(30, 20),
size=(650, 15), style=wx.SL_HORIZONTAL)
sld1 = wx.Slider(pnl, value=0, minValue=0, maxValue=360, pos=(30, 50),
size=(650, 15), style=wx.SL_HORIZONTAL)
sld.Bind(wx.EVT_SCROLL, self.OnSliderScroll)
sld1.Bind(wx.EVT_SCROLL, self.OnSliderScroll)
self.txt1 = wx.StaticText(pnl, label='0', pos=(250, 120))
self.txt2 = wx.StaticText(pnl, label='0', pos=(10, 50))
self.SetSize((700, 200))
self.SetTitle('ROBO ARM')
self.Centre()
self.Show(True)
def OnSliderScroll(self, e):
id_slider = e.GetId()
obj = e.GetEventObject()
VARIABLE = obj.GetValue()
tty = str(val)+str(id_slider)
self.txt1.SetLabel(str(VARIABLE)
class Example1(wx.Frame):
def __init__(self, parent, title):
super(Example1, self).__init__(parent, title=title, size=(350, 280))
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Centre()
self.Show()
def OnPaint(self, e):
dc = wx.PaintDC(self)
dc.SetPen(wx.Pen('#d4d4d4'))
dc.SetBrush(wx.Brush('#c56c00'))
dc.DrawRectangle(10, 7, 90, **VARIABLE NEEDS HERE**)
我有两个问题:如何将VARIABLE从Class Example传递到Example1到标记的位置 第二是因为我知道我可以在这一行改变自我:self.txt1.SetLabel(str(VARIABLE)到类似Example.InitUI(和其他东西) - 你可以写这个字符串,因为它必须是?
感谢
答案 0 :(得分:1)
在这种情况下(VARIABLE
似乎是Example1
的所有实例的共享状态),它看起来适合使用全局变量。
VARIABLE = None
class Example(wx.Frame):
...
def OnSliderScroll(self, e):
global VARIABLE
id_slider = e.GetId()
obj = e.GetEventObject()
VARIABLE = obj.GetValue()
tty = str(val)+str(id_slider)
self.txt1.SetLabel(str(VARIABLE))
class Example1(wx.Frame):
...
def OnPaint(self, e):
global VARIABLE
dc = wx.PaintDC(self)
dc.SetPen(wx.Pen('#d4d4d4'))
dc.SetBrush(wx.Brush('#c56c00'))
dc.DrawRectangle(10, 7, 90, VARIABLE)
或者,如果由于某种原因你想要避免全局变量,你可以使用一个类变量(仍假设它在Example1
的所有实例中共享:
class Example(wx.Frame):
...
def OnSliderScroll(self, e):
id_slider = e.GetId()
obj = e.GetEventObject()
VARIABLE = obj.GetValue()
tty = str(val)+str(id_slider)
self.txt1.SetLabel(str(VARIABLE))
Example1.VARIABLE = VARIABLE
class Example1(wx.Frame):
...
def OnPaint(self, e):
dc = wx.PaintDC(self)
dc.SetPen(wx.Pen('#d4d4d4'))
dc.SetBrush(wx.Brush('#c56c00'))
dc.DrawRectangle(10, 7, 90, Example1.VARIABLE)