首页 > 解决方案 > 如何在 PyQt5 中的现有主窗口位置(不是屏幕中心)放置新窗口?

问题描述

我有一个项目,用户按下一个打开一个新窗口的按钮。如何将我的新窗口设置为始终在主窗口位置顶部打开?(当用户移动窗口时不要在屏幕中心打开它,安装在主窗口位置的顶部)

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class MainWindow(QtWidgets.QMainWindow):

def __init__(self):
    super(MainWindow, self).__init__()
    self.mainUI()

def mainUI(self):
    self.setWindowTitle('Estetica Arch Bt.')
    self.setGeometry(0,0,400,400)

    self.button = QPushButton('Open',self)
    self.button.clicked.connect(self.on_click_button)

    self._main = QtWidgets.QWidget()
    self.setCentralWidget(self._main)
    layout = QtWidgets.QGridLayout(self._main)
    layout.addWidget(self.button)

    self.show()

@pyqtSlot()
def on_click_button(self):
    self.pdw = ProjektDetailsWindow()
    self.pdw.show()


class ProjektDetailsWindow(QWidget):

def __init__(self):
    super().__init__()
    self.projektdetails()
    

def projektdetails(self):
    self.setFixedSize(200, 200)
    self.show()


def main():
    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

标签: pythonuser-interfacepyqt5locationwindow

解决方案


你可以move()在你想要的地方新建一个窗口show()

@pyqtSlot()
def on_click_button(self):
    self.pdw = ProjektDetailsWindow()
    self.pdw.show()
    self.pdw.move(self.pos())

推荐阅读