gpt4 book ai didi

python - 在 PyQt5 中使窗口缩放到屏幕大小

转载 作者:行者123 更新时间:2023-11-30 22:07:04 29 4
gpt4 key购买 nike

我目前正在使用 Qt5 用 Python 开发一个 Web 浏览器。只是想知道:如何使窗口缩放到屏幕尺寸?例如,Chromium 和 Firefox 在最大化时会适应屏幕的大小。

以下是 Chromium 在最大化时显示 Google 的方式:

enter image description here

编辑:由于我被要求添加一些示例代码,因此代码如下:

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_()

enter image description here

最佳答案

您必须学习使用布局,这些元素用于管理另一个窗口小部件的大小。

在 Qt Designer 中,任务非常简单,根据您的情况,选择顶部的 4 个元素,然后按按钮: enter image description here然后在主窗口内的空白处按下按钮:enter image description here 。最后你将得到以下结构:

enter image description here

enter image description here

生成以下.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>&lt;---</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>---&gt;</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_())

ma​​in.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_())

enter image description here

关于python - 在 PyQt5 中使窗口缩放到屏幕大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52530402/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com