简体   繁体   中英

Make window scale to screen size in PyQt5

I'm currently developing a web browser in Python using Qt5. Just wondering: How can I make the window scale to the screen size? For example, Chromium and Firefox adapt to the size of the screen they're on when maximized.

Here's how Chromium displays Google when maximized:

在此处输入图片说明

EDIT: Since I was requested to add some example code, here is the code:

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

在此处输入图片说明

You have to learn to use the layouts, these elements are used to manage the sizes of the widgets within another.

In Qt Designer the task is very simple, in your case select the 4 elements at the top and press the button: 在此处输入图片说明 and then press in an empty space inside the main window and press the button: 在此处输入图片说明 . At the end you will get the following structure:

在此处输入图片说明

在此处输入图片说明

Generating the following .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_())

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM