2011-05-26 25 views
2

我已經從一本書到另一本,一個谷歌搜索到另一本,我注意到每一個單一的一個以完全不同的方式啓動主窗口。困惑於如何構建GUI(wxpython)

我不想挑起壞習慣,所以有人請給我最好的這些選項,爲什麼它更好的方法。下面是我見過它做

A)
類的iFrame(wx.Frame)的所有方式: 高清初始化(....): wx.Frame._ 的init _(...)

B)
類的iFrame(wx.Frame): DEF 初始化(...): super_ 初始化 _(...)

C)
後來我看到一些使用該面板,而不是如
類茁壯(wx.Panel) 高清初始化 (...): wx.Panel。 初始化(...)

d)
而更令人困惑的一些使用的是常規的App類WX的
類接入點內部協議(wx.App): 高清的OnInit(個體經營): wx.Frame。如果我的某些結構是錯的,但我正在回憶這些事情的時候,我再次提起這個問題......如果有的話,如果有的話是最好的方法構建GUI。很難以下教程和書籍時,他們都做的東西差異的方式

編輯:對不起,如果格式不正確,但通常它的工作原理...

回答

2

我最喜歡的方式開始WX應用開發:

import wx 

class MainWindow(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     wx.Frame.__init__(self, *args, **kwargs) 

     self.panel = wx.Panel(self) 
     self.button = wx.Button(self.panel, label="Test") 

     self.sizer = wx.BoxSizer() 
     self.sizer.Add(self.button) 

     self.panel.SetSizerAndFit(self.sizer) 
     self.Show() 

app = wx.App(False) 
win = MainWindow(None) 
app.MainLoop() 

也是這個question見,這是關係。

1

不要擔心。你現在不會通過做出錯誤的選擇而毀掉你的未來編程。

您提到的任何選項都是錯誤的。他們都做不同的事情,因爲不同的應用程序有不同的要求沒有一條路是最好的。

只要你想做什麼,做什麼適合你,一旦你有更多的熟悉,那麼你就會明白爲什麼不同的代碼會有所不同。

0

我已經學會了困難的方式,就像在每個應用程序中一樣,封裝很重要。對於wxPython來說,主框架對象應該只有一個面板小部件,以及可選的菜單欄,工具欄和狀態欄小部件。沒有其他的。

這是我的新的wxPython應用

import wx 


class MainFrame(wx.Frame): 
    """Create MainFrame class.""" 
    def __init__(self): 
     """Initialise the class.""" 
     wx.Frame.__init__(self, None, -1, 'Basic wxPython module') 
     self.panel = MainPanel(self) 
     self.SetMenuBar(MenuBar(self)) 
     self.ToolBar = MainToolbar(self) 
     self.status_bar = StatusBar(self).status_bar 
     self.Bind(wx.EVT_CLOSE, self.panel.on_quit_click) 
     self.Centre() 
     self.Show() 


class MainPanel(wx.Panel): 
    """Create a panel class to contain screen widgets.""" 
    def __init__(self, parent): 
     """Initialise the class.""" 
     wx.Panel.__init__(self, parent) 

     """Create and populate main sizer.""" 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     cmd_quit = wx.Button(self, id=wx.ID_EXIT) 
     cmd_quit.Bind(wx.EVT_BUTTON, self.on_quit_click) 
     sizer.Add(cmd_quit) 
     self.SetSizerAndFit(sizer) 

    def on_quit_click(self, event): 
     """Handle parent close click event.""" 
     del event 
     quit() 


class MenuBar(wx.MenuBar): 
    """Create the menu bar class.""" 
    def __init__(self, parent): 
     """Create menu bar.""" 
     wx.MenuBar.__init__(self) 
     # File menu 
     file_menu = wx.Menu() 
     self.Append(file_menu, '&File') 

     quit_menu_item = wx.MenuItem(file_menu, wx.ID_EXIT) 
     parent.Bind(wx.EVT_MENU, parent.panel.on_quit_click, id=wx.ID_EXIT) 

     file_menu.AppendItem(quit_menu_item) 


class MainToolbar(wx.ToolBar): 
    def __init__(self, parent): 
     """Create tool bar.""" 
     wx.ToolBar.__init__(self, parent) 

     #self.AddTool(wx.ID_NEW, wx.Bitmap("images/new.png")) 
     #self.SetToolShortHelp(wx.ID_NEW, 'New file') 
     #self.Bind(wx.EVT_TOOL, parent.panel.on_new_menu_click, id=wx.ID_NEW) 
     self.Realize() 


class StatusBar(object): 
    def __init__(self, parent): 
     """Create status bar.""" 
     self.status_bar = parent.CreateStatusBar() 


if __name__ == '__main__': 
    """Run the application.""" 
    screen_app = wx.App() 
    main_frame = MainFrame() 
    screen_app.MainLoop() 
基本格局