Одним из возможных решений является установка sizeHint как фиксированного размера после установки макетов.
import sys
from PyQt4 import QtCore, QtGui
QSS = '''
QLabel#big{
font: bold 30pt Comic Sans MS
}
QLabel#small{
font: bold 15pt Comic Sans MS
}
'''
class Example1(QtGui.QWidget):
def __init__(self, parent=None):
super(Example1, self).__init__(parent)
self.initUI()
def initUI(self):
grid = QtGui.QGridLayout(self)
label_payment = QtGui.QLabel("Payment Details", objectName="big")
label_received = QtGui.QLabel("Received Amount", objectName="big")
label_amount = QtGui.QLabel("Amount: 192", objectName="small")
label_quantity = QtGui.QLabel("Quantity: 1", objectName="small")
cash_button = QtGui.QPushButton("Cash")
card_button = QtGui.QPushButton("Card")
wallet_button = QtGui.QPushButton("Wallet")
lcd = QtGui.QLCDNumber()
sp = lcd.sizePolicy()
sp.setHorizontalPolicy(QtGui.QSizePolicy.Fixed)
lcd.setSizePolicy(sp)
grid_buttons = QtGui.QGridLayout()
names = ['7', '8', '9',
'4', '5', '6',
'1', '2', '3',
'<--', '0', '.']
positions = [(i,j) for i in range(4) for j in range(3)]
for position, name in zip(positions, names):
button = QtGui.QPushButton(name)
grid_buttons.addWidget(button, *position)
grid.addWidget(label_payment, 0, 0, 2, 3)
grid.addWidget(label_amount, 0, 5)
grid.addWidget(label_quantity, 1, 5)
hbox = QtGui.QHBoxLayout(spacing=0)
hbox.setContentsMargins(0, 0, 0, 0)
hbox.addWidget(cash_button)
hbox.addWidget(card_button)
hbox.addWidget(wallet_button)
grid.addLayout(hbox, 3, 0, 1, 6)
vbox = QtGui.QVBoxLayout(spacing=0)
vbox.setContentsMargins(0, 0, 0, 0)
vbox.addWidget(label_received)
vbox.addWidget(lcd)
grid.addLayout(vbox, 4, 1)
grid.addLayout(grid_buttons, 4, 3, 4, 3)
self.setFixedSize(self.sizeHint())
def main():
app = QtGui.QApplication(sys.argv)
app.setStyleSheet(QSS)
ex = Example1()
ex.show()
ex.setGeometry(300,300,500,500)
sys.exit(app.exec_())
if __name__ == '__main__': main()