In this part of the wxPython tutorial, we will create some simple examples
本节教程中,我们创建一些简单的例子。
We start with a very simple example. Our first script will only show a small window. It won't do much. We will analyze the script line by line. Here is the code
我们从最简单的例子开始,第一段代码只显示一个小窗口。不要紧,我们将一行一行进行代码分析。
import wx app = wx.App() frame = wx.Frame(None,-1,"simple.py") frame.Show() app.MainLoop()
wx.Frame widget is one of the most important widgets in wxPython. It is a container widget. It means that it can contain other widgets. Actually it can contain any window that is not a frame or dialog. wx.Frame consists of a title bar, borders and a central container area. The title bar and borders are optional. They can be removed by various flags.
wx.Frame是wxPython中最重要的部件。它是一个窗口,意思是它可以包含其它部件。实际上它可以包含任意窗口或者对话框。wx.Frame包括一个标题栏,边界,中心区域。标题与边界是可选的。
wx.Frame has the following constructor. As we can see, it has seven parameters. The first parameter does not have a default value. The other six parameters do have. Those four parameters are optional. The first three are mandatory.
wx.Frame具有以下构造函数。像我们看到的,它有七个参数。第一个参数没有默认值,其它六个都有,四个参数可选,头三个是强制性的。
wx.Frame(wx.Window parent, int id=-1, string title='', wx.Point pos = wx.DefaultPosition, wx.Size size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, string name = "frame")
import wx app = wx.App() window = wx.Frame(None,style=wx.MAXIMIZE_BOX|wx.RESIZE_BORDER |wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX) window.Show() app.MainLoop()
We can specify the size of our application in two ways. We have a size parameter in the constructor of our widget. Or we can call the SetSize() method.
有两个方法可以指定程序的大小:1.用指定的参数指定大小;2.用SetSize()方法
import wx class Example(wx.Frame): def __init__(self,parent,title): super(Example,self).__init__(parent,title=title,size=(250,200)) self.Show() if __name__ == '__main__': app = wx.App() Example(None,title='Size') app.MainLoop()
def __init__(self,parent,title): super(Example,self).__init__(parent,title=title,size=(250,200))
import wx class Example(wx.Frame): def __init__(self,parent,title): super(Example,self).__init__(parent,title=title,size=(250,200)) self.Move((800,250)) self.Show() if __name__ == '__main__': app = wx.App() Example(None,title='Size') app.MainLoop()
If we want to center our application on the screen, wxPython has a handy method. The Centre()
method simply centers the window on the screen. No need to calculate the width and the height of the screen. Simply call the method.
import wx class Example(wx.Frame): def __init__(self,parent,title): super(Example,self).__init__(parent,title=title,size=(250,200)) self.Centre() self.Show() if __name__ == '__main__': app = wx.App() Example(None,title='Size') app.MainLoop()