I need to show a '&' symbol on a button and I don't understand this -

how come

self.l1 = QLabel("&", self)
        self.l2 = QLabel(unichr(int("0026", 16)), self)

both of these show it on a label but

self.b1 = QPushButton("&", self)
        self.b2 = QPushButton(unichr(int("0026", 16)), self)

none of these show the symbol on a button? Nor do these

self.b1 = QPushButton()
        self.b2 = QPushButton()
        self.b1.setText("&")
        self.b2.setText(unichr(int("0026", 16)))

Am I missing anything? Is there any way to show it on a button?

Thank you.

try using the escape clause, maye "&" gives the button a specific tag or function. try "\&" and see if that works, im not very familiar with that toolkit so thats a shot in the dark suggestion.

good luck.

Most GUI toolkits have a keyboard option for a button, implemented by the '&' character. So, if your button label is "E&xit", only "Exit" shows on the button. However pressing alt/x on the keyboard will activate the button too. You may not be able to display the '&' on a button.

Oh, I know this, just forgot about it. Now it makes sense.

The escape thing doesn't work either. If not even unicode shows it, then I don't know what will.

So, there's no other way to show a '&'?

Oh, I know this, just forgot about it. Now it makes sense.

The escape thing doesn't work either. If not even unicode shows it, then I don't know what will.

So, there's no other way to show a '&'?

Try this:

self.b1 = QPushButton("&&Hello", self)

Simple Qt code:

# -*- coding: utf-8 -*-			
#!/usr/bin/env python
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import * 
 
a = QApplication(sys.argv)
 
# Our function to call when the button is clicked
def sayHello():
    print "Hello, World!"
 
# Instantiate the button
hellobutton = QPushButton("Say&& 'Hello world!'",None)
 
# And connect the action "sayHello" to the event "button has been clicked"
a.connect(hellobutton, SIGNAL("clicked()"), sayHello)
 
# The rest is known already...
hellobutton.show()
a.exec_()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.