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,
更灵活的网格布局方式。
示例代码:
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()
相关文章推荐
- python开发之tkinter实现图形随鼠标移动的方法
- Python知识小点(备注)
- 每天一点python——python数据类型
- Python 一篇学会多线程
- python开发之tkinter实现图形随鼠标移动的方法
- python开发之基于thread线程搜索本地文件的方法
- python开发之thread实现布朗运动的方法
- python开发之thread线程基础实例入门
- Python基于pygame实现图片代替鼠标移动效果
- Python中pygame安装方法图文详解
- Python基于pygame实现的弹力球效果(附源码)
- python基于pygame实现响应游戏中事件的方法(附源码)
- python中pygame针对游戏窗口的显示方法实例分析(附源码)
- Python基于pygame实现的font游戏字体(附源码)
- Python中pygame的mouse鼠标事件用法实例
- Python 中的位置参数和关键字参数分析
- Python学习笔记(6),实践
- Python笔记,简略版
- 简略的笔记 Numpy
- Python多进程编程(转)