wxPython第一步

First Steps第一步

In this part of the wxPython tutorial, we will create some simple examples

本节教程中,我们创建一些简单的例子。

Simple example简单例子

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()
这是我们的第一个例子
import wx:导入wxPython模块。

app=wx.App():每个wxPython程序必须

frame = wx.Frame(None,-1,"simple.py")
frame.Show()
我们创建一个frame,它就是一个wx.Frame
然后调用它的Show()方法让它显示出来。

最后一行进入mainloop语句。这个mainloop语句是无休止的循环。

wx.Frame

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()

看上面这段代码,运行起来时,窗口没有了最小化。

Size and Position尺寸与位置

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()
在这个示例中,应用程序将为250 x200 px
def __init__(self,parent,title):
        super(Example,self).__init__(parent,title=title,size=(250,200))

同样地:我们可以指定程序的位置。
Move(wx.Point point):移动窗口至指定位置。
MoveXY(int x,int y):移动窗口至指定位置。
SetPosition(wx.Point point):设置窗口的位置。
SetDimensions(wx.Point point,wx.Size size):位置与大小

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()

Centering on the screen

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()





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值