问题描述
|
我已经从一本书转到另一本书,从一个谷歌搜索到另一本书,我注意到每个人都以完全不同的方式启动主窗口。
我不想养成不良习惯,所以有人可以给我最好的选择,以及为什么它是更好的方法。以下是我看过的所有方法
一种)
iFrame(wx.Frame)类:
def init(....):
wx.Frame._init _(...)
B)
iFrame(wx.Frame)类:
def init(...):
super_init _(...)
C)
然后,我看到一些使用面板代替的东西,例如
类iPanel(wx.Panel)
def init(...):
wx.Panel.init(...)
D)
更令人困惑的是,有些人正在使用wx的常规App类
iApp(wx.App)类:
def OnInit(自己):
wx.Frame.init(...)
如果我的某些结构有误,请原谅我,但我想起了这些,请再问一遍。。。如果是其中之一,则IF ANY是构建GUI的最佳方法。当他们都以差异方式做事时,很难遵循教程和书籍
编辑:很抱歉,如果格式不正确,但通常可以正常工作...
解决方法
我最喜欢的开始wx应用程序开发的方法是:
import wx
class MainWindow(wx.Frame):
def __init__(self,*args,**kwargs):
wx.Frame.__init__(self,**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()
另请参阅与此相关的问题。
,不用担心。您现在不会通过选择错误来破坏将来的编程。
您提到的所有选项都不对。由于不同的应用程序具有不同的要求,因此它们的工作方式都不同。没有一种方法是最好的。
只需根据需要进行工作,然后为您工作,然后对您有所了解,就会明白为什么不同的代码会有所不同。
,我已经学会了很难的方法,就像在每个应用程序中一样,封装很重要。而且是wxPython特有的,主框架对象应该恰好具有一个面板小部件,以及可选的菜单栏,工具栏和状态栏小部件。没有其他的。
这是我的新wxPython应用程序的基本模式:
(2019年2月7日更新:Wx Phoenix和Python 3)
import wx
class MainFrame(wx.Frame):
\"\"\"Create MainFrame class.\"\"\"
def __init__(self,**kwargs):
super(MainFrame,self).__init__(None,**kwargs)
self.Title = \'Basic wxPython module\'
self.SetMenuBar(MenuBar(self))
self.ToolBar = MainToolbar(self)
self.status_bar = StatusBar(self).status_bar
self.Bind(wx.EVT_CLOSE,self.on_quit_click)
panel = MainPanel(self)
sizer = wx.BoxSizer()
sizer.Add(panel)
self.SetSizerAndFit(sizer)
self.Centre()
self.Show()
def on_quit_click(self,event):
\"\"\"Handle close event.\"\"\"
del event
wx.CallAfter(self.Destroy)
class MainPanel(wx.Panel):
\"\"\"Panel class to contain frame widgets.\"\"\"
def __init__(self,parent,**kwargs):
super(MainPanel,self).__init__(parent,**kwargs)
\"\"\"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,parent.on_quit_click)
sizer.Add(cmd_quit)
self.SetSizer(sizer)
class MenuBar(wx.MenuBar):
\"\"\"Create the menu bar.\"\"\"
def __init__(self,**kwargs):
super(MenuBar,self).__init__(*args,**kwargs)
# 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.on_quit_click,id=wx.ID_EXIT)
file_menu.Append(quit_menu_item)
class MainToolbar(wx.ToolBar):
\"\"\"Create tool bar.\"\"\"
def __init__(self,**kwargs):
super(MainToolbar,**kwargs)
#quit_bmp = wx.ArtProvider.GetBitmap(wx.ART_QUIT)
#self.AddTool(wx.ID_EXIT,\'Quit\',wx.Bitmap(quit_bmp))
#self.SetToolShortHelp(wx.ID_EXIT,\'Quit\')
#self.Bind(wx.EVT_TOOL,id=wx.ID_EXIT)
#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()
,为了回应XilyummY的评论,我添加了这个附加答案,以显示如何在单独的文件中组织主类。
这是我基于四个文件的解决方案:
main.py:应用程序的主框架和应用程序加载器;
main_panel.py:应用程序的主面板;
menu_bar.py:框架的菜单栏定义;
tool_bar.py:框架中的工具栏。
代码按以下顺序进行:
main.py
import wx
from main_panel import MainPanel
from menu_bar import MenuBar
from tool_bar import MainToolbar
class MainFrame(wx.Frame):
\"\"\"Create MainFrame class.\"\"\"
def __init__(self,event):
\"\"\"Handle close event.\"\"\"
del event
wx.CallAfter(self.Destroy)
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()
main_panel.py
import wx
class MainPanel(wx.Panel):
\"\"\"Panel class to contain frame widgets.\"\"\"
def __init__(self,parent.on_quit_click)
sizer.Add(cmd_quit)
self.SetSizer(sizer)
menu_bar.py
import wx
class MenuBar(wx.MenuBar):
\"\"\"Create the menu bar.\"\"\"
def __init__(self,id=wx.ID_EXIT)
file_menu.Append(quit_menu_item)
tool_bar.py
import wx
class MainToolbar(wx.ToolBar):
\"\"\"Create tool bar.\"\"\"
def __init__(self,**kwargs)
new_bmp = wx.ArtProvider.GetBitmap(wx.ART_NEW)
#preferences_bmp = wx.Bitmap(\'images/preferences.png\')
quit_bmp = wx.ArtProvider.GetBitmap(wx.ART_QUIT)
self.AddTool(wx.ID_NEW,\'New\',new_bmp)
#self.AddTool(wx.ID_PREFERENCES,\'Preferences\',preferences_bmp)
self.AddTool(wx.ID_EXIT,quit_bmp)
self.SetToolShortHelp(wx.ID_NEW,\'New ...\')
self.SetToolShortHelp(wx.ID_PREFERENCES,\'Preferences ...\')
self.SetToolShortHelp(wx.ID_EXIT,\'Quit\')
self.Bind(wx.EVT_TOOL,id=wx.ID_EXIT)
self.Realize()