您的位置:首页 > 编程语言 > Python开发

wxPython笔记(Getting started with wxPython 2)

2015-11-11 09:33 387 查看
Getting started with wxPython 2:http://wiki.wxpython.org/Getting%20Started

1.界面元素:

1)菜单栏:wx.MenuBar,状态栏:wx.StatusBar,工具栏:wx.ToolBar

2)各种控件:Sub-classes of wx.Control,如wx.Button, wx.StaticText, wx.TextCtrl and wx.ComboBox

3)控件容器:wx.Panel,可将控件放置到容器中

4)界面元素的层次结构:作为顶层窗口wx.Frame可包含多个Panel,每个Panel可放置多种控件

2.界面元素布局的4种方式:

1)人工布局:精确控制每个控件的大小和位置,不推荐;

2)wx.LayoutConstraints:复杂

3)wx.LayoutAnchors:更容易的使用wx.LayoutConstraints

4)wxSizer:更常用

3.wxSizer不是窗口对象,只是一个控制布局的对象:

1)wxSizer可为每个控件计算合适的大小;

2)遵循设置的布局规则放置各个控件元素;

3)跟随外部窗口的缩放动态调整控件元素的大小和位置

4)存在多种Sizer,如:wx.BoxSizer, 按照水平行或垂直列的方式布局元素;wx.GridSizer, 按照网格方式布局元素;wx.FlexGridSizer,
更灵活的网格布局方式。

示例代码:

import wx
import os

class MainWindow(wx.Frame):
def __init__(self, parent, title):
self.dirname=''

# A "-1" in the size parameter instructs wxWidgets to use the default size.
# In this case, we select 200px width and the default height.
wx.Frame.__init__(self, parent, title=title, size=(200,-1))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.CreateStatusBar() # A Statusbar in the bottom of the window

# Setting up the menu.
filemenu= wx.Menu()
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")

# Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.

# Events.
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)

self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.buttons = []
for i in range(0, 6):
self.buttons.append(wx.Button(self, -1, "Button &"+str(i)))
self.sizer2.Add(self.buttons[i], 1, wx.EXPAND)

# Use some sizers to see layout options
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.control, 1, wx.EXPAND)
self.sizer.Add(self.sizer2, 0, wx.EXPAND)

#Layout sizer<span style="font-size:10px;">s, <span style="font-family: sans-serif;">might be shorten to one line</span> window: self.S</span>etSizerAndFit(sizer)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
self.Show()

def OnAbout(self,e):
# Create a message dialog box
dlg = wx.MessageDialog(self, " A sample editor \n in wxPython", "About Sample Editor", wx.OK)
dlg.ShowModal() # Shows it
dlg.Destroy() # finally destroy it when finished.

def OnExit(self,e):
self.Close(True)  # Close the frame.

def OnOpen(self,e):
""" Open a file"""
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.control.SetValue(f.read())
f.close()
dlg.Destroy()

app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: