Google

星期五, 九月 26, 2008

The quickest way to build wxWidgets GUIs

Anthemion DialogBlocks is a sizer-based resource editor that quickly gets you creating professional-looking dialogs, wizards and frames for deployment on Windows, Linux, Mac or any other supported wxWidgets platform.

标签: ,

星期五, 九月 05, 2008

wxWidgets控件

http://www.koansoftware.com/en/prd_svil_wxdownload.htm

标签:

星期四, 八月 21, 2008

使用xrc的一点心得

这篇文章老早就想引过来了。写得不错。

首先向大家说明,我学python才一个多月,写了一个程序主 要是为了练习python和wxPython,如果有什么不对的地方还请大家指出。我也没怎么查是否有人写过类似的说明,就当给大家一个参考。我原来写过 c++/gtk/gtkmm的程序,觉得wxPython的图形程序写起来还是很容易的,不过偶得一个同事说我的python程序写 的比较像C++程序,现在正在深入的研究python中。

我要讲的是如何在程序中使用wxPython库,其中包括各种widget的获得,动态的切换widget,也有很多资料的,我就把我使用过程中觉得需要注意的总结一下,其中也说明了整个gui的构建框架

我把程序分成了三个文件
一个 main.py
一个 ui.py
一个 common.py

Main.py 是这样写的

import ui

if __name__ == '__main__':
application = ui.Application(0)
application.MainLoop()
else:
print "Can't import Buildipsl main module,it shoud be run alone!"

也就是在主程序中运行起来图形界面,进入事件循环,比较简单。

Ui.py程序比较长,大概有1000行,就不贴在这了,我主要说明的就是这个部分。

Common.py是一些常用的公共函数,定义的一些存放数据的底层的类,ui界面的数据交互最终都是通过和底层类对象数据交换来实现的,这样层次比较分明,也容易独立修改。

下面看一下ui.py文件中的Application类,也就是main.py中调用的那个类

class Application(wx.App):
def OnInit(self):
self.conf = common.HandleConfig('fic.conf')
self.connection_pool = common.ConnectionPool()
self.resource = xrc.XmlResource('../resource/IPSLBuild.xrc')
self.frame = MainFrame(None, self.resource,self.conf,self.connection_pool)
self.frame.Show()
return True

这个类在构造函数中实列化了一个’配置文件’类对象,一个’连接缓冲池’类对象,并且初始化了一个xrc资源文件类对象,传入MainFrame这个主窗体类。


MainFrame类 ,主窗体的实现类:
先看一下前几行
class MainFrame(wx.Frame):
def __init__(self, parent, resource,setting,pool):
self.res = resource
self.setting = setting
self.connection_pool=pool
self.PostCreate(self.res.LoadFrame(parent, 'MainWindow'))

self.PostCreate(self.res.LoadFrame(parent, 'MainWindow'))
这一行利用xrc资源对象,把在xml文件中定义的窗体load进来,设置本窗体类。

主窗体load进来了,接着就load窗体上的widget
一般的widget都可以使用像下面这样的函数:
self.text_ctrl_build_dir = xrc.XRCCTRL(self,'text_ctrl_build_dir')
也就是使用xrc,XRCCTRL,第一个参数是parent,这里是self,也就是我们load进来的主窗体,后面加的是包含在主窗体里的 widget的XML id 值,这样widget就进来了,由前面的self.text_ctrl_build_dir来引用着。使用和用代码构建出来的widget没什么区别。
但是,这里有例外,xrc.XRCCTRL只能使用在由wxWindow继承出来的widget上,如果widget不是由wxWindow 继承而来的,就获得不到对象引用,比如menu 和 toolbar。Menuitem和toolbar button要使用下面的方式:

Menubar:
self.menubar = self.res.LoadMenuBar('MainWindow_MenuBar')
这里的menubar在XML文件中是个顶层的widget,也就是说这个menubar不是包含在主窗体里的,结构上是和主窗体平级的

一个小提示:
XML文件中顶层的widget都是用LoadXXXX 来获得,非顶层的才使用XRCCTRL,并且XRCCTRL中第一个参数要是以LoadXXXX获得的widget做为直接或间接的父widget.
比如一个panel,如果在XML文件的最顶层,就要使用LoadPanel,如果在一个Frame中,那就要使用XRCCTRL(Frame的引用,’panel xml id’)

Menubar获得了,就把menubar设置在主窗体上
self.SetMenuBar(self.menubar)

获得子菜单用
self.menu_add = self.menubar.FindItemById(xrc.XRCID('menu_item_node_add'))

toolbar可以在主窗体上获得
self.toolbar = xrc.XRCCTRL(self,'MainWdinow_ToolBar')

在使用上,我获得toolbar button主要是要改变其可用和不可用的状态:
self.toolbar.EnableTool(xrc.XRCID("tool_button_node_add"),True)
想要toolbar button不能使用就用False
回调函数我下面再说.

到现在获得widget大家应该都没什么疑惑了吧,再说回调函数
回调函数一般是这样
self.config_panel.Bind(wx.EVT_BUTTON,self.OnConfSearch,id=xrc.XRCID('button_search_node_conf'))

self.Bind(wx.EVT_MENU, self.OnAdd,id=xrc.XRCID('menu_item_node_add'))
self.Bind(wx.EVT_TOOL, self.OnAdd,id=xrc.XRCID('tool_button_node_add'))

第一个是正常的Buttton
第二个是menuitem
第三个是toolbar button
其实都是一样的
父widget.Bind(事件类型,函数名,xml id)
再次说一下上面的menuitem 和toolbar button,其实如果不要像我这样要设置他们的可用和不可用的状态,不用获得他们的对象引用,可以直接绑定的,因为他们用的是xml id来查找的,没有bind在他们对象的引用上.

下面说一个我使用中的需求:
我窗体左边有一个treeview,需要在点左边不同的treeview item的时候,右边可以根据不同的情况变化.
我不想给右边通过代码来实现,因为可能有如下问题
1. 右边可能是个比较复杂的panel,代码量比较大,而且手写代码不容易后来的修改
2. 每次切换都要destory和Create一堆东西,资源的占用比较大
我喜欢完全的界面代码分离,好处多多,所以还是想用xrc的方式来做.
下面是实现步骤

右面我们要操作的实际上是一个splitter window的panel,叫splitter_right_panel

定义一个BoxSizer,用来向里面添东西
self.splitter_right_box_for_guest_panel=wx.BoxSizer(wx.VERTICAL)

初始化的时候先放上一个空panel
self.splitter_right_box_for_guest_panel.Add(wx.Panel(self),1, wx.EXPAND, 0)

设置布局
self.splitter_right_panel.SetAutoLayout(True)

设置容器
self.splitter_right_panel.SetSizer(self.splitter_right_box_for_guest_panel)

调整widget间的布局关系
self.splitter_right_box_for_guest_panel.Fit(self.splitter_right_panel)

把两个要加的panel都变成右边splitter_right_panel的子widget
self.config_panel.Reparent(self.splitter_right_panel)
self.connection_panel.Reparent(self.splitter_right_panel
这一步比较重要,一定要让panel的父widget是他的直接panel,如果是主窗体,显示有问题.
还有这两个panel是xml文件中顶层的panel,要使用LoadPanel加载进来.

初始化基本上就完成了,在切换中的代码是写在treeview中的select changed回调函数中.

self.main_frame.splitter_right_box_for_guest_panel.Detach(0)
self.main_frame.config_panel.Hide()
self.main_frame.connection_panel.Hide()

if ……:
self.main_frame.config_panel.Show()
self.main_frame.splitter_right_box_for_guest_panel.Add(self.main_frame.config_panel,1,wx.EXPAND,0)
else:
self.main_frame.connection_panel.Show()
self.main_frame.splitter_right_box_for_guest_panel.Add(self.main_frame.connection_panel,1,wx.EXPAND,0)


1. 任何时候都先把BoxSizer中的panel分离
2. 两个面板都hide
3. 根据判断加载不同的panel,show出来
这里用到的是Detach(),不要用Destory,panel对象是在xrc 资源文件中的,destory有问题,而且我们就是不想让它被destory,这样最开始得到的panel内部的widget的对象的引用一直都是可用的.

基本的原理我就说完了,写了两个common文件的类在这,是实现ssh连接的,我测试是可用的,还没写好, python异常我还没看,不好意思
使用的是paramiko模块,不过我用起来发现有问题,put和get函数有问题,没办法我自己写的put和get.
远程执行命令的时候不等执行完就退出了,命令在后台执行,这个不是我想要的,不过肯定有方法,只是我还不知道.我还没写完,让大家见笑了,呵呵

class Connection:
def __init__(self,ip,account,pwd):
self.trans = paramiko.Transport((ip, 22))
self.trans.connect(username=account, password=pwd)
def put(self,localfile,remotefile):
sftp=paramiko.SFTPClient.from_transport(self.trans)
data = open(localfile, 'r').read()
print 'remotefile is %s' % remotefile
sftp.open(remotefile, 'w').write(data)
def get(self,remotefile,localfile):
sftp=paramiko.SFTPClient.from_transport(self.trans)
data = sftp.open(remotefile, 'r').read()
open(localfile, 'w').write(data)
def execmd(self,cmd):
chan = self.trans.open_session()
chan.exec_command(cmd)
def active(self):
return self.trans.is_active()
def __del__(self):
self.trans.close()

class ConnectionPool:
def __init__(self):
self.pool={}
self.filelist=['bg','pnp','sub','fp','ep']
self.genscript='fic.pexpect'
def add_connection(self,name,ip,account,pwd):
if self.pool.has_key(name):
return False
try:
con=Connection(ip,account,pwd)
except Exception:
return False
self.pool[name]=con
return True
def del_connection(self,name):
if not self.pool.has_key(name):
return False
else:
del self.pool[name]
def connected(self,name):
if self.pool.has_key(name):
return True
else:
return False
def gen_config(self,name,path):
if not self.pool.has_key(name):
return False
try:
self.pool[name].execmd(r'rm -rf /tmp/%s' % self.genscript)
self.pool[name].put(get_path(self.genscript),'/tmp/%s' % self.genscript)
self.pool[name].execmd('python /tmp/%s' % self.genscript)
self.pool[name].execmd(r'rm -rf /tmp/%s' % self.genscript)
time.sleep(600)
for i in self.filelist:
print os.path.join(path,i)
self.pool[name].get("/tmp/fic/%s" % i,os.path.join(path,i))
except:
return False
return True

http://bbs.chinaunix.net/viewthread.php?tid=858243

标签: , ,

星期四, 七月 24, 2008

XRC方式载入的wxPython对象访问方式

res = xrc.XmlResource(’resource.xrc’)
res.LoadPanel(self, ‘MyPanel’)

self.Bind(wx.EVT_BUTTON, self.OnClose, id=xrc.XRCID(’CloseButton’))
self.Center()

标签: , , ,

星期五, 七月 18, 2008

用wxPython实现Tray Icon

class MyTaskBarIcon(wx.TaskBarIcon):
...

##
# The task bar application
#
class TaskBarApp(wx.Frame):

##
# \brief the constructor
#
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, -1, title, size = (1, 1),
style=wx.FRAME_NO_TASKBAR|wx.NO_FULL_REPAINT_ON_RESIZE)

self.tbicon = MyTaskBarIcon(self)

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475155

标签: , ,

星期二, 七月 15, 2008

11.3.3 什么是box sizer?[wxPython in Action]

box sizer是wxPython所提供的sizer 中的最简单和最灵活的sizer。一个box sizer是一个垂直列或水平行,窗口部件在其中从左至右或从上到下布置在一条线上。虽然这听起来好像用处 太简单,但是来自相互之间嵌套sizer的能力使你能够在每行或每列很容易放置不同数量的项目。由于每个sizer都是一个独立的实体,因此你的布局就有 了更多的灵活性。对于大多数的应用程序,一个嵌套有水平sizer的垂直sizer将使你能够创建你所需要的布局。

图11.11-11.14 展示了几个简单的box sizer的例子。图中所展示的各个框架我们都是手动调整了大小的,以便展示每个sizer是如何响应框架的增大的。图 11.11显示了一个水平的box sizer,图11.12在一个垂直的box sizer显示了现图11.11相同的窗口部件。

图11.11


图11.12


图11.13显示了一个垂直的sizer,其中一个窗口部件被设置成可扩展并填充有效的垂直空间。图11.14展示了一个垂直的sizer,其中的两个窗口部件设置为按不同的比例占据有效的垂直空间。

图11.13


图11.14


生成以上四个sizer框架的示例代码如例11.9所示。

例11.9 产生多个box sizer

import wx
from blockwindow import BlockWindow

labels = "one two three four".split()

class TestFrame(wx.Frame):
title = "none"
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title)
sizer = self.CreateSizerAndWindows()
self.SetSizer(sizer)
self.Fit()

class VBoxSizerFrame(TestFrame):
title = "Vertical BoxSizer"

def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.VERTICAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(200,30))
sizer.Add(bw, flag=wx.EXPAND)
return sizer


class HBoxSizerFrame(TestFrame):
title = "Horizontal BoxSizer"

def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(75,30))
sizer.Add(bw, flag=wx.EXPAND)
return sizer

class VBoxSizerStretchableFrame(TestFrame):
title = "Stretchable BoxSizer"

def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.VERTICAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(200,30))
sizer.Add(bw, flag=wx.EXPAND)

# Add an item that takes all the free space
bw = BlockWindow(self, label="gets all free space", size=(200,30))
sizer.Add(bw, 1, flag=wx.EXPAND)
return sizer

class VBoxSizerMultiProportionalFrame(TestFrame):
title = "Proportional BoxSizer"

def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.VERTICAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(200,30))
sizer.Add(bw, flag=wx.EXPAND)

# Add an item that takes one share of the free space
bw = BlockWindow(self,
label="gets 1/3 of the free space",
size=(200,30))
sizer.Add(bw, 1, flag=wx.EXPAND)

# Add an item that takes 2 shares of the free space
bw = BlockWindow(self,
label="gets 2/3 of the free space",
size=(200,30))
sizer.Add(bw, 2, flag=wx.EXPAND)
return sizer

app = wx.PySimpleApp()
frameList = [VBoxSizerFrame, HBoxSizerFrame,
VBoxSizerStretchableFrame,
VBoxSizerMultiProportionalFrame]
for klass in frameList:
frame = klass()
frame.Show()
app.MainLoop()

box sizer是类wx.BoxSizer的实例,wx.BoxSizer是wx.Sizer的子类,相对于wx.Sizer,wx.BoxSizer几乎没有增加新的方法。wx.BoxSizer的构造函数有一个参数:

wx.BoxSizer(orient)

参 数orient代表该sizer的方向,它的取值可以是wx.VERTICAL或wx.HORIZONTAL。对于box sizer所定义的唯一一个新 的方法是GetOrientation(),它返回构造函数中orient的整数值。一旦一个box sizer被创建后,你就不能改变它的方向了。 box sizer的其它的函数使用本章早先所讨论的一般的sizer的方法。

box sizer的布局算法对待该sizer的主方向 (当构建的时候已被它的方向参数所定义)和次要方向是不同的。特别地,proportion参数只适用于当sizer沿主方向伸缩时,而 wx.EXPAND标记仅适用于当sizer的尺寸在次方向上变化时。换句话说,当一个垂直的box sizer被垂直地绘制时,传递给每个Add()方 法调用的参数proportion决定了每个项目将如何垂直地伸缩。除了影响sizer和它的项目的水平增长外,参数proportion以同样的方式影 响水平的box sizer。在另一方面,次方向的增长是由对项目所使用的wx.EXPAND标记来控制的,所以,在一个垂直的box sizer中的项 目将只在水平方向增长,如果它们设置了wx.EXPAND标记的话,否则这些项目保持它们的最小或最合适的尺寸。图6.7演示了这个过程。

在box sizer 中,项目的比例增长类似于flex grid sizer,但有一些例外。第一,box sizer的比例行为是在窗口部件被添加到该sizer时,使用 proportion参数被确定的——你无需像flex grid sizer那样单独地指定它的增长性。第二,比例为0的行为是不同的。在 box sizer中,0比例意味着该窗口部件在主方向上不将根据它的最小或最合适尺寸被调整尺寸,但是如果wx.EXPAND标记被使用了的话,它仍可 以在次方向上增长。当box sizer在主方向上计算它的项目的布局时,它首先合计固定尺寸的项目所需要的空间,这些固定尺寸的项目,它们的比例为0。 余下的空间按项目的比例分配。

标签: , ,

wxWidgets界面设计工具

[Commerial]
DialogBlocks http://www.anthemion.co.uk/dialogblocks/
wxDesigner http://www.roebling.de/

[Free]
wxGlade http://www.roebling.de/
VisualWx http://visualwx.altervista.org/
XRCed http://xrced.sourceforge.net/

wiki:http://wiki.wxformbuilder.org/
官网:http://wxformbuilder.org/

标签: , ,

wxWidgets的Sizer

当我们需要写自己的dialog的时候,需要布置好控件的位置,界面才美观.可是如果控件比较多的化,难道我们需要对每个都指定坐标么?还有一个重要的问 题是,这个过程不是"所见即所得"!(当然现在wxWidgets也有可视的界面编辑器了,这里不考虑这个).这个对于从VC/Delphi转过来的程序 员会比较不适应,因为IDE有集成界面编辑器,非常方便调整界面.对于写过AWT(java),GTK,QT的人来说,相信都用过类似layout的东 西.wxWidgets中wxSizer就是做这个的.
  我们先说说一些基本的概念.
  1.所有的sizers都是容器.并且是可嵌套的.
  2.item的最小尺寸
  一般的控件最小尺寸就是初试化的时候的默认值.有些控件是可以计算他们的尺寸的(例如Checkbox),但是有些不好计算,例如listbox,宽高都是不确定的,因为可以有滚动条:-).有些则能计算高度,不能计算宽度,比如text control.
  3.item的边框
  包围着控件的周围的空白空间.每个控件都有自己的边框.边框的大小是可以设置的.位置也可以设置的,比如只有左边有边框,或者上下有,左右没有.
  4.item是可对齐的
   可以居中/上/小/左/右等对齐方式.
  5.item是可伸缩的
  一个item一般占用空间包括item的最小尺寸和边框.但是这个是可以伸缩的.
  6.item是可以手工设置为隐藏的
   使用wxSizer::Show函数实现.注意后面接着要调用Layout函数强制更新界面布局.当我们想隐藏部分界面的时候,这个功能非常有用.避免 我们做这样的事情:先从sizer中把控件移除,然后需要的时候在加入.但是要注意的,这个只有wxBoxSizer和wxFlexGridSizer支 持.
  我们在来看看wxWidgets都提供我们哪些类来完成控件的布局任务.
  wxSizer, wxGridSizer, wxFlexGridSizer, wxBoxSizer, wxStaticBoxSizer
  wxSizer是基类,一般我们都不直接使用.
  wxBoxSizer:
  可以指定是水平还是垂直方向排列item.只能二者选一.如果需要横竖混排,就需要嵌套使用.
  wxStaticBoxSizer:
   和wxBoxSizer一模一样,就是多了一个static box做为边框.
  wxGridSizer:
  就是表格状的排布.注意的是,所有的格子的大小都是一样的.具体的尺寸依赖item中最大的一个.
  wxFlexGridSizer:
  wxGridSizer的升级版本,每行的高度和每列的宽度是独立的.具体的大小依赖行/列中最大个高/宽.
  下面通过对wxBoxSizer的Add函数分析来详细说明wxBoxSizer使用方法.
  原形如下:
  wxSizerItem* Add(wxWindow* window, int proportion = 0,int flag = 0, int
  border = 0, wxObject* userData = NULL)
  wxSizerItem* Add(wxSizer* sizer, int proportion = 0, int flag = 0, int
  border = 0, wxObject* userData = NULL)
  一个是针对wxWindow的,一个是嵌套wxSizer用的,其他的参数都是一致的.
  参数说明:
  proportion:
  这个参数是用在wxBoxSizer的.前面说到每个wxBoxSizer是可以指定一个方向的,这个可以认为是wxBoxSizer的主方向.
  如果该参数是1,表示可以在主方向上伸缩需要加入的item.
  flag:
  指定item边框的位置(可以用"|"操作符号组合上/下/左/右).
  指定item对齐方式(可以用"|"操作符号组合上/下/左/右/垂直居中/水平居中).
   还有一个特别的wxEXPAND.指出item在非主方向是否可以伸缩,主方向的伸缩是由proportion指定.这里还有一个特别说明的(对主方向 和非主方向都有效),如果父级的sizer限制了不能伸缩尺寸,子级的sizer是不能更改的,就算设置可以伸缩也是无效的.如果父级的能伸缩,子级的 sizer可以设置为不能伸缩.
  这里就不举具体的例子说明了.确实需要可以参考wxWidgets的帮助中的"Topic
  overviews"中的"Sizer overview"的"Programming with
  wxBoxSizer".本文也是参考"Sizer overview"写的.
http://blog.csdn.net/Utensil/archive/2007/12/19/1953355.aspx

标签: ,

星期五, 六月 20, 2008

如何使用wxPython设计gui

wxPython介绍+一个实用的例子

1. wxPython简介

wxPython是wxWidget的库的一个python的封装。提供了一些库和一些工具。

这样wxPython即有python语言的优点:
语法强悍,少写了不少代码:)

也有wxWidget图形库的优点:
直接拉控件到大概位置就行了,不需要去调整控件的对齐,也不需要关心gui界面是否支持各种分辨率的桌面。而且界面都是可以运行时切换,只要写很少的切换代码。跨平台的图形库...

后悔,我怎么以前会用vb开发gui的 -_-!。

缺点,网上资料还比较少。而且在线文档都是英文的。不过如果英文好的话,到真的无所谓,因为wxPython的安装包本身提供的工具和资料也足够多,足够好了。

2. 开发入门
我使用ultraedit作为编辑器,wxPython自带的XRCed作为编辑xrc文件(一种xml格式的资源文件,类似于VC6中的RC文件)。

文档主要参考wxPython自带的在线文档和demo代码。不过关于如何使用xrc文件设计gui,wxPython自带的资料似乎还不够详细,这也是我写本文的原因。

通常情况下google"cvs def wxpython相关的关键字"可以快速找到开源的源代码参考。

如果这些例子还不够的话,就参考我的程序吧,这是一个商用程序的原型,我相信该程序的内容已经足够丰富了,应该比网上的一些wxPython教程中的hello world程序更有借鉴意义。

3. 源代码
两个文件,main.py是主程序,main.xrc是资源文件。只要安装了了python和wxpython,然后将这两个文件放在同一个目录中,运行main.py就可以了。

http://www.builder.com.cn/2008/0527/887121.shtml

很重要,此文的实现基于.xrc文件。

标签: , ,

wxpython:sizer学习

使用sizer后,mainFrame的大小由sizer的孩子决定

grid sizer:

最简单的一个种sizer,各个栅的大小相同,栅的最大大小取决以栅中最大孩子的尺寸(当一个sizer被创建时,它根据它的孩子的综合的最小尺寸(最小的宽度和最小的高度)隐含地创建一个最小尺寸。),如果一个栅没有孩子,不会占有效空间的.gridsizer不会限制孩子的个数的,即使在开始声明也是一样

=========================

flex grid sizer

默认情况下,当尺寸调整时,它不改变它的单元格的尺寸。

可以指定某方向上的行为(以行或列为其它单位,gridsizer是以整体为单位),每个栅的宽度是该列中宽度最大的项目的宽度,它们的高度是该行中宽度最高的项目的宽度。显式地告诉该sizer该行或列是可扩展的:

当窗体大小变化时,需要显式地告诉该sizer该行或列是可扩展的:
AddGrowableCol(idx, proportion=0)
AddGrowableRow(idx, proportion=0)

SetFlexibleDirection(direction)

SetNonFlexibleGrowMode(mode)

=========================
grid bag sizer

1、能够将一个窗口部件添加到一个特定的单元格。(如果一个栅没有孩子,会占有效空间的)
2、能够使一个窗口部件跨越几个单元格(就像HTML表单中的表格所能做的一样)。

注意这里的Add()方法与以前的看起来有点不同 :

1 Add(window, pos, span=wx.DefaultSpan, flag=0, border=0, userData=None)

2 Add(sizer, pos, span=wx.DefaultSpan, flag=0, border=0, userData=None)

3 Add(size, pos, span=wx.DefaultSpan, flag=0, border=0, userData=None)

4 AddItem(item)
比其它sizer多了一个位置参数:因为它为孩子预留了空间

============== 下面是通用方法 ================

sizer对象的构造方法有两个比较重要的参数:

flag:窗体风格,对齐方式,边框位置

proportion:如果使用窗体大小变化时的空间使用,为一个比例值

孩子指定一个最小的尺寸

窗口的方法

SetMinSize(width, height)

SetSizeHints(minW, minH, maxW, maxH)

使用sizer的SetItemMinSize()方法。它也有三个形式:

SetItemMinSize(window, size)
SetItemMinSize(sizer, size)
SetItemMinSize(index, size)

对sizer添加或移除孩子:

Add(window, proportion=0, flag=0, border=0, userData=None)
Add(sizer, proportion=0, flag=0, border=0, userData=None)
Add(size, proportion=0, flag=0, border=0, userData=None)

Insert(index, window, proportion=0, flag=0, border=0, userData=None)
Insert(index, sizer, proportion=0, flag=0, border=0, userData=None)
Insert(index, size, proportion=0, flag=0, border=0, userData=None)

Prepend(window, proportion=0, flag=0, border=0, userData=None)
Prepend(sizer, proportion=0, flag=0, border=0, userData=None)
Prepend(size, proportion=0, flag=0, border=0, userData=None)

Detach(window)
Detach(sizer)
Detach(index)

管理它的孩子的尺寸和对齐的

调整grid sizer的大小时,每个部件之间的间隙将随之改变,但是默认情况下,窗口部件的尺寸不会变,并且始终按左上角依次排列

添加一个窗口部件到sizer时,可以通过给flag参数一个特定值来调整该窗口部件的尺寸改变行为。
wx.ALIGN_BOTTOM:按照窗口部件被分配的空间(格子)的底部对齐。

wx.ALIGN_CENTER:放置窗口部件,使窗口部件的中心处于其所分配的空间的中心。

wx.ALIGN_CENTER_HORIZONTAL:在它所处的格子中,水平居中。

wx.ALIGN_CENTER_VERTICAL :在它所处的格子中,垂直居中。

wx.ALIGN_LEFT:靠着它所处的格子左边缘。这是默认行为。

wx.ALIGN_TOP:靠着它所处的格子的上边缘。这是默认的行为。

wx.EXPAND:填满它所处的格子空间。

wx.FIXED_MINSIZE:保持固定项的最小尺寸。

wx.GROW:与wx.EXPAND相同。但比之少两个字符,节约了时间。

wx.SHAPED:窗口部件的尺寸改变时,只在一个方向上填满格子,另一个方向上按窗口部件原先的形状尺寸的比列填充。什么方向的sizer就向什么方向增长

管理每个孩子的边框

边框是连续数量的空白空间,方向有:wx.BOTTOM, wx.LEFT, wx.RIGHT, wx.TOP

传递了边框信息到flags参数后,也需要传递边框宽度的像素值给border参数 ,为边框的大小

http://sellroad.w58.ws86.com/jessinio/?p=512

标签: , , ,

星期三, 四月 16, 2008

配置 Python的wxWidgets可视开发环境

注:转载请注明出处

一、下载 Python 2.5.1
  这一步是必须做的,下载 Python 语言的 SDK
  下载地址(直接复制到迅雷):点击下载

  下载完成后安装 Python 2.5.1,注意安装路径中不要有空格,不然会引起一些问题。

二、下载 wxPython 2.8
  这将下载 Python 的可视化开发库,拥有它就可以进行可视化的开发了。
  下载地址:

    Unicode 版:点击下载
    Ansi 版:点击下载

  建议下载 Unicode 的,用 Ansi 版的话,组件标题不能写 Unicode 字符,代码包含 Unicode 字符也会出错。安装时注意,安装路径中不能有空格。
  到这一步为止,类库就齐全了,下面我们需要一个 IDE

三、下载 Boa Constructor
  Python 拥有非常多的开发环境,也就是 IDE。但是到现在为止,能完全支持可视开发的只有 Boa Constructor。使用它可以像使用 Delphi 那样,直接拖出控件进行快速的开发。
  下载地址:点击下载

  Boa Constructor 还是开源的,如果需要它的源码,可以点击下载
  注意,Boa Constructor 的安装路径也不能有空格。

四、配置 Boa Constructor
  打开 Boa Constructor,如果你是中文的系统,IDE 的语言会变成中文,你必须把它修改成英文的,在 工具->语言设置 菜单中将它换成英文。如果使用中文的 IDE,打开,保存文件时会发生错误(感谢纳米Baby提供这一信息,我自己的系统是英文的,试不出此问题)。
  点击 Tools->Python Interpreter Chooser,设置 Python.exe 的所在目录。
  关闭 Boa Constructor并重新启动它,配置即告完成。

五、一个简单的 Hello World 实例

  在工具栏中点击 wx.App 按钮,创建一个 wx 工程,这个工程将生成 2 个文件,一个是工程文件,另一个是窗体文件。

  然后将两个文件分别保存为 HelloWorld.py(工程文件) 和 Form.py(窗体文件)。在 Form.py 的显示窗口下按 F12,调出可视设计器。

  如上图所示,拖入一个 TextCtrl 和一个 Button。设置 TextCtrl 的 Name 为 txtHello,Value 为空。设置 Button 的 Name 为 btnHello,Label 为 Hello。

  然后我们要让 Button 按下时,TextCtrl 显示出 Hello World 字样。所以需要绑定 Button 的 OnButton 事件。在事件管理器中找到 OnButton,然后双击,点击 Apply 按钮。此时代码编辑器中会生成该事件相关的代码。

  如图所示,添加一行代码即可。然后运行程序,就能看到效果了。

标签: ,

TinyXML与wxWidgets

TinyXML 是一個小巧的 C++ XML parser Library,十分容易就可以整合到你的程式,而且學習曲線十分的短。

TiXmlNode

TinyXML 使用 node 的觀念來看待 XML 文件樹,TiXmlNode 就是代表著一個節點的 class。

Load and Save

TinyXML 使用 TiXmlDocument 這個 class 來載入/儲存 xml file。A document binds together all the XML pieces. It can be saved, loaded, and printed to the screen.

TiXmlDocument doc;doc.Load( "myfile.xml" );

存取的時候,要先取得下一層的 Child Element:

TiXmlElement* element = doc.FirstChildElement();

也可以指定標籤來取得特定的 Element,如同下面的例子:

TiXmlElement* element = doc.FirstChildElement("Node");

TiXmlElement

The element is a container class. It has a value, the element name, and can contain other elements, text, comments, and unknowns. Elements also contain an arbitrary number of attributes.
<foo>This is textfoo>

例如上面的片斷,我們可以使用 GetText() 取得被包起來的文字:
const char* str = fooElement->GetText();

TinyXML and wxWidgets

TinyXML 預設以 UTF-8 編碼,wxWidgets 編譯成 Unicode mode 時,傳入的參數需要轉碼,wxString 可以使用 mb_str() 轉碼,就可以與 TinyXML一起使用。

如果要轉換 UTF-8 char * 字串為 wxString,使用下列的方式:

const char* str = fooElement->GetText();wxString myString(str, wxConvUTF8);
http://www.cppblog.com/tx7do/archive/2006/04/24/6153.html

标签: , ,

星期二, 四月 15, 2008

wxWidgets是个好东西

最近一个工具,由于是工具,本来是用SDK编写的,只是功能完成后,对界面不满意,但是苦于手头可用的东东有限。。。

MFC?这玩意儿,我从大三开始研究,把各类宏,消息映射机理,搞得很清楚,看了《深入浅出MFC》不止两次,还搞不出东西,于是又看了 《Programming Windows With MFC》终于好像能搞出一些DEMO, 后来碰到一聊友,说“把MFC扔进垃圾堆,永远不要碰这种东西……”

VCL! 是个好东西,虽然不会Delphi,但C++ Builder也很好用,虽然对C++改了些语法和语意,嘿嘿。 Chuck 和 Anders这两个牛人。

SWT/JFace: 可以说这一年多一直在进行着基于Eclipse的RCP开发,所以对SWT/JFace也很熟悉,而且Java语法优美简洁,比起C++,写起来轻松多了 (不仅是生理上的,而且是心理上的: 这个 new 要不要 delete,不行,应该用RAII, 参数不能是直接是类型,应该改成const引用以避免拷贝…… 哇,不想这些了,自然轻松和愉快)

所以如果对方机子上有JVM,或者允许安装JVM的话, 我会选择SWT/JFace

当然,如果对于大一些的窗口程序,使用 Eclipse RCP 简直酷B了!

偶有一次,在使用Ruby,遇见 wxRuby,这东东的文档中提到了 wxWidgets, 于是就这样结识了它(本人我是古墓派)

于是立马下载,安装,编译,哇噻! 还有许许多多的 samplesssssssssss。除了自带的帮助,我还找到了一本由 Julian Smart(我以后就给我儿子取名叫 "X有钱") 亲自写的《Cross-Platform GUI Programming with wxWidgets》...

然后我把我那个类似于资源管理器的小工具用 wxWidgets 重新写过。 果然不错

我的手头又多了一样武器~~~

http://www.javaeye.com/topic/100094

标签:

星期二, 四月 08, 2008

Introduction to wxWidgets

Introduction

wxWidgets formerly known as wxWindows is a framework for developing cross-platform GUI applications in C++. Julian Smart started the framework in 1992 at the Artificial Intelligence Applications Institute, University of Edinburgh. In 1995, a port to Xt was released by Markus Holzem. In May 1997, the Windows and the GTK+ ports were merged and put into a CVS repository.

What is wxWidgets

wxWidgets gives you a single, easy-to-use API for writing GUI applications on multiple platforms. Link it with the appropriate library for your platform (Windows/Unix/Mac) and compiler (almost any popular C++ compiler), and your application will adopt the look and feel appropriate to that platform. On top of the great GUI functionality, wxWindows gives you: online help, network programming, streams, clipboard and drag and drop, multithreading, image loading and saving in a variety of popular formats, database support, HTML viewing and printing, and much more.

Who should use wxWidgets

wxWidgets is a framework very much similar to MFC, except for a few negative points of its own. Those MFC programmers who are aware of the growing number of Linux users and who want to write cross platform GUI applications can use wxWidgets. With wxWidgets, it is very easy to use a framework based on C++ and it has a proven record of 13 years. In fact, wxWidgets is very stable and is supported on:

  • Windows 3.1, Windows 95/98, Windows NT, Windows 2000/XP, Windows ME, Windows CE.
  • Linux and other UNIX platforms with GTK+.
  • UNIX with Motif or the free Motif clone Lesstif.
  • Mac OS.
  • Embedded platforms are being investigated. See the wxUniversal project.
  • An OS/2 port is in progress, and you can also compile wxWidgets for GTK+ or Motif on OS/2.
http://www.codeproject.com/KB/library/wxwidgets.aspx

标签: ,

wxWidgets组件

http://wxcode.sourceforge.net/complist.php

标签:

wxDb/wxDbTable wxODBC Overview

he wxODBC classes were designed for database independence. Although SQL and ODBC both have standards which define the minimum requirements they must support to be in compliance with specifications, different database vendors may implement things slightly differently. One example of this is that Oracle requires all user names for the datasources to be supplied in uppercase characters. In situations like this, the wxODBC classes have been written to make this transparent to the programmer when using functions that require database-specific syntax.

Currently several major databases, along with other widely used databases, have been tested and supported through the wxODBC classes. The list of supported databases is certain to grow as more users start implementing software with these classes, but at the time of the writing of this document, users have successfully used the classes with the following datasources:

  • DB2
  • DBase (IV, V)**
  • Firebird
  • INFORMIX
  • Interbase
  • MS SQL Server (v7 - minimal testing)
  • MS Access (97, 2000, 2002, and 2003)
  • MySQL (2.x and 3.5 - use the 2.5x drivers though)
  • Oracle (v7, v8, v8i)
  • Pervasive SQL
  • PostgreSQL
  • Sybase (ASA and ASE)
  • XBase Sequiter
  • VIRTUOSO

An up-to-date list can be obtained by looking in the comments of the function wxDb::Dbms in db.cpp, or in the enumerated type wxDBMS in db.h.

**dBase is not truly an ODBC datasource, but there are drivers which can emulate much of the functionality of an ODBC connection to a dBase table. See the wxODBC Known Issues section of this overview for details.

http://docs.wxwidgets.org/2.8.0/wx_odbcoverview.html#wxodbcconfiguringyoursystem

标签: , ,

Runtime class information (aka RTTI) overview

One of the failings of C++ used to be that no run-time information was provided about a class and its position in the inheritance hierarchy. Another, which still persists, is that instances of a class cannot be created just by knowing the name of a class, which makes facilities such as persistent storage hard to implement.

Most C++ GUI frameworks overcome these limitations by means of a set of macros and functions and wxWidgets is no exception. As it originated before the addition of RTTI to the C++ standard and as support for it is still missing from some (albeit old) compilers, wxWidgets doesn't (yet) use it, but provides its own macro-based RTTI system.

In the future, the standard C++ RTTI will be used though and you're encouraged to use whenever possible the wxDynamicCast() macro which, for the implementations that support it, is defined just as dynamic_cast<> and uses wxWidgets RTTI for all the others. This macro is limited to wxWidgets classes only and only works with pointers (unlike the real dynamic_cast<> which also accepts references).

Each class that you wish to be known to the type system should have a macro such as DECLARE_DYNAMIC_CLASS just inside the class declaration. The macro IMPLEMENT_DYNAMIC_CLASS should be in the implementation file. Note that these are entirely optional; use them if you wish to check object types, or create instances of classes using the class name. However, it is good to get into the habit of adding these macros for all classes.

Variations on these macros are used for multiple inheritance, and abstract classes that cannot be instantiated dynamically or otherwise.

DECLARE_DYNAMIC_CLASS inserts a static wxClassInfo declaration into the class, initialized by IMPLEMENT_DYNAMIC_CLASS. When initialized, the wxClassInfo object inserts itself into a linked list (accessed through wxClassInfo::first and wxClassInfo::next pointers). The linked list is fully created by the time all global initialisation is done.

IMPLEMENT_DYNAMIC_CLASS is a macro that not only initialises the static wxClassInfo member, but defines a global function capable of creating a dynamic object of the class in question. A pointer to this function is stored in wxClassInfo, and is used when an object should be created dynamically.

wxObject::IsKindOf uses the linked list of wxClassInfo. It takes a wxClassInfo argument, so use CLASSINFO(className) to return an appropriate wxClassInfo pointer to use in this function.

The function wxCreateDynamicObject can be used to construct a new object of a given type, by supplying a string name. If you have a pointer to the wxClassInfo object instead, then you can simply call wxClassInfo::CreateObject.

http://docs.wxwidgets.org/stable/wx_runtimeclassoverview.html

标签: ,

http://www.xml4wx.org/

XML for wxWidgets is an open source XML parser writen in C++ for use with the wxWidgets cross platform GUI toolkit. This is not the same classes/library included with wxWidgets, but a complete re-write.
  • Designed to be as reasonably standards compliant as possible
  • Includes DOM 1.0, XPath 1.0, XSLT 1.0 and SAX-like implementation
  • Many convienience functions for common functionality makes it easy to use
  • Completely embedable - link your project with the static library
  • Inheritable class structure allows creation of special-use DOM's
  • Easily extendable XPath and XSLT functions so that you can implement the functions you need
  • The open source, relativly simple design makes it easy to solve issues while developing

The biggest feature is that it is a piece of cake to work with an in memory XML Document from your C++ code. Instead of having to write specialized C++ classes/structures to represent your xml, you can simply use the iDOMDocument as-is. You can think of the XML4WX DOM as a giant Hash table that also has a stateless search and reporting engines built in.

Other XML DOMS (xmllib2, Xerces, MS XML) require you to write large ammounts of C++ code to access the data. If your xml changes, you'll have to re-write the container classes and re-compile your application. With XML4WX, if you write your XPath queries carefully, your C++ code will continue to work.

标签: , ,

星期一, 四月 07, 2008

使用wxWidgets的开源项目

http://sourceforge.net/projects/multiget/

标签: ,

Creating Nice Reports with wxWidgets ZIP API

wxWidgets contains a set of classes which handle several archive formats. Most commonly used archive format is ZIP. This tutorial shows how to use wxWidgets API for reading and writing ZIP-files.
As you probably know, Microsoft Office 2007 produces .DOCX files which are ZIP archives which contain several XML files, images and few directories inside. After visiting Microsoft Developer’s Days I decided to add new feature to my current project and create new type of report which is based on .DOCX file format. This tutorial contains some techniques which you can use for generating such reports by yourself.
First of all, we have to open Microsoft Word 2007 and create a stub document. The document which is used in this article contains:
- Heade
- Textual description
- Table with two columns
- Image at the bottom of the table

http://wxwidgets.info/zip_api_create_docx_file_en

标签:

使用wxWidgets的总体框架

下午又尝试了使用wxWidgets做一个稍微完整一点的界面,虽然简单,但都包含了wxWidgets的基本编程"框架"了.说框架夸张了点,吓人用的,^_^
程序里面已经包含了非常详细的注释了,哈哈,突然想起了文学编程!

/////////////////////////

//TextEditorApp.h头文件//

*///////////////////////

#ifndef TEXTEDITORAPP_H
#define TEXTEDITORAPP_H

//每个wxWidgets程序需要定义一个wxApp类的子类
//需要创建并且只能创建一个这个类的实例
//这个实例控制着整个程序的执行!
class TextEditorApp : public wxApp
{
public:
//继承自wxApp的子类至少需要定义OnInit()函数
//这个函数类似于WinMain,wxWidgets准备运行代码时会调用这个函数
virtual bool OnInit();
//这个函数创建至少一个窗口实例,对传入的命令行参数进行解析等
//如果这个函数返回真,wxWidgets将开始时间循环接受/处理用户输入
//如果返回为假,那么wxWidgets将会释放内部分配的资源,然后结束整个的程序的运行
};
//当wxWidgets创建这个TextEditorApp类实例的时候
//会将创建的结果赋值给一个全局变量wxTheApp
//可以直接在程序中使用这个变量,但是需要进行从wxApp到TextEditorApp类型强制转换
//增加了下面一行后,可以调用wxGetApp()函数
//这个函数返回一个到TextEditorApp的实例引用
DECLARE_APP(TextEditorApp)
#endif // TEXTEDITORAPP_H

///////////////////////////

//TextEditorApp.cpp 源文件//

///////////////////////////

// 对支持预编译编译器要包含"wx/wx.h"
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "TextEditorApp.h"
#include "TextFrame.h"
//这里没有创建TextEditorApp的代码!这是在wxWidgets内部实现的!
//你需要告诉wxWidgets创建那个App类实例,就是通过这个宏实现的
IMPLEMENT_APP(TextEditorApp)
//这个宏会检查编译应用程序使用的库文件是否和当前的库文件相匹配

bool TextEditorApp::OnInit()
{
TextFrame *frame = new TextFrame("Simple Text Editor", 100, 100, 400, 300);
frame->Show(TRUE);
SetTopWindow(frame);
return true;
}

上面就是程序的总体框架了,程序就是从这里开始执行的,下面时程序的窗体部分:

///////////////////////////

//TextFrame.h 头文件 //

//////////////////////////

#ifndef _TEXTFRAME_H
#define _TEXTFRAME_H

//一个Frame窗口是一个可以容纳别的窗口的顶层窗口
class TextFrame : public wxFrame
{
public:
//构造函数. 用来创建新的TextFrame
TextFrame(const wxChar *title, int xpos, int ypos, int width, int height);
//析构函数
~TextFrame();

//菜单处理事件
//每个方法都有一个参数用来包含事件的信息
//从菜单获得的事件是一个wxCommandEvent类型的数据
void OnMenuFileOpen(wxCommandEvent &event);
void OnMenuFileSave(wxCommandEvent &event);
void OnMenuFileQuit(wxCommandEvent &event);
void OnMenuInfoAbout(wxCommandEvent &event);
protected:
//要处理事件的每个类都需要声明一个事件表
//宏DECLARE_EVENT_TABLE用来完成这项工作
DECLARE_EVENT_TABLE()
private:
wxTextCtrl *m_pTextCtrl;
wxMenuBar *m_pMenuBar;
wxMenu *m_pFileMenu;
wxMenu *m_pInfoMenu;
enum
{
MENU_FILE_OPEN,
MENU_FILE_SAVE,
MENU_FILE_QUIT,
MENU_INFO_ABOUT
};
};
#endif //_TEXTFRAME_H

///////////////////////

// TextFrame.cpp源文件//

///////////////////////

// 对支持预编译编译器要包含"wx/wx.h"
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "TextFrame.h"
TextFrame::TextFrame(const wxChar *title, int xpos, int ypos, int width, int height)
: wxFrame((wxFrame *) NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height))
{
//假如想给程序加上图标,应该使用下面的语句
//SetIcon(wxIcon(mondrian_xpm));
//此前应该加入一个xpm格式的图片
//#include "modrian.xpm"
//xpm其实是一个ASCII编码的完全符合c++语法的文本文件,所以可以直接用c++的方式包含到代码中
//SetIcon使用mondrian_xpm变量在堆栈上创建了一个图标
//这个mondrian变量是在mondrian.xpm文件中定义的


//编辑框
m_pTextCtrl = new wxTextCtrl(this, -1, wxT(wxString("测试")), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);

// 菜单条
m_pMenuBar = new wxMenuBar();

// 文件菜单
m_pFileMenu = new wxMenu();

//wxT宏可以让代码兼容Unicode模式,和宏_T的作用一样
//_()将字符串翻译为指定语言的版本
//&符号的前导的字符将成为彩旦的快捷操作符,实际中为一下划线
//t符号则表示一个全局的快捷键
m_pFileMenu->Append(MENU_FILE_OPEN, wxT("O&打开\tF2"), wxT("打开文件"));
m_pFileMenu->Append(MENU_FILE_SAVE, wxT("S&保存\tF3"), wxT("保存文件"));
m_pFileMenu->AppendSeparator();
m_pFileMenu->Append(MENU_FILE_QUIT, wxT("Q&退出\tF4"), wxT("退出系统"));
m_pMenuBar->Append(m_pFileMenu, wxT("F&文件"));

// 关于菜单
m_pInfoMenu = new wxMenu();
m_pInfoMenu->Append(MENU_INFO_ABOUT, wxT("A&关于\tF5"), wxT("关于软件"));
m_pMenuBar->Append(m_pInfoMenu, wxT("I&信息"));

//设置程序菜单条
SetMenuBar(m_pMenuBar);

//创建一个有三个域的状态栏。第一个域包含文本"Ready"
//状态栏用来显示对一个菜单项的描述。
CreateStatusBar(3);
SetStatusText("Ready",0);
}
TextFrame::~TextFrame()
{
}

//事件表是放在实现文件中的
//宏BEGIN_EVENT在事件表声明的开始处使用
BEGIN_EVENT_TABLE(TextFrame, wxFrame)
//将一个方法与事件关联要使用EVT_MENU宏
//这个宏需要菜单ID与事件名
EVT_MENU(MENU_FILE_OPEN, TextFrame::OnMenuFileOpen)
EVT_MENU(MENU_FILE_SAVE, TextFrame::OnMenuFileSave)
EVT_MENU(MENU_FILE_QUIT, TextFrame::OnMenuFileQuit)
EVT_MENU(MENU_INFO_ABOUT, TextFrame::OnMenuInfoAbout)
END_EVENT_TABLE()
//事件表的最后用宏END_EVENT_TABLE作结束标记

void TextFrame::OnMenuFileOpen(wxCommandEvent &event)
{
wxLogMessage(wxT("File Open Menu Selected"));
}

void TextFrame::OnMenuFileSave(wxCommandEvent &event)
{
wxLogMessage(wxT("File Save Menu Selected"));
}

void TextFrame::OnMenuFileQuit(wxCommandEvent &event)
{
//Close函数并不直接关闭Frame窗口,而是产生了一个wxEVT_CLOSE_WINDOWS事件
//这个事件默认的处理函数调用wxWindow::Destory函数释放了frame窗口
Close(FALSE);
//当点击关闭按钮时,wxWidgets也会调用close()函数,给frame窗口发一个wxEVT_CLOSE_WINDOWS事件
//可以在程序中拦截这个事件,改变默认的处理行为!
}

void TextFrame::OnMenuInfoAbout(wxCommandEvent &event)
{
wxLogMessage(wxT("File About Menu Selected"));
}

上面部分就是主窗体,主要包含了菜单,状态栏,编辑框等控件,还包含了事件响应部分,感觉这部分有点像MFC的处理方式。

代码很少(代码是看例子写的),也很简洁,就不用说明了吧(上面还不够详细?)有问题请留言。

http://blog.chinaunix.net/u/25096/showart_375024.html

标签:

星期五, 四月 04, 2008

在Code::Blocks上使用wxWidgets-2.8.0 (二)——开发篇

终于进入正题了,我们先建立一个最简单的demo:
(1) 建立工程
用new projects建立一个wxWidgets application,在project option里使用Using UNICODE static wxWidgets libraries,当然如果你编译的是其他类型的库就选其他的。
这时建立的工程使用的却都是wxWidgets-2.6.0的配置,你需要自己修改wxWidgets的配置。
在左边栏的工程上点右键->Build Options进行配置.
(2) 配置Custom variables
WX_DIR配置为wxWidgets config时设的--prefix的目录,我的就是mingw啦。
WX_CFG配置为$(WX_DIR)\bin\wx-config。
(3) 配置directory
Compiler:
$(WX_DIR)\include\wx-2.8
$(WX_DIR)\lib\wx\include\msw-unicode-release-static-2.8

Linker:
$(WX_DIR)\lib

Resource compiler
$(WX_DIR)\include\wx-2.8
(4) 配置Linker
将原来的mswxu去掉,改成wx_mswu-2.8
再加上lib目录下的其他几个libwx开头的库,如wxpng-2.8。
注意这些库一定要排在所有库的最前面,否则link的时候仍然会出错。
(5) 编译运行
最重要的时刻到了, 点击Build and Run吧!怎么样,出错了吧,因为还要改一些小地方:)
这肯定是CB的bug了,你会看到new wxMenu时报错, 应该把""改成_(""), 这是因为我们现在建立的是Unicode程序,wxWidgets处理的字符串全都要加上"_()"这个宏.
现在可以运行了吧~
使用CB的wxSmith插件可以进行RAD开发, 大家可以看看这篇文章:
http://wiki.codeblocks.org/index.php?title=WxSmith_Tutorial_%26_Pointers
写的很详细,不过仍然是基于wx 2.6.0的,如果用在2.8的库上会出一些小错误,不过很好改,有空的话我再写吧~

(6) something about CB
CB的编辑器有个问题,就是对中文的支持不是很好,比如要把窗口的标题使用中文字符串就不行,不过这只是编辑器的问题,我们换个支持UTF-8的编辑器编辑这个源文件就可以了,比如emacs23:) 这样中文就能用在你的wxWidget里了吧~
最后强烈推荐Code::Blocks,它的开发团队非常活跃,每天都会有进展,这点非常重要,听说Dev-CPP的原创人之一都已经跳槽到CB开发了~ 希望更多的人能支持这个IDE,毕竟开源界太缺少这样的工具了,虽然写代码我一般用emacs,但是要开发GUI程序的话,还是有个RAD工具最好了~

http://aniluke.spaces.live.com/blog/cns!63F0E8C8F9B72D8A!412.entry

标签: , ,

星期三, 三月 26, 2008

wxWidgets研究目标

wxWidgets学习日志:

显示Splash窗口/已经OK.
显示URL,鼠标经过高亮,点击打开浏览器进入网址,不大好找资料,先不研究类库,偷个懒。

标签:

星期二, 三月 25, 2008

wxWidgets类列表(2.6.4)

管理窗口