[英]Gui for a simple chat in PyQt5 using QGridLayout
I have been studying PyQt5 and recenlty decided to make a gui for a simple chat client. 我一直在研究PyQt5,因此悔恨地决定为一个简单的聊天客户端制作GUI。 This is my mockup: 这是我的模型:
I used QGridLayout, and this is what I got: 我使用了QGridLayout,这就是我得到的:
How do I decrease the size of the bottom QTextEdit, so it has 2-3 available lines, and make QPushButton larger? 如何减小底部QTextEdit的大小,使其具有2-3条可用行,并使QPushButton变大?
My program: 我的程序:
import sys
from PyQt5.QtWidgets import (QMainWindow, QAction, QApplication, QDesktopWidget,
QDialog, QTextEdit, QGridLayout, QPushButton, QWidget)
from PyQt5.QtGui import QIcon
class Chat(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.populateUI()
self.resize(400, 400)
self.center()
self.setWindowTitle('Simple Chat')
self.show()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def populateUI(self):
self.createMenu()
self.statusBar()
centralWidget = CentralWidget()
self.setCentralWidget(centralWidget)
def createMenu(self):
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
fileMenu.addAction(self.createExitAction())
helpMenu = menuBar.addMenu('&Help')
helpMenu.addAction(self.createAboutAction())
def createExitAction(self):
exitAction = QAction(QIcon('exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.close)
return exitAction
def createAboutAction(self):
aboutAction = QAction(QIcon('info.png'), '&About', self)
aboutAction.setShortcut('Ctrl+H')
aboutAction.setStatusTip('Information about the program')
aboutAction.triggered.connect(self.createAboutDialog)
return aboutAction
def createAboutDialog(self):
dialog = QDialog(self)
dialog.setWindowTitle('About')
dialog.setWindowIcon(QIcon('info.png'))
dialog.resize(200, 200)
dialog.exec_()
class CentralWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
ribbon = QTextEdit()
chat = QTextEdit()
sendBtn = QPushButton('Send')
grid = QGridLayout()
grid.setSpacing(3)
grid.addWidget(ribbon, 0, 0, 1, 3)
grid.addWidget(chat, 1, 0, 1, 1)
grid.addWidget(sendBtn, 1, 2)
self.setLayout(grid)
if __name__ == '__main__':
app = QApplication(sys.argv)
chat = Chat()
sys.exit(app.exec_())
我猜你正在照顾QWidget的 setFixedSize(QSize size) 。
You first need to set a fixed height for the bottom text-edit based on the number of lines you want shown. 首先,您需要根据要显示的行数为底部的文本编辑设置固定高度。 This also needs to take account of the frame and document margin: 这还需要考虑框架和文档边距:
def initUI(self):
...
chat.setFixedHeight(
(chat.fontMetrics().lineSpacing() * 3) +
(chat.document().documentMargin() * 2) +
(chat.frameWidth() * 2) - 1
)
You then need to change the size policy of the button so that it expands vertically: 然后,您需要更改按钮的大小策略,以使其垂直扩展:
policy = sendBtn.sizePolicy()
policy.setVerticalPolicy(QSizePolicy.MinimumExpanding)
sendBtn.setSizePolicy(policy)
Finally, you need to set the stretch factors on the first row and column so that the text-edits take up all the available space: 最后,您需要在第一行和第一列上设置拉伸因子,以便文本编辑占用所有可用空间:
grid.setRowStretch(0, 1)
grid.setColumnStretch(0, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.