Python PyQt-窗口的位置
我无法将窗口放在屏幕的右下角。frameGeometry()无法正常工作。请帮帮我,我能做什么?我假设您的“窗口”是Python PyQt-窗口的位置,python,python-3.x,user-interface,pyqt,pyqt5,Python,Python 3.x,User Interface,Pyqt,Pyqt5,我无法将窗口放在屏幕的右下角。frameGeometry()无法正常工作。请帮帮我,我能做什么?我假设您的“窗口”是QWidget的子类。如果是这样,以下内容应符合您的需要: def location_on_the_screen(self): fg = self.frameGeometry() sbrp = QDesktopWidget().availableGeometry().bottomRight() fg.moveBottomRight(sbrp) sel
QWidget
的子类。如果是这样,以下内容应符合您的需要:
def location_on_the_screen(self):
fg = self.frameGeometry()
sbrp = QDesktopWidget().availableGeometry().bottomRight()
fg.moveBottomRight(sbrp)
self.move(fg.topLeft())
以下是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):
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_()
谢谢您的回答,但此代码也无法按我的需要工作:(可以在macOS和Ubuntu上完美运行。我没有安装Windows操作系统,因此无法测试,但问题似乎是开始菜单栏的高度,
QDesktopWidget().screenGeometry()
没有考虑到这一点。)。
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_()
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_()