将框架保持在另一个窗口框架中

时间:2013-11-18 10:16:56

标签: python-2.7 window wxpython frame

我的程序在水平boxsizer中创建一个带有三个面板的Frame。带有“新窗口”项的菜单,用于创建一个seconde框架。我将seconde面板作为seconde窗口的父级。我想把第二帧停留在第一帧的第二个面板区域。 如果用户移动两个窗口中的一个,则seconde将保留在面板屏幕区域中。 你知道某种方式吗? 我尝试了一些东西,但使用不是很美观。

import wx

class MainWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))

        #Widgets
        panel_gch = wx.Panel(self,-1,size = (150,-1))
        panel_gch.SetBackgroundColour('white')
        self.panel=wx.Panel(self,-1,size=(300,400))
        self.panel.SetBackgroundColour((200,230,200))
        panel_drt = wx.Panel(self,-1,size = (150,-1))
        panel_drt.SetBackgroundColour('white')
        box = wx.BoxSizer(wx.HORIZONTAL)
        self.SetSizer(box)

        #Add
        box.Add(panel_gch,0,wx.EXPAND)
        box.Add(self.panel,1,wx.EXPAND)
        box.Add(panel_drt,0,wx.EXPAND)

        #Menu
        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        file_menu=wx.Menu()
        ID_FILE_NEW = 1
        file_menu.Append(ID_FILE_NEW,"New Window","This is a new window")
        menubar.Append(file_menu,"File")
        self.SetMenuBar(menubar)

        #bind and layout
        self.Bind(wx.EVT_MENU, self.get_new_window)
        panel_gch.Layout()
        self.panel.Layout()
        panel_drt.Layout()
        self.Layout()

    def get_new_window(self,event): # create new window
        self.new = NewWindow(self.panel,-1)
        self.new.Show(True)
        self.new.Bind(wx.EVT_MOVE,self.window2_on_move)

    def window2_on_move(self,event): # Window2 must stay in
        x, y = event.GetPosition()
        v,w =self.panel.GetScreenPosition()
        s,t = self.panel.GetClientSizeTuple()
        if x < v:
            self.new.Move((v,-1))
        if y < w:
            self.new.Move((-1,w))
        if x+200 > v+s:
            self.new.Move((v+s-200,-1))
        if y+200 > w+t:
            self.new.Move((-1,w+t-200))    

class NewWindow(wx.MiniFrame):

    def __init__(self,MainWindow,id):
        wx.MiniFrame.__init__(self, MainWindow, id, 'New Window', size=(200,200),\
                              style = wx.MINIMIZE | wx.CAPTION | wx.CLOSE_BOX | wx.CLOSE_BOX)
        self.CenterOnParent()


if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=MainWindow(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

2 个答案:

答案 0 :(得分:0)

你可能想要的是AUI。我个人推荐使用wx.lib.agw.aui而不是wx.aui,因为前者是纯Python,并且最近已经完成了很多工作。 wxPython演示包中有多个示例。你也可以在这里阅读:

答案 1 :(得分:0)

非常感谢Mike,正是我所需要的。

使用wxpython我发现这样: 孩子留在面板区域,并在移动时跟随窗口父母。

import wx

class MainWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))
        self.new = None
        #Widgets
        self.position = (0,0)
        panel_gch = wx.Panel(self,-1,size = (150,-1))
        panel_gch.SetBackgroundColour('white')
        self.panel=wx.Panel(self,-1,size=(300,400))
        self.panel.SetBackgroundColour((200,230,200))
        panel_drt = wx.Panel(self,-1,size = (150,-1))
        panel_drt.SetBackgroundColour('white')
        box = wx.BoxSizer(wx.HORIZONTAL)
        self.SetSizer(box)

        #Add
        box.Add(panel_gch,0,wx.EXPAND)
        box.Add(self.panel,1,wx.EXPAND)
        box.Add(panel_drt,0,wx.EXPAND)

        #Menu
        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        file_menu=wx.Menu()
        ID_FILE_NEW = 1
        file_menu.Append(ID_FILE_NEW,"New Window","This is a new window")
        menubar.Append(file_menu,"File")
        self.SetMenuBar(menubar)

        #bind and layout
        self.Bind(wx.EVT_MENU, self.get_new_window)

        panel_gch.Layout()
        self.panel.Layout()
        panel_drt.Layout()
        self.Layout()

    def get_new_window(self,event): # create new window
        if self.new == None:
            self.win_one_move = False
            self.new = NewWindow(self.panel,-1)
            self.new.Show(True)
            self.new.Bind(wx.EVT_MOVE,self.window2_on_move)
            self.Bind(wx.EVT_MOVE,self.window1_on_move)
            v,w =self.GetPosition()
            x, y = self.new.GetPosition()                
            self.get_windows_position((x-v),(y-w))       


    def get_windows_position(self,x,y):
        self.position = (x,y)
        print "check",self.position

    def window2_on_move(self,event): # Window2 must stay in
        if self.win_one_move == False:
            x, y = event.GetPosition()
            v,w =self.panel.GetScreenPosition()
            s,t = self.panel.GetClientSizeTuple()
            new_x,new_y = self.new.GetClientSizeTuple()
            if x < v:
                self.new.Move((v,-1))
            if y < w:
                self.new.Move((-1,w))
            if x+new_x > v+s:
                self.new.Move((v+s-new_x,-1))
            if y+new_y > w+t:
                self.new.Move((-1,w+t-new_y))
            v,w =self.GetPosition()
            x,y = self.new.GetPosition() 
            self.get_windows_position((x-v),(y-w))
        if self.win_one_move == True:

            self.win_one_move = False

    def window1_on_move(self,event):
        self.win_one_move = True
        print "1 move"
        x,y = self.GetPosition()
        self.new.Move((x+self.position[0],y+self.position[1]))        
        print self.position


class NewWindow(wx.MiniFrame):

    def __init__(self,MainWindow,id):
        wx.MiniFrame.__init__(self, MainWindow, id, 'New Window', size=(200,200),\
                              style = wx.CAPTION | wx.CLOSE_BOX | wx.CLOSE_BOX)
        self.CenterOnParent()


if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=MainWindow(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

可以被另一个人使用。谢谢