在这篇Python GUI文章中,我想向您展示如何使用wxPython创建AboutBox对话框。因此,包括wx在内,我们需要另一个需要导入的类,名为wx.adv。这个类包含有关程序的一般信息,比如它的名称、版本、版权等等,以及程序开发人员、文档编写人员、艺术家和翻译人员的列表。
下面是使用Python的GUI库wxPython制作AboutBox 对话框的完整代码。
import wx
import wx.adv
class MyFrame(wx.Frame):
def __init__(self, parent, title):
super(MyFrame, self).__init__(parent, title =title, size = (800,600))
self.panel = MyPanel(self)
class MyPanel(wx.Panel):
def __init__(self, parent):
super(MyPanel, self).__init__(parent)
self.button = wx.Button(self, label = "关于Linux迷 www.linuxmi.com", pos = (100,100))
self.Bind(wx.EVT_BUTTON, self.onAbout)
def onAbout(self, event):
info = wx.adv.AboutDialogInfo()
info.SetName("Linux迷www.linuxmi.com")
info.SetVersion("最新开源资讯与教程")
info.SetDescription("Linux迷(www.linuxmi.com)与您分享关于开源的新鲜事")
info.SetCopyright("(C) 2011-2020")
info.SetWebSite("www.linuxmi.com")
info.AddDeveloper("Linux迷")
wx.adv.AboutBox(info)
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(parent=None, title="关于Linux迷")
self.frame.Show()
return True
app = MyApp()
app.MainLoop()
首先,我们有从wx继承的框架类。它是我们在这个类中创建MyPanel对象的顶级窗口。
class MyFrame(wx.Frame):
def __init__(self, parent, title):
super(MyFrame, self).__init__(parent, title =title, size = (800,600))
self.panel = MyPanel(self)
在这之后,我们创建了MyPanel类,这个类是一个容器类,用于我们的小部件,如按钮,菜单,复选按钮等,你可以看到,我们在这个类中创建了一个按钮,我们也完成了按钮的绑定。
class MyPanel(wx.Panel):
def __init__(self, parent):
super(MyPanel, self).__init__(parent)
self.button = wx.Button(self, label = "关于Linux迷 www.linuxmi.com", pos = (100,100))
self.Bind(wx.EVT_BUTTON, self.onAbout)
这是AboutBox的方法
def onAbout(self, event):
info = wx.adv.AboutDialogInfo()
info.SetName("Linux迷www.linuxmi.com")
info.SetVersion("最新开源资讯与教程")
info.SetDescription("Linux迷(www.linuxmi.com)与您分享关于开源的新鲜事")
info.SetCopyright("(C) 2011-2020")
info.SetWebSite("www.linuxmi.com")
info.AddDeveloper("Linux迷")
wx.adv.AboutBox(info)
最后一个类是MyApp类继承自wx.App。OnInit()方法通常是创建框架子类对象(frame subclass objects)。
然后开始我们的主循环(main loop)。就是这样。一旦应用程序的主事件循环处理接管,控制权就传递给wxPython。与过程性程序不同,wxPython GUI程序主要响应在其周围发生的事件,这些事件主要由用户用鼠标单击和键盘输入决定。当应用程序中的所有帧都被关闭时,app.MainLoop()方法将返回,程序将退出。
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(parent=None, title="关于Linux迷")
self.frame.Show()
return True
app = MyApp()
app.MainLoop()
运行完整代码,结果如下:
