wxPython的一个简单例子

简介

wxPython是python众多的GUI平台的其中一个。其他的平台有如Tkinter,PythonWin,Java Swing,PyGTK,PyQt等。按照个人喜好选择一款或多款GUI平台使用。

下载安装wxPython

pip install wxPython

简单示例

#!/usr/bin/python3

import wx


WIN_NAME    = "简单的窗口"
WIN_SIZE    = (400, 300)
BUTTON_LABEL_DISP = "显示"
BUTTON_LABEL_EXIT = "退出"


def win_disp(event):

    content = text_filename.GetValue()
    text_contents.SetValue(content)


def win_exit(event):

    exit(0)


"""
button_disp = wx.Button(win, label="显示", pos=(200, 5), size=(80, 25))
button_exit = wx.Button(win, label="退出", pos=(300, 5), size=(80, 25))

text_filename = wx.TextCtrl(win, pos=(5,  5), size=(200, 25))
text_contents = wx.TextCtrl(win, pos=(5, 35), size=(300, 250), 
                           style=wx.TE_MULTILINE | wx.HSCROLL | wx.VSCROLL)
"""

# Init Task
#
app = wx.App()

# Init Window
#
win = wx.Frame(None, title=WIN_NAME, size=WIN_SIZE)
bkg = wx.Panel(win) # 背景面板组件,创建一些嵌套的box尺寸器

# Init Element
#
button_disp = wx.Button(bkg, label=BUTTON_LABEL_DISP)
button_disp.Bind(wx.EVT_BUTTON, win_disp)

button_exit = wx.Button(bkg, label=BUTTON_LABEL_EXIT)
button_exit.Bind(wx.EVT_BUTTON, win_exit)

text_filename = wx.TextCtrl(bkg)
text_contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE | wx.HSCROLL)

# InitSizer
#
# 水平相对布局
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(text_filename, proportion=1, flag=wx.EXPAND)
hbox.Add(button_disp,   proportion=0, flag=wx.LEFT, border=5)
hbox.Add(button_exit,   proportion=0, flag=wx.LEFT, border=5)

# 垂直相对布局
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox,          proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(text_contents, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5) 

bkg.SetSizer(vbox)



# show win and run task
#
win.Show()
app.MainLoop()

你可能感兴趣的:(python)