[英]PyQt5: how to fix the width/height to specific column/row in QGridLayout?
對於這個問題,我指的例子calculator.py從http://zetcode.com/gui/pyqt5/layout/
在示例中,使用了QGridLayout
。 我想問問是否可以為某些特定的列/行定義寬度/高度?
請看圖片。 例如,我希望第二列的寬度為50px,第三行的高度為80px。 因此,無論窗口大小是多少,這些50px和80px始終按定義顯示。 窗口大小更改時,其余行/列可以自動縮放。
我已經搜索了,但是找不到答案。
感謝您的幫助和提示!
更新:為方便起見,我將代碼粘貼到此處(對原始版本進行了微小的更改)
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout, QPushButton, QApplication)
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
names = ['Cls', 'Bck', '', 'Close',
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+']
positions = [(i,j) for i in range(5) for j in range(4)]
for position, name in zip(positions, names):
if name == '':
continue
button = QPushButton(name)
grid.addWidget(button, *position)
self.move(300, 150)
self.setWindowTitle('Calculator')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
一種可能的解決方案是設置窗口小部件的固定尺寸,如下所示:
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
names = ['Cls', 'Bck', '', 'Close',
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+']
positions = [(i,j) for i in range(5) for j in range(4)]
for position, name in zip(positions, names):
if name == '':
continue
button = QPushButton(name)
row, column = position
if row == 2:
button.setFixedHeight(80)
if column == 1:
button.setFixedWidth(50)
grid.addWidget(button, *position)
self.move(300, 150)
self.setWindowTitle('Calculator')
self.show()
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.