我目前正在使用Qt5在Python中开发Web浏览器。只是想知道:如何使窗口缩放到屏幕尺寸?例如,Chromium和Firefox在最大化时会适应它们所在的屏幕大小。
以下是Chromium在最大化时显示Google的方式:
编辑:由于有人要求我添加一些示例代码,因此代码如下:
from PyQt5.QtWidgets import QApplication, QWidget
from ui_output import Ui_Form
app = QApplication(sys.argv)
class MainWindow(QWidget, Ui_Form):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.go_button.clicked.connect(self.pressed)
def pressed(self):
self.webView.setUrl(QUrl(self.lineEdit.displayText()))
view = MainWindow()
view.show()
view.showMaximized()
app.exec_()
答案 0 :(得分:1)
您必须学习使用布局,这些元素用于管理另一个窗口小部件中的窗口大小。
在Qt Designer中,任务非常简单,在您的情况下,选择顶部的4个元素,然后按按钮:,然后在主窗口内的空白处按一下,然后按按钮:{{ 3}}。最后,您将获得以下结构:
生成以下.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string><---</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>---></string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QPushButton" name="go_button">
<property name="text">
<string>GO</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QWebView" name="webView">
<property name="url">
<url>
<string>about:blank</string>
</url>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QWebView</class>
<extends>QWidget</extends>
<header>QtWebKitWidgets/QWebView</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
ui_output.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui_output.ui'
#
# Created by: PyQt5 UI code generator 5.11.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.go_button = QtWidgets.QPushButton(Form)
self.go_button.setObjectName("go_button")
self.horizontalLayout.addWidget(self.go_button)
self.verticalLayout.addLayout(self.horizontalLayout)
self.webView = QtWebKitWidgets.QWebView(Form)
self.webView.setUrl(QtCore.QUrl("about:blank"))
self.webView.setObjectName("webView")
self.verticalLayout.addWidget(self.webView)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "<---"))
self.pushButton_2.setText(_translate("Form", "--->"))
self.go_button.setText(_translate("Form", "GO"))
from PyQt5 import QtWebKitWidgets
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
main.py
import sys
from PyQt5 import QtCore ,QtWidgets
from ui_output import Ui_Form
class MainWindow(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.go_button.clicked.connect(self.pressed)
def pressed(self):
self.webView.setUrl(QtCore.QUrl(self.lineEdit.displayText()))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
view = MainWindow()
view.showMaximized()
sys.exit(app.exec_())