Miguel Duran Diaz
Miguel Duran Diaz

Reputation: 312

Resize Widgets with PyQT

I'm a beginner of Python and PyQT. I'm trying to use QGridLayout to make the GUI, however I've got some problems when I place an image. This is how it looks without the image:

pic without image

Pretty good for me. However, when I try to add an image in the upper-right corner I get:

pic with image

Clearly isn't what I want (I'd like that part of the widget the same size as the first image i.e approximately the size of the original title). The code is a bit long so I post just the part of the grid:

    wid = QWidget(self)
    self.setCentralWidget(wid)
    grid = QGridLayout()
    title = QLabel(self)
    newfont = QFont("Times", 20, QFont.Bold)
    title.setText('Interfaz Gráfica PIC18F4550')
    title.setFont(newfont)
    title.setAlignment(Qt.AlignCenter)

    pic = QLabel(self)
    pic.setPixmap(QtGui.QPixmap('escudo.gif'))

    m = PlotCanvas(self, width=5, height=4)
    connect_btn = QPushButton('Conectar', self)
    connect_btn.setIcon(QIcon('lautaro.jpeg'))
    connect_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    exit_btn = QPushButton('Salir', self)
    exit_btn.setIcon(QIcon('lautaro.jpeg'))
    exit_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    disconnect_btn = QPushButton('Desconectar', self)
    disconnect_btn.setIcon(QIcon('lautaro.jpeg'))
    disconnect_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    frequency_btn = QPushButton('Frecuencia', self)
    frequency_btn.setIcon(QIcon('lautaro.jpeg'))
    frequency_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    duty_cycle_btn = QPushButton('Duty Cycle', self)
    duty_cycle_btn.setIcon(QIcon('lautaro.jpeg'))
    duty_cycle_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    grid.addWidget(connect_btn, 1, 0, 2, 1)
    grid.addWidget(disconnect_btn, 3, 0, 2 ,1)
    grid.addWidget(exit_btn, 5, 0, 2, 1)
    grid.addWidget(m, 1, 1, 6, 1)
    grid.addWidget(frequency_btn, 1, 2, 3, 1)
    grid.addWidget(duty_cycle_btn, 4, 2, 3, 1)
    grid.addWidget(title, 0, 1)
    grid.addWidget(pic, 0, 2)
    wid.setLayout(grid)

So, the question: how can I set the widget size? Clearly in this case the widgets related to title and the label are way larger than the buttons and the graph.

Upvotes: 0

Views: 12582

Answers (1)

Isma
Isma

Reputation: 15180

If you want the image to always have the same size you can set a fixed width and height as follows:

pic = QLabel(self)
pic.setPixmap(QtGui.QPixmap('escudo.gif'))
pic.setFixedWidth(250)
pic.setFixedHeight(250)

If you want the image to have a maximum size but it should resize and get smaller if the window gets smaller you can use the following:

pic.setMaximumWidth(250)
pic.setMaximumHeight(250)

Upvotes: 2

Related Questions