python:wxpython create Multi Document Interface (MDI) using MVC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # encoding: utf-8 # 版权所有 2024 ©涂聚文有限公司 # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # OS : windows 10 # Datetime : 2024/12/6 23:32 # User : geovindu # Product : PyCharm # Project : ictsimple # File : wxmdi3.py # explain : 学习 import wx import wx.aui # 模型 class MyModel: pass # 视图 class MyView(wx.Frame): def __init__( self , parent, id , title): wx.Frame.__init__( self , parent, id , title, size = ( 800 , 600 )) self .mgr = wx.aui.AuiManager() self .mgr.SetManagedWindow( self ) self .nb = wx.aui.AuiNotebook( self ) self .mgr.AddPane( self .nb, wx.aui.AuiPaneInfo().CenterPane()) self .controller = None # 初始化为None,稍后在外部设置 self .mgr.Update() def add_tab_panel( self , name): # 改名为更符合Python命名规范的add_tab_panel panel = wx.Panel( self .nb) self .nb.AddPage(panel, name) btn = wx.Button(panel, - 1 , "Close Tab" , pos = ( 10 , 10 )) btn.Bind(wx.EVT_BUTTON, self .on_close_tab) def on_close_tab( self , event): if self .controller: # 检查controller是否已经被设置 self .controller.on_close_tab(event) # 控制器 class MyController: def __init__( self , model, view): self .model = model self .view = view self .setup_initial_tabs() # 在控制器中设置初始标签页 def setup_initial_tabs( self ): self .add_tab( "Tab 1" ) self .add_tab( "Tab 2" ) def add_tab( self , name): self .view.add_tab_panel(name) def on_close_tab( self , event): page = self .view.nb.GetSelection() if page ! = - 1 : self .view.nb.DeletePage(page) # 主程序 if __name__ = = "__main__" : app = wx.App( False ) model = MyModel() view = MyView( None , - 1 , "MDI Example" ) controller = MyController(model, view) view.controller = controller # 设置视图中的控制器引用 view.Show( True ) app.MainLoop() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | # encoding: utf-8 # 版权所有 2024 ©涂聚文有限公司 # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: python wxpython mdi using mvc style tabs # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # OS : windows 10 # Datetime : 2024/12/6 22:24 # User : geovindu # Product : PyCharm # Project : ictsimple # File : wxmdi2.py # explain : 学习 import wx import wx.aui class Model: def __init__( self ): self .data = "Initial data" class View(wx.Panel): def __init__( self , parent, model): wx.Panel.__init__( self , parent) self .model = model self .text_ctrl = wx.TextCtrl( self , - 1 , self .model.data, style = wx.TE_MULTILINE) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .text_ctrl, 1 , wx.EXPAND) self .SetSizer(sizer) def update( self ): self .text_ctrl.SetValue( self .model.data) class Controller: def __init__( self , model, view): self .model = model self .view = view self .view.text_ctrl.Bind(wx.EVT_TEXT, self .on_text_changed) def on_text_changed( self , event): self .model.data = self .view.text_ctrl.GetValue() self .view.update() class MainFrame(wx.Frame): def __init__( self ): wx.Frame.__init__( self , None , - 1 , "MVC Tabs" ) self .notebook = wx.aui.AuiNotebook( self ) for i in range ( 3 ): model = Model() view = View( self .notebook, model) controller = Controller(model, view) self .notebook.AddPage(view, f "Tab {i+1}" ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .notebook, 1 , wx.EXPAND) self .SetSizer(sizer) if __name__ = = "__main__" : app = wx.App() frame = MainFrame() frame.Show() app.MainLoop() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | # encoding: utf-8 # 版权所有 2024 ©涂聚文有限公司 # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # OS : windows 10 # Datetime : 2024/12/6 21:49 # User : geovindu # Product : PyCharm # Project : ictsimple # File : wxmdi.py # explain : 学习 #!/usr/bin/env python # -*- encoding: utf-8 -*- # 如果您的源代码是UTF-8,建议使用这个 import wx import wx.aui class ParentFrame(wx.aui.AuiMDIParentFrame): def __init__( self , parent): super ().__init__(parent, - 1 , title = "AuiMDIParentFrame" , size = ( 640 , 480 ), style = wx.DEFAULT_FRAME_STYLE) self .count = 0 self .SetMenuBar( self .MakeMenuBar()) self .statusBar = self .CreateStatusBar(style = wx.BORDER_NONE) # set text to status bar self .statusBar.SetStatusText( "Status Bar" ) #self.statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP) #self.statusbar.SetStatusWidths([-2, -3]) #self.statusbar.SetStatusText("Ready", 0) #self.statusbar.SetStatusText("Welcome To wxPython!", 1) def MakeMenuBar( self ): mb = wx.MenuBar() menu = wx.Menu() item = menu.Append( - 1 , "New child window\tCtrl-N" ) self .Bind(wx.EVT_MENU, self .OnNewChild, item) item = menu.Append( - 1 , "Close parent" ) self .Bind(wx.EVT_MENU, self .OnDoClose, item) mb.Append(menu, "&File" ) return mb def OnNewChild( self , evt): self .count + = 1 try : child = ChildFrame( self , self .count) child.Show() except Exception as erro: print (erro) def OnDoClose( self , evt): self .Close() class ChildFrame(wx.aui.AuiMDIChildFrame): def __init__( self , parent, count): super ().__init__(parent, - 1 , title = "Child: %d" % count) self .count = count self .SetMenuBar( self .MakeMenuBar()) p = wx.Panel( self ) wx.StaticText(p, - 1 , "This is child %d" % count, ( 10 , 10 )) p.SetBackgroundColour( 'light blue' ) sizer = wx.BoxSizer() sizer.Add(p, 1 , wx.EXPAND) self .SetSizer(sizer) wx.CallAfter( self .Layout) def MakeMenuBar( self ): mb = wx.MenuBar() menu = wx.Menu() item = menu.Append( - 1 , "This is child %d's menu" % self .count) mb.Append(menu, "&Child" ) return mb if __name__ = = '__main__' : print (wx.VERSION_STRING) app = wx.App() frame = ParentFrame( None ) app.SetTopWindow(frame) frame.Show() app.MainLoop() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import wx import wx.aui class MyFrame(wx.Frame): def __init__( self , parent, id , title): wx.Frame.__init__( self , parent, id , title, size = ( 800 , 600 )) self .mgr = wx.aui.AuiManager() self .mgr.SetManagedWindow( self ) # Create a notebook for the tabs self .nb = wx.aui.AuiNotebook( self ) self .mgr.AddPane( self .nb, wx.aui.AuiPaneInfo().CenterPane()) # Add a tab to the notebook self .addTab( "Tab 1" ) self .addTab( "Tab 2" ) self .mgr.Update() def addTab( self , name): panel = wx.Panel( self .nb) self .nb.AddPage(panel, name) # Add a close button to the tab btn = wx.Button(panel, - 1 , "Close Tab" , pos = ( 10 , 10 )) btn.Bind(wx.EVT_BUTTON, self .onCloseTab) def onCloseTab( self , event): page = self .nb.GetSelection() if page ! = - 1 : self .nb.DeletePage(page) if __name__ = = "__main__" : app = wx.App( False ) frame = MyFrame( None , - 1 , "MDI Example" ) frame.Show( True ) app.MainLoop() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import wx class MainFrame(wx.MDIParentFrame): def __init__( self , parent, title): wx.MDIParentFrame.__init__( self , parent, - 1 , title) # Create a menu bar menubar = wx.MenuBar() fileMenu = wx.Menu() newChildItem = fileMenu.Append(wx.ID_ANY, "&New Child Window" ) exitItem = fileMenu.Append(wx.ID_EXIT, "E&xit" ) menubar.Append(fileMenu, "&File" ) self .SetMenuBar(menubar) # Bind events self .Bind(wx.EVT_MENU, self .OnNewChild, newChildItem) self .Bind(wx.EVT_MENU, self .OnExit, exitItem) def OnNewChild( self , event): childFrame = ChildFrame( self , "Child Window" ) childFrame.Show() def OnExit( self , event): self .Close() class ChildFrame(wx.MDIChildFrame): def __init__( self , parent, title): wx.MDIChildFrame.__init__( self , parent, - 1 , title) if __name__ = = "__main__" : app = wx.App() frame = MainFrame( None , "MDI Example" ) frame.Show() app.MainLoop() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import wx class TabbedFrame(wx.Frame): def __init__( self , parent, id , title): wx.Frame.__init__( self , parent, id , title, size = ( 800 , 600 )) self .notebook = wx.Notebook( self , - 1 , style = wx.NB_TOP) # 创建第一个标签页 self .create_tab( "Tab 1" ) # 创建第二个标签页 self .create_tab( "Tab 2" ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .notebook, 1 , wx.EXPAND | wx. ALL , 5 ) self .SetSizer(sizer) def create_tab( self , title): panel = wx.Panel( self .notebook) text = wx.StaticText(panel, - 1 , f "This is {title}" ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(text, 1 , wx.EXPAND | wx. ALL , 10 ) panel.SetSizer(sizer) self .notebook.AddPage(panel, title) app = wx.App( False ) frame = TabbedFrame( None , - 1 , "Tabbed Frame" ) frame.Show() app.MainLoop() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import wx import wx.aui class MyFrame(wx.Frame): def __init__( self , parent, title): wx.Frame.__init__( self , parent, title = title, size = ( 800 , 600 )) self .notebook = wx.aui.AuiNotebook( self ) self .notebook.SetArtProvider(wx.aui.AuiSimpleTabArt()) # Use a simpler tab art # Create some panels for the tabs panel1 = wx.Panel( self .notebook) panel2 = wx.Panel( self .notebook) # Add the panels to the notebook with close buttons self .notebook.AddPage(panel1, "Tab 1" , True ) self .notebook.AddPage(panel2, "Tab 2" , True ) # Bind the close button event self .notebook.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self .on_page_close) self .Show( True ) def on_page_close( self , event): page = event.GetSelection() self .notebook.DeletePage(page) if __name__ = = '__main__' : app = wx.App() frame = MyFrame( None , "MDI-like Tabbed Interface" ) app.MainLoop() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import wx # Model class Document: def __init__( self , content = ""): self .content = content def get_content( self ): return self .content def set_content( self , new_content): self .content = new_content # View class DocumentView(wx.Panel): def __init__( self , parent, document): super ().__init__(parent) self .document = document self .text_ctrl = wx.TextCtrl( self , style = wx.TE_MULTILINE) self .text_ctrl.SetValue( self .document.get_content()) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .text_ctrl, 1 , wx.EXPAND) self .SetSizer(sizer) def update_content( self ): self .text_ctrl.SetValue( self .document.get_content()) # Controller class DocumentController: def __init__( self , document, view): self .document = document self .view = view def update_document( self ): self .document.set_content( self .view.text_ctrl.GetValue()) # Main Frame class MainFrame(wx.MDIParentFrame): def __init__( self , * args, * * kwargs): super ().__init__( * args, * * kwargs) self .file_menu = wx.Menu() self .file_menu.Append(wx.ID_NEW, "New" ) self .Bind(wx.EVT_MENU, self .on_new, id = wx.ID_NEW) menu_bar = wx.MenuBar() menu_bar.Append( self .file_menu, "File" ) self .SetMenuBar(menu_bar) def on_new( self , event): document = Document() view = DocumentView( self , document) controller = DocumentController(document, view) child_frame = wx.MDIChildFrame( self , - 1 , "New Document" ) child_frame.SetClientSize(view.GetBestSize()) child_frame.Show() if __name__ = = '__main__' : app = wx.App() frame = MainFrame( None , title = "MDI Example" ) frame.Show() app.MainLoop() |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-12-06 SVG: draw text on a image
2009-12-06 Microsoft Visual Studio International Pack 1.0 SR1