我制作了一个不使用布局的用户界面,但是当我制作全屏按钮时,按钮只是停留在左上角,并且看起来很小。
像素化对我来说并不重要,我只是想让事情变得更大。 (当分辨率为 1920x1080 时,这是正常的,但是当我们将其设为 4k 时,事情会变大 4 倍)
使用pyqt5可以拉伸所有窗口内容吗?
就像我在 mspaint 中制作的这个例子:
主要代码⬇️
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys
from converted_ui import Ui_MainWindow
class MainUI(QMainWindow):
def __init__(self):
super(MainUI, self).__init__()
# Initialize the user interface
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MainUI()
ui.show()
sys.exit(app.exec_())
converted_ui ⬇️
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(70, 80, 250, 250))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(465, 170, 106, 46))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.label.setText(_translate("MainWindow", "Lorem Ipsum"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
是的,您可以使用 resizeEvent() 方法 您可以更改小部件的大小和位置。
但这并不实用,最好使用布局管理器。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
#from converted_ui import Ui_MainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
# ---------------------------------------------> vvvvvvvvvvvvvvvv <----
# self.pushButton.setGeometry(QtCore.QRect(70, 80, 250, 250))
self.pushButton.setGeometry(QtCore.QRect(80, 60, 200, 200))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
# ----------------------------------------> vvvvvvvvvvvvvvvvv <----
# self.label.setGeometry(QtCore.QRect(465, 170, 106, 46))
self.label.setGeometry(QtCore.QRect(460, 60, 200, 200))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.label.setText(_translate("MainWindow", "Lorem Ipsum"))
class MainUI(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainUI, self).__init__()
self.setupUi(self)
self.label.setStyleSheet("background-color: #1E5F74; color: #47F5E1;")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.w = 800
self.h = 600
self.x = 80
self.y = 60
self.setMinimumSize(self.w/3, self.h/3)
self.widthFactor = 1
self.heightFactor = 1
def resizeEvent(self, event):
self.widthFactor = self.rect().width() / 800
self.heightFactor = self.rect().height()/ 600
w = self.w * 0.25 * self.widthFactor
self.pushButton.resize(w, w)
self.pushButton.move(self.x * self.widthFactor , self.y * self.heightFactor)
self.label.resize(w, w)
self.label.move((self.x * self.widthFactor + w * 2), self.y * self.heightFactor)
super(MainUI, self).resizeEvent(event)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MainUI()
ui.show()
sys.exit(app.exec_())