如何在wxpython中创建wx.ITEM_DROPDOWN

时间:2017-11-17 02:18:12

标签: wxpython

我是新手wxpython,我想在工具栏中创建一个选项按钮,代码在这里,但他没有生效,我不知道是什么错了,你能给我一些建议吗,谢谢

import wx


class Mywin(wx.Frame):
    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title=title)
        self.InitUI()
    def InitUI(self):
        menubar = wx.MenuBar()
        menu = wx.Menu()
        menubar.Append(menu, "File")
        self.SetMenuBar(menubar)
        tb = wx.ToolBar(self, -1,style=wx.TB_TEXT|wx.TB_FLAT|wx.TB_HORZ_TEXT|wx.TB_HORZ_LAYOUT|wx.TB_TOP)#wx.TB_BOTTOM wx.TB_DOCKABLE
        tb.SetToolSeparation(22)
        tb.AddTool(101, 'what', wx.Bitmap("中国电信.png"), "开始",wx.ITEM_DROPDOWN)
        menu = wx.Menu()
        item = wx.MenuItem(menu, 222, "查看", "跳转到url")
        menu.AppendItem(item)
        bool = tb.SetDropdownMenu(101,menu)
        print(bool)
        tb.Bind(wx.EVT_TOOL_DROPDOWN,self.OnDropDown)
        tb.Bind(wx.EVT_TOOL, self.Onright)
        tb.Realize()
        self.SetSize((700, 250))

        #self.text = wx.TextCtrl(self, -1, style=wx.EXPAND | wx.TE_MULTILINE)
        self.Centre()
        self.Show(True)
    def OnDropDown(self,event):
        print(event)

    def Onright(self, event):
        print(event)
        self.text.AppendText(str(event.GetId()) + "\n")
        event.Skip()



ex = wx.App()
Mywin(None, 'ToolBar demo')
ex.MainLoop()

1 个答案:

答案 0 :(得分:0)

这是演示,你能告诉我你的版本,2.7还是3.0?

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
ZetCode wxPython tutorial

This example shows a simple menu.

author: Jan Bodnar
website: www.zetcode.com
last modified: September 2011
'''
import wx

class Example(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs) 

        self.InitUI()

    def InitUI(self):    

        menubar = wx.MenuBar()
        fileMenu = wx.Menu()
        fitem = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
        menubar.Append(fileMenu, '&File')
        self.SetMenuBar(menubar)

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

        self.SetSize((300, 200))
        self.SetTitle('Simple menu')
        self.Centre()
        self.Show(True)

    def OnQuit(self, e):
        self.Close()

def main():
    ex = wx.App()
    Example(None)
    ex.MainLoop()    


if __name__ == '__main__':
    main()