如何写一个“&”在wxpython中的按钮文本中

时间:2011-01-12 10:48:55

标签: python wxpython

这看起来很简单但我没有找到任何文档。试过&&,这是行不通的。想要一个这样的按钮:

Button1=wx.Button(self, 5, "abc&abc", (130, 230))

2 个答案:

答案 0 :(得分:5)

对我有用:

import wx

class MainWindow(wx.Frame):
  def __init__(self,parent,title):
    wx.Frame.__init__(self,parent,title=title,size=(200,-1))
    self.sizer = wx.BoxSizer(wx.HORIZONTAL)
    self.buttons = [
      wx.Button(self,-1,"Button &One"),
      wx.Button(self,-1,"Button &&Two"),
    ]
    for btn in self.buttons:
      self.sizer.Add(btn,1,wx.EXPAND)
    self.SetSizer(self.sizer)
    self.SetAutoLayout(1)
    self.sizer.Fit(self)
    self.Show()

app = wx.App(False)
frame = MainWindow(None,"Hello Ampersand")
app.MainLoop()

答案 1 :(得分:0)

您的代码工作MattH但我的问题实际上是我在当时菜单栏的点击事件中显示按钮&&不工作..检查下面的代码......在这种情况下应该做什么

import wx

class MainWindow(wx.Frame):
  def __init__(self,parent,title):     
    wx.Frame.__init__(self,parent,title=title,size=(699, 570))     

    fileMenu= wx.Menu()
    folder=wx.MenuItem(fileMenu, 1, "&Start","Click here to start..")

    fileMenu.AppendItem(folder)
    about = wx.MenuItem(fileMenu, 2, '&About',"Test")

    fileMenu.AppendItem(about)
    quit = wx.MenuItem(fileMenu, 3, '&Quit',"Terminate the program")

    fileMenu.AppendItem(quit)
    menuBar = wx.MenuBar()
    menuBar.Append(fileMenu,"&File")    
    self.Bind(wx.EVT_MENU, self.ShowButton, folder)

    self.SetMenuBar(menuBar)     
    pndSummaryButton = wx.Button(self, 3, "Button &&Two", (130, 146))
    pndSummaryButton.name="pndSummary"

    self.printArea2 = wx.TextCtrl(self,pos = (290, 146), size = (255, 25),style = wx.TE_READONLY)
    pndSummaryButton.SetFont(wx.Font(11, wx.SWISS, wx.NORMAL,wx.LIGHT))
    self.printArea2.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL,wx.LIGHT))
    pndSummaryButton.SetBackgroundColour(wx.Colour(153,0,0))
    pndSummaryButton.SetForegroundColour(wx.Colour(255,255,255))
    pndSummaryButton.SetSize(pndSummaryButton.GetBestSize())

    self.Show()  

  def ShowButton(self,event):
    pndSummaryButton = wx.Button(self, 3, "Button &&Two", (130, 176))
    pndSummaryButton.name="pndSummary1"
    self.printArea2 = wx.TextCtrl(self,pos = (290, 176), size = (255, 25),style = wx.TE_READONLY)
    pndSummaryButton.SetFont(wx.Font(11, wx.SWISS, wx.NORMAL,wx.LIGHT))
    self.printArea2.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL,wx.LIGHT))
    pndSummaryButton.SetBackgroundColour(wx.Colour(153,0,0))
    pndSummaryButton.SetForegroundColour(wx.Colour(255,255,255))
    pndSummaryButton.SetSize(pndSummaryButton.GetBestSize())

app = wx.App(False) 
frame = MainWindow(None,"Hello Ampersand") 
app.MainLoop()