PyQt - 窗口的位置

5
def location_on_the_screen(self):
    fg = self.frameGeometry()
    sbrp = QDesktopWidget().availableGeometry().bottomRight()
    fg.moveBottomRight(sbrp)
    self.move(fg.topLeft())

我无法将窗口放在屏幕右下角。frameGeometry()的工作不像应该的那样。请帮助我,我该怎么做?

3个回答

12

这里是Windows的一个可能解决方案:

import sys

from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget


class MyWidget(QWidget):

    def __init__(self):
        super().__init__()
        self.setFixedSize(400, 300)

    def location_on_the_screen(self):
        ag = QDesktopWidget().availableGeometry()
        sg = QDesktopWidget().screenGeometry()

        widget = self.geometry()
        x = ag.width() - widget.width()
        y = 2 * ag.height() - sg.height() - widget.height()
        self.move(x, y)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MyWidget()
    widget.location_on_the_screen()
    widget.show()
    app.exec_()

2
from PyQt5.QtWidgets import QMainWindow, QLabel
from PyQt5.QtWidgets import QGridLayout, QWidget, QDesktopWidget
#------------------------------   
def center_window():
   qtRectangle = self.frameGeometry()
   centerPoint = QDesktopWidget().availableGeometry().center()
   qtRectangle.moveCenter(centerPoint)
   self.move(qtRectangle.topLeft())
   #---------------------------------
if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MyWidget()
    widget.window_center():
    widget.show()
    app.exec_()

0
我假设你的“窗口”是QWidget的子类。如果是这样,以下内容应该符合你的需求:
import sys

from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget


class MyWidget(QWidget):

    def __init__(self):
        super().__init__()
        self.setFixedSize(400, 300)

    def location_on_the_screen(self):    
        screen = QDesktopWidget().screenGeometry()
        widget = self.geometry()
        x = screen.width() - widget.width()
        y = screen.height() - widget.height()
        self.move(x, y)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MyWidget()
    widget.location_on_the_screen()
    widget.show()
    app.exec_()

谢谢您的回答,但是这段代码也不能按照我需要的方式工作。 :( http://i.imgur.com/YY0Y67D.png - Newbie
在 macOS 和 Ubuntu 上运行得非常完美。我没有安装 Windows 操作系统,因此无法进行测试,但似乎问题是开始菜单栏的高度未被 QDesktopWidget().screenGeometry() 考虑。 - Daniele Pantaleone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接