Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在PyQt5中按屏幕大小进行窗口缩放_Python_Python 3.x_Pyqt_Pyqt5 - Fatal编程技术网

Python 在PyQt5中按屏幕大小进行窗口缩放

Python 在PyQt5中按屏幕大小进行窗口缩放,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,我目前正在使用Qt5用Python开发一个web浏览器。只是想知道:我如何使窗口缩放到屏幕大小?例如,Chromium和Firefox在最大化时会根据屏幕大小进行调整 以下是Chromium在最大化时显示Google的方式: 编辑:因为我被要求添加一些示例代码,下面是代码: from PyQt5.QtWidgets import QApplication, QWidget from ui_output import Ui_Form app = QApplication(sys.argv)

我目前正在使用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_()

您必须学会使用布局,这些元素用于管理其他窗口小部件的大小

在Qt Designer中,任务非常简单,在您的示例中,选择顶部的4个元素并按下按钮:然后在主窗口内的空白处按下并按下按钮:。最后,您将获得以下结构:

正在生成以下.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>

执行:
your_window.showMaximized()
@eyllanesc谢谢,它现在运行的最大化了,但是我如何将WebView缩放到我的窗口的大小呢?WebView是什么意思,您指的是QWebEngineView或QWebView,您可以提供一个更好地理解您的方法,因为您的解释不理解它,您还可以显示问题的图像。我添加了一些代码。
# -*- 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_())
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_())