开发者

resizing of button icons in pyqt4

I want to make im开发者_如何学JAVAage in my QMainWindow so when you click on it you have a signal translating like qpushbutton I use this:

self.quit=QtGui.QPushButton(self)
self.quit.setIcon(QtGui.QIcon('images/9.bmp'))

But the problem is whene I resize the window qpushbutton resized too but not his icon,


Qt won't stretch your image for you - and it's best this way. I recommend to keep the pushbutton of a constant size by adding stretchers to the layout holding it. A resizable pushbutton isn't very appealing visually, and is uncommon in GUIs, anyway.

To make a clickable image, here's the simplest code I can think of:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class ImageLabel(QLabel):
    def __init__(self, image, parent=None):
        super(ImageLabel, self).__init__(parent)
        self.setPixmap(image)

    def mousePressEvent(self, event):
        print 'I was pressed'    


class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.create_main_frame()       

    def create_main_frame(self):
        name_label = QLabel("Here's a clickable image:")
        img_label = ImageLabel(QPixmap('image.png'))

        vbox = QVBoxLayout()
        vbox.addWidget(name_label)
        vbox.addWidget(img_label)

        main_frame = QWidget()
        main_frame.setLayout(vbox)
        self.setCentralWidget(main_frame)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    app.exec_()

Just replace image.png with your image filename (acceptable format by QPixmap) and you're set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜