你如何在 wxpython 中重新定位一个打开的 window

How do you re-position an open window in wxpython

当我在程序中初始化主 window 时,我可以愉快地设置 windows 位置:

self.MoveXY(G_hpos,G_vpos)    


self.Move(wx.Point(G_hpos,G_vpos))

self.SetPosition(wx.Point(G_hpos, G_vpos))

它们都工作得同样好,但是,如果稍后我想使用相同的代码更改初始位置,但在同一 class 的另一个函数中,什么也不会发生。
我在这里遗漏了一些非常简单的东西,或者我只是今天头发不好。
注意:这是使用 Linux

今天头发很糟糕!
我试图根据另一个 window 的输入在它实际改变之前更改位置,所以本质上我是将位置更改为当前位置。
对于任何对此问题感兴趣的人,这里有一个演示:

# -*- coding: utf-8 -*-
import wx
import time
class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
#        kwds["pos"] = (10,10)
        self.frame = wx.Frame.__init__(self, *args, **kwds)
        self.SetTitle("Move around the screen")
        self.InitUI()

    def InitUI(self):
        self.location1 = wx.Point(10,10)
        self.location2 = wx.Point(500,500)
        self.panel1 = wx.Panel(self)
        self.button1 = wx.Button(self.panel1, -1, label="Move", size=(80,25), pos=(10,10))
        self.button1.Bind(wx.EVT_BUTTON, self.OnItem1Selected)
        self.Show()
        self.Move(self.location1)

    def OnItem1Selected(self, event):
        self.MoveAround()

    def MoveAround(self):
        #Judder effect by moving the window
        for i in range(30):
            curr_location = self.GetPosition() #or self.GetPositionTuple()
            if curr_location == self.location1:
                print ("moving to ", self.location2)
                self.Move(self.location2) #Any of these 3 commands will work
    #            self.MoveXY(500,500)
    #            self.SetPosition(wx.Point(500,500), wx.SIZE_USE_EXISTING)
            else:
                print ("moving to ", self.location1)
                self.Move(self.location1) #Any of these 3 commands will work
    #            self.MoveXY(10,10)
    #            self.SetPosition(wx.Point(10,10), wx.SIZE_USE_EXISTING)
            self.Update()
            time.sleep(0.1)

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame(None)
    app.MainLoop()

N.B。在您的代码返回到主循环之前,移动本身似乎不会发生,因此例如,如果您试图通过在同一函数内多次更改位置几秒钟来创建抖动效果,您只会看到它移动到最终位置并且没有颤动。

缺少的元素实际上是对 self.Update() 的调用。