wxPython移动小部件

时间:2014-07-07 22:38:24

标签: python wxpython

我是wxPython的初学者。现在我试图通过使用timer()来使静态文本可移动,但问题是当后一个出现时,前者不会隐藏。 所以我在想这些方式: (1)那是因为我使用的是静态文本吗?也许它可能是“可移动的”,而我正在使用其他一些小部件? (2)当我想首先使用“隐藏”或“破坏”时,它会出现“未定义”。

任何有用的建议都会很棒,下面是我的代码(python版本:2.6):

#!/user/bin/python

import wx

pos_st = [[10,50],[40,50],[70,50],[100,50]]
i = -1

class Frame(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id,
                          'Move widget',
                          size = (200,150),
                          style=wx.MINIMIZE_BOX | wx.RESIZE_BORDER 
    | wx.SYSTEM_MENU | wx.CAPTION |  wx.CLOSE_BOX)
        self.initUI()

    def initUI(self):

        self.widgetPanel=wx.Panel(self, -1)
        self.widgetPanel.SetBackgroundColour('white')

        # Buttons for play the simulation
        playButton = wx.Button(self.widgetPanel, -1, "Play", pos=(10,10), size=(30,30))

        self.Bind(wx.EVT_BUTTON, self.play, playButton)
        playButton.SetDefault()

    def play(self, event):
        self.timer = wx.CallLater(1000, self.run_st)

    def run_st(self):
        global i
        i = (i+1)%4
        self.timer.Restart(1000)
        self.sT = wx.StaticText(self.widgetPanel, -1, '1',
                              pos=pos_st[i], size=(20,20))
        self.sT.SetBackgroundColour('grey')

if __name__ == "__main__":
    app = wx.App(False)
    frame = Frame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:1)

StaticText的静态部分实际上与移动无关......甚至是可变性,而是从用户的角度看它是静态的(即它们不能改变它)

def run_st(self):
    global i
    i = (i+1)%4
    self.timer.Restart(1000)
    if not hasattr(self,"sT"):
        self.sT = wx.StaticText(self.widgetPanel, -1, '1', size=(20,20))
        self.sT.SetBackgroundColour('grey')
    self.sT.SetPosition(pos_st[i])

因为你没有改变你每次创建新文本的现有文本位置......这样你就可以移动现有的文本......虽然你应该使用实际的计时器

def initUI(self):

    self.widgetPanel=wx.Panel(self, -1)
    self.widgetPanel.SetBackgroundColour('white')

    # Buttons for play the simulation
    playButton = wx.Button(self.widgetPanel, -1, "Play", pos=(10,10), size=(30,30))

    self.Bind(wx.EVT_BUTTON, self.play, playButton)
    playButton.SetDefault()
    self.sT = wx.StaticText(self,-1,"",size=(20,20))
    self.timer = wx.Timer()
    self.timer.Bind(wx.EVT_TIMER,self.run_st)
def play(self, event):
    self.timer.Start(1000)

def run_st(self,timerEvent):
    global i
    i = (i+1)%4
    self.sT.SetLabel("1")
    self.sT.SetPosition(pos_st[i])