Python Forum
Place QT Window in the middle
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Place QT Window in the middle
#1
Hello,

I'm working on a Python Tool with Qt6 (I have created the UI with QT Designer) but I'm running into a few problems...
This is my code so far:

#!/usr/bin/env Python3

# Imports
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.uic import loadUi
from PyQt6.QtCore import Qt
import ctypes
import json
import sys

# Main Function
def main()
    window()

# Window Function
def window()
    # Load Config Information from JSON
    with open ('config/config.json', 'r') as fp
        data = json.load(fp)

    # Create Variables from JSON
    for i in data
        for j in data[i]
            for k, l in zip(j.keys(), j.values())
                globals()[k] = l

    # Create Application and Window
    app = QApplication(sys.argv)
    window = QMainWindow()
    loadUi(config/MainWindow.ui, window)

    # Get DPI Scale
    user32 = ctypes.windll.user32
    user32.SetProcessDPIAware()
    dpi = user32.GetDpiForSystem()
    scale_factor = dpi  96

    # Get Screen Size
    screen = app.primaryScreen()
    size = screen.size()
    screenWidth = round(size.width() * scale_factor)
    screenHeight = round(size.height() * scale_factor)

    # Set Static Size and Disbale Menu Bar
    window.setGeometry((screenWidth - window.width() // 2, (screenHeight - window.height() // 2, int(screenWidth // float(Size)), int(screenHeight // float(Size)))
    window.setWindowFlags(Qt.WindowType.FramelessWindowHint)
    window.show()

    # Start Close Button
    window.closeButton.clicked.connect(app.quit)
    window.resizeButton.clicked.connect(lambda window.resize(750, 100))

    # Start the Application Loop
    sys.exit(app.exec())

# Start Main Function
main()

#End
sys.exit()
I'm trying to place the Window in the middle of the screen I'm starting it (not on my primary monitor). For explanation, I used the scale_factor because the window.height and width does not give the right screen size (2560 instead of 3840)... Every time I start the tool the window is not in the middle of the monitor.
I tried to this in Lazarus once and it worked really fast but here I'm having problems... Can someone help me?
Reply


Messages In This Thread
Place QT Window in the middle - by AlphaInc - Jul-16-2023, 11:54 AM
RE: Place QT Window in the middle - by Axel_Erfurt - Jul-16-2023, 12:49 PM
RE: Place QT Window in the middle - by AlphaInc - Jul-16-2023, 01:08 PM
RE: Place QT Window in the middle - by Axel_Erfurt - Jul-16-2023, 01:13 PM
RE: Place QT Window in the middle - by AlphaInc - Jul-16-2023, 01:24 PM
RE: Place QT Window in the middle - by Axel_Erfurt - Jul-16-2023, 03:21 PM
RE: Place QT Window in the middle - by Axel_Erfurt - Jul-16-2023, 05:00 PM
RE: Place QT Window in the middle - by AlphaInc - Jul-19-2023, 09:08 AM
RE: Place QT Window in the middle - by Axel_Erfurt - Jul-19-2023, 06:04 PM
RE: Place QT Window in the middle - by Gaurav_Kumar - Aug-03-2023, 12:15 PM
RE: Place QT Window in the middle - by Axel_Erfurt - Aug-03-2023, 05:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 743 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Label.Place did not work? ATARI_LIVE 15 5,501 Sep-18-2020, 04:22 PM
Last Post: ATARI_LIVE
  [Tkinter] How to place scroll bar correctly scratchmyhead 1 4,022 May-18-2020, 04:17 PM
Last Post: scratchmyhead
  How to use place holders in tkinter sqlite scratchmyhead 1 1,880 May-12-2020, 06:13 PM
Last Post: Larz60+
  tkinter window and turtle window error 1885 3 6,848 Nov-02-2019, 12:18 PM
Last Post: 1885
  thinker button gui place next to each other jacklee26 9 4,890 Jul-04-2019, 07:48 AM
Last Post: wuf
  update a variable in parent window after closing its toplevel window gray 5 9,247 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020