`
wwty
  • 浏览: 543011 次
  • 性别: Icon_minigender_1
  • 来自: 北京-郑州
社区版块
存档分类
最新评论

wxPython的Controls组件学习1----Menus and Toolbars

阅读更多

To implement a menubar in wxPython we need to have three things. A wx.MenuBar, a wx.Menu and a wx.MenuItem.


1、创建一个MenuBar对象

menubar = wx.MenuBar()

 

2、创建一个菜单

file = wx.Menu()
file.Append(-1, 'Quit', 'Quit application')

 

The first parameter is the id of the menu item.

The second parameter is the name of the menu item.

The last parameter defines the short helpstring that is displayed on the statusbar

 

menubar.Append(file, '&File')
self.SetMenuBar(menubar)

 

3、添加MenuItem元素:

注意:

MenuItem Append(self, id, text, help, kind) 
MenuItem AppendCheckItem(self, id, text, help) 
MenuItem AppendItem(self, item) 
MenuItem AppendMenu(self, id, text, submenu, help) 
MenuItem AppendRadioItem(self, id, text, help) 
MenuItem AppendSeparator(self) 

 

Menu的append方法返回的都是MenuItem类型的,这就说明Menu是一个菜单列表,声明一个MenuItem,作为Menu的一个元素。

 

 Append方法只是添加了一个普通的MenuItem元素,而AppendItem则是这样的:

 menubar = wx.MenuBar()
 file = wx.Menu()
 #为菜单设置小图标
 quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')
 quit.SetBitmap(wx.Bitmap('icons/exit.png'))
 file.AppendItem(quit)

 

下面这段话是对AppendItem的一个说明:

If we want to add shortcuts and icons to our menus, we have to manually create a wx.MenuItem. So far we have created menuitems indirectly. The & character specifies an accelerator key. The following character is underlined. The actual shortcut is defined by the combination of characters. We have specified Ctrl + Q characters. So if we press Ctrl + Q, we close the application. We put a tab character between the & character and the shortcut. This way, we manage to put some space between them. To provide an icon for a menuitem, we call a SetBitmap() method. A manually created menuitem is appended to the menu by calling the AppendItem() method.

 

可以这样理解:

一个MenuBar可以放置多个Menu,一个Menu可以放置多个MenuItem作为其元素。
 

self.Bind(wx.EVT_MENU, self.OnQuit, Source)

第三个参数Source就是指定一个MenuItem本身,当然也可以通过id来制定一个元素。

 

 

4、子菜单:

file.AppendSeparator()

 
A menu separator is appended with the AppendSeparator() method.

 

 imp = wx.Menu()
 imp.Append(-1, 'Import newsfeed list...')
 imp.Append(-1, 'Import bookmarks...')
 imp.Append(-1, 'Import mail...')

 file.AppendMenu(-1, 'I&mport', imp)

 

Creating a submenu is trivial.
First, we create a menu.
Then we append menu items.
A submenu is created by calling the AppenMenu() on the menu object.

 

5、其他样式的MenuItem

normal item
check item
radio item

 

6、Context menu------单击鼠标右键而产生的环境菜单

 

 

 

2>工具栏:

1、创建工具栏:
ToolBar一般利用wx.Frame的CreateToolBar方法来创建
toolbar = self.CreateToolBar()
toolbar.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap('../icons/exit.png'))
toolbar.Realize()
After we have put our items to the toolbar, we call the Realize() method. Calling this method is not obligatory on Linux. On windows it is.

 

2、If we want to create more than one toolbars, we must do it differently.

        vbox = wx.BoxSizer(wx.VERTICAL)

        toolbar1 = wx.ToolBar(self, -1)
        toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/new.png'))
        toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/open.png'))
        toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/save.png'))
        toolbar1.Realize()

        toolbar2 = wx.ToolBar(self, -1)
        toolbar2.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap('../icons/exit.png'))
        toolbar2.Realize()

        vbox.Add(toolbar1, 0, wx.EXPAND)
        vbox.Add(toolbar2, 0, wx.EXPAND)

        self.Bind(wx.EVT_TOOL, self.OnExit, id=wx.ID_EXIT)

        self.SetSizer(vbox)

 

3、Sometimes we need to create a vertical toolbar. Vertical toolbars are often seen in graphics applications like Inkscape or Xara Xtreme.

 toolbar = self.CreateToolBar(wx.TB_VERTICAL)
 toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/select.gif'))
 toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/freehand.gif'))
 ...

 

4、In the following example, we will show, how we can enable and disable toolbar buttons. We will also see a separator line.

In our example, we have three toolbar buttons. One button is for exiting the application. The other two buttons are undo and redo buttons. They simulate undo/redo functionality in an application. (for a real example, see tips and tricks) We have 4 changes. The undo and redo butons are disabled accordingly.  

 self.toolbar.EnableTool(wx.ID_REDO, False)
 self.toolbar.AddSeparator()

In the beginning, the redo button is disabled. We do it by calling the EnableTool() method. We can create some logical groups within a toolbar. We can separate various groups of buttons by a small vertical line. To do this, we call the AddSeparator() method.

 

 def OnUndo(self, event):
     if self.count > 1 and self.count <= 5:
         self.count = self.count - 1

     if self.count == 1:
         self.toolbar.EnableTool(wx.ID_UNDO, False)

     if self.count == 4:
         self.toolbar.EnableTool(wx.ID_REDO, True)

 

We simulate undo and redo functionality. We have 4 changes. If there is nothing left to undo, the undo button is disabled. After undoing the first change, we enable the redo button. Same logic applies for the OnRedo() method.

 

 

  • 大小: 31.8 KB
分享到:
评论

相关推荐

    wxPython2.8-win64-unicode-2.8.12.1-py27 wxPython2.8-win32-unicode-2.8.12.1-py27

    用户可以通过运行这些文件来安装对应的wxPython版本,它们会将必要的库文件和组件添加到系统中,使得Python项目能够导入并使用wxPython。 **使用wxPython** 使用wxPython开发应用程序时,首先需要在Python环境中...

    wxPython2.8-win32-unicode-2.8.11.0-py27.exe

    wxPython2.8-win32-unicode-2.8.11.0-py27.exe

    wxPython2.8-win64-unicode-2.8.12.1-py27

    【标题】"wxPython2.8-win64-unicode-2.8.12.1-py27" 指的是一个特定版本的wxPython库的安装程序,适用于64位Windows操作系统,并且支持Unicode编码。这个版本是2.8.12.1,针对Python 2.7解释器设计。 【描述】中的...

    wxPython2.8-win32-unicode-2.8.12.1-py27

    super().__init__(None, -1, 'My First wxPython App', size=(300, 200)) self.Show() if __name__ == '__main__': app = wx.App() frame = MyFrame() app.MainLoop() ``` 这段代码会创建一个基本的窗口,...

    wxPython3.0-win64-3.0.2.0-py27(64位系统安装包)

    通过 wxPython,Python 开发者可以创建原生、高性能的桌面应用程序,无需学习不同平台的原生编程接口。 **wxPython 版本信息** 题目中的“wxPython3.0-win64-3.0.2.0-py27”表示这是一个适用于 Python 2.7 的 ...

    wxPython2.8-win32-ansi-2.8.10.1-py26&wxPython2.8-win32-docs-demos-2.8.10.1

    This file includes two exe file for wxpython which are file for installation and documents for reference. IT'S REQUIRED thatPYTHON IS INSTALLED IN your computer.

    wxPython2.8-win64-unicode-2.8.12.1-py27安装文件

    在描述中提到的"robotframework环境搭建所需相关组件",可能指的是使用wxPython作为Robot Framework的GUI测试库,如`RIDE (Robot Framework Integrated Development Environment)`,它是一个用wxPython编写的测试...

    wxPython2.8-win32-docs-demos-2.8.12.1

    wxPython2.8-win32-docs-demos-2.8.12.1 wxPython2.8-win32-docs-demos-2.8.12.1 wxPython2.8-win32-docs-demos-2.8.12.1 wxPython2.8-win32-docs-demos-2.8.12.1 wxPython2.8-win32-docs-demos-2.8.12.1 wxPython...

    wxPython2.8-win32-unicode-2.8.12.1-py27 for python

    wxPython 就是将 wxWidgets 的功能包装成 Python 可用的形式,使得 Python 开发者可以利用这一强大的 GUI 工具,而无需深入学习 C++。 2. **Unicode 支持** "unicode" 在文件名中表示该版本的 wxPython 支持 ...

    python wxPython2.8-win32-docs-demos-2.8.7.1.exe

    This document will help explain some of the major changes in wxPython 2.6 since the 2.4 series and let you know what you need to do to adapt your programs to those changes. Be sure to also check in ...

    wxPython3.0-win64-3.0.0.0

    wxPython3.0-win64-3.0.0.0-py27 方便大家下载不成功的时候

    wxPython2.8-win32-docs-demos-2.8.8.1

    通过阅读文档,开发者可以学习如何有效地组织窗口,如何响应用户交互,以及如何利用wxPython提供的各种组件构建复杂的用户界面。 示例程序部分则是实际应用wxPython库的实例,通常包括各种功能和组件的演示。这些...

    wxPython2.8-win32-unicode-2.8.4.0-py25.exe

    python, 安装包, 来下载试试把, wxPython2.8-win32-unicode-2.8.4.0-py25.exe wxPython2.8-win32-unicode-2.8.4.0-py25.exe

    wxPython2.8-win32-unicode-2.8.9.2-py26.rar

    1. **主要特点**:wxPython具有丰富的控件集,包括对话框、菜单、工具栏、滚动条等,支持事件处理和布局管理。它的API设计友好,易于学习,同时也支持面向对象的编程方式。 2. **Unicode支持**:"unicode"标识表示...

Global site tag (gtag.js) - Google Analytics