简体   繁体   English

wxPython移动小部件

[英]wxPython moving widgets

I'm a very beginner of wxPython. 我是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. 现在,我正在尝试使用timer()使静态文本可移动,但是问题是,当出现后一个时,前一个就不会隐藏。 So I'm thinking in these ways: (1) Is that because I'm using static text? 所以我在想这些方式:(1)是因为我使用的是静态文本吗? 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". (2)当我想首先使用“隐藏”或“破坏”时,它显示为“未定义”。

Any helpful suggestion will be great, and below is my code(python version: 2.6): 任何有用的建议都会很棒,下面是我的代码(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()

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM