Move windows without title bar (wxPython)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • analfabete
    New Member
    • Aug 2007
    • 19

    Move windows without title bar (wxPython)

    Hi evrebody!
    Currently I am using wxPython to make my own program !
    My program have no taskbar so I would like to know how to do this:
    When an user click on right mouse button, and he keep down this button , he can move the program windows anywhere.

    I have this:
    Code:
    self.Bind(wx.EVT_RIGHT_DOWN, self.OnMoveFen)
    So I don't know how to write OnMoveFen function... Can you help me please ?
    Thank you :-)

    EDIT: Can you delete this topic, I do an error
  • analfabete
    New Member
    • Aug 2007
    • 19

    #2
    Move windows without title bar (wxPython)

    Hi evrebody!
    Currently I am using wxPython to make my own program !
    My program have no taskbar so I would like to know how to do this:
    When an user click on right mouse button, and he keep down this button , he can move the program windows anywhere.

    I have this:
    Code:
    self.Bind(wx.EVT_RIGHT_DOWN, self.OnMoveFen)
    So I don't know how to write OnMoveFen function... Can you help me please ?
    Thank you :-)

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      I'm guessing that you mean "title bar"...

      I'd say that you probably want to catch

      EVT_MOTION

      Then, in the handler:[CODE=python]if event.m_leftDow n:
      x, y = event.GetPositi on()[/CODE]Then calculate the new position of the window based on the difference from the last motion event. You could later limit the area of the window that responds this way (or not).

      Comment

      • analfabete
        New Member
        • Aug 2007
        • 19

        #4
        So I have do this:
        Code:
            
        self.Bind(wx.EVT_MOTION , self.OnMoveFen)
        
        def OnMoveFen(self, event):
                if wx.MouseEvent.LeftIsDown == True:
                    pt = wx.Point
                    self.Move(pt)
        But when I push down my left button nothing do... can you help me please...

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by analfabete
          So I have do this:
          Code:
              
          self.Bind(wx.EVT_MOTION , self.OnMoveFen)
          
          def OnMoveFen(self, event):
                  if wx.MouseEvent.LeftIsDown == True:
                      pt = wx.Point
                      self.Move(pt)
          But when I push down my left button nothing do... can you help me please...
          Hopefully, this example will help you learn some of the fundamentals of OOP and event programming. You really need a basic understanding of object creation and have fallen into some basic misunderstandin gs.[CODE=python]#Boa:Frame:Fram e1

          import wx

          def create(parent):
          return Frame1(parent)

          [wxID_FRAME1] = [wx.NewId() for _init_ctrls in range(1)]

          class Frame1(wx.Frame ):
          def _init_ctrls(sel f, prnt):
          # generated method, don't edit
          wx.Frame.__init __(self, id=wxID_FRAME1, name='', parent=prnt, pos=wx.Point(22 , 22),
          size=wx.Size(40 0, 250), style=wx.DEFAUL T_FRAME_STYLE, title='Frame1')
          self.SetClientS ize(wx.Size(392 , 223))
          self.Bind(wx.EV T_MOTION, self.OnFrame1Mo tion)
          self.Bind(wx.EV T_LEFT_DOWN, self.OnFrame1Le ftDown)

          def __init__(self, parent):
          self._init_ctrl s(parent)

          self.lastMouseP os = wx.Point(0, 0)


          def OnFrame1Motion( self, event):
          if event.LeftIsDow n():
          x, y = event.GetPositi on()
          deltaX = x - self.lastMouseP os[0]
          deltaY = y - self.lastMouseP os[1]
          self.lastMouseP os = wx.Point(x, y)
          x, y = self.GetPositio n()
          self.Move(wx.Po int(x + deltaX, y + deltaY))
          event.Skip()

          def OnFrame1LeftDow n(self, event):
          self.lastMouseP os = event.GetPositi on()
          event.Skip()


          if __name__ == "__main__":
          app = wx.PySimpleApp( )
          frame = create(None)
          frame.Show()
          app.MainLoop()
          [/CODE]It's a little rough around the edges, but contains the basic idea.

          Comment

          • MJFinnegan87
            New Member
            • Feb 2016
            • 1

            #6
            I realize this is 7 years old, but in case someone else comes across this, an improvement on the implementation above is the following:

            Code:
            import wx
             
            def create(parent):
                return Frame1(parent)
             
            [wxID_FRAME1] = [wx.NewId() for _init_ctrls in range(1)]
             
            class Frame1(wx.Frame):
                def _init_ctrls(self, prnt):
                    # generated method, don't edit
                    wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt, pos=wx.Point(22, 22),
                            size=wx.Size(400, 250), style=wx.NO_BORDER, title='Frame1')
                    self.Bind(wx.EVT_MOTION, self.OnFrame1Motion)
                    self.Bind(wx.EVT_LEFT_DOWN, self.OnFrame1LeftDown)
             
                def __init__(self, parent):
                    self._init_ctrls(parent)
                    self.lastMousePos = wx.Point(0, 0)
            
                def OnFrame1Motion(self, event):
                    if event.LeftIsDown():
                        windowX = self.lastMousePos[0]
                        windowY = self.lastMousePos[1]
                        screenX = wx.GetMousePosition()[0]
                        screenY = wx.GetMousePosition()[1]
                        self.Move(wx.Point(screenX - windowX, screenY - windowY))
                    event.Skip()
             
                def OnFrame1LeftDown(self, event):
                    self.lastMousePos = event.GetPosition()
                    event.Skip()
            
                def closeWindow(self, e):
                    self.Destroy() #prevent memory leak
             
            if __name__ == "__main__":
                app = wx.App()
                frame = create(None)
                frame.Show()
                app.MainLoop()
            This now drags the window correctly, and at the same speed the mouse moves. Consider adding an 'x' button or option to close the window for the user. Alt-F4 on windows will close it in its current state.
            Last edited by MJFinnegan87; Feb 16 '16, 06:47 AM. Reason: Addt'l assitance

            Comment

            Working...