wxPython移动控件

2024-09-27 23:25:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: pos文本selfidplaysizedef静态
1条回答
网友
1楼 · 发布于 2024-09-27 23:25:40

静态文本的静态部分实际上与运动无关。。。甚至是可变的,但从用户的角度来看,它是静态的(即他们不能改变它)

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

因为你没有改变现有的文本位置你每次都在创建一个新的文本。。。这样你就可以移动现有的。。。虽然你真的应该用一个真正的计时器

^{pr2}$

相关问题 更多 >

    热门问题