0

I'm a very beginner of wxPython. Now I'm trying to make a static text moveable by using timer(), but the question is that when the later one appears, the former one just doesn't hide. So I'm thinking in these ways: (1) Is that because I'm using static text? Maybe it could be "moveable" while I'm using some other widgets? (2) When I wanna use "hide" or "destroy" at first, it comes out "not defined".

Any helpful suggestion will be great, and below is my code(python version: 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 Answer 1

1

the static part of StaticText actually has nothing to do with movement... or even mutability, but rather from a users perspective it is Static (ie they cannot alter it)

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

its because you are not changing the existing text position you are creating a new text each time ... this way you will just move the existing one ... although really you should use an actual timer

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])
Sign up to request clarification or add additional context in comments.

5 Comments

Many thanks for your quick reply! While now the program shows: File "que_simplify.py", line 32, in play self.timer.start() AttributeError: 'Timer' object has no attribute 'start'
ehh try .Start instead I always get those mixed up (because python thread is .start)
And may I ask what can I use if I want a movable widget instead of static text?
you can move static text fine .... Im not sure what you mean by moveable? you mean like drag-drop? by the user (also note that i forgot the timer speed (which is an argument to Start
Brilliant! Now it works perfectly! And it's enough for the "movable" in my mind, thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.