How can I resize the main window depending on screen resolution using PyQt

24,846

You can use

showFullScreen() or just showMaximized()

and you can get screen geometry with:

desktop() and screenGeometry()

Share:
24,846
Valla
Author by

Valla

Updated on November 26, 2020

Comments

  • Valla
    Valla over 3 years

    I have a main window with three frames.The top frame consists of the header and the bottom frame consists of the footer. I designed it using the PyQt4 designer.The window looks fine when I run it on my laptop with a screen resolution of 1920*1080. But when I check the same on other resolutions like 1600*900 the footer gets cut off. I wanted to know if there is a way to resize the window according to screen resolution in the runtime so that all the three frames are shown. I tried to check online if there are any solutions for this but could not find any. I tried using window.setGeometry and window.setFixedSize functions,but it did not work.

    The code for the window is:

    import sys
    import os
    import threading
    import smtplib
    
    from PyQt4 import QtCore, QtGui, uic
    import sched
    import time
    
    form_class = uic.loadUiType("FirstTest.ui")[0]                 # Load the UI
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        _fromUtf8 = lambda s: s
    
    class MyWindowClass(QtGui.QMainWindow, form_class):
        def __init__(self, parent=None):
            QtGui.QMainWindow.__init__(self, parent)        
            self.setupUi(self)        
    
    #has some code for the field values to be shown
    
    
    
    
    
    
    
    app = QtGui.QApplication(sys.argv)
    myWindow = MyWindowClass(None)
    #myWindow.setFixedSize(500,500)
    myWindow.showMaximized()
    palette = QtGui.QPalette()
    palette.setColor(QtGui.QPalette.Background,QtCore.Qt.white)
    myWindow.setPalette(palette)
    myWindow.show()
    app.exec_()
    
  • Valla
    Valla over 9 years
    When I use window.showFullScreen() it is takiing up the entire screen and the close button on the button also disappears and there is no way to close the window. Could you give me an example of how it can be used.
  • Jablonski
    Jablonski over 9 years
    @Valla I understood, see my edit about showMaximized, I think it is what are you looking for.
  • Valla
    Valla over 9 years
    Still does not work. I used QT4 designer to work on the panels.So should the widget and the frame width be resized through the designer itself?
  • Jablonski
    Jablonski over 9 years
    @Valla so do you use layouts? Layout can resize all elements accordingly to size of main window. It can't be done in designer because designer with with your current screen.
  • Valla
    Valla over 9 years
    I have edited my question and included the code that is there in my .py file. I did not use any layout in the designer while creating the form.
  • Jablonski
    Jablonski over 9 years
    @Valla so without layout all resizing has no any sence, so use layout, see tutorials in web. For example: qt-project.org/doc/qt-4.8/designer-layouts.html
  • Valla
    Valla over 9 years