【问题标题】:How to place new window on existing main window location in PyQt5 (not center of screen)?如何在 PyQt5 中的现有主窗口位置(不是屏幕中心)放置新窗口?
【发布时间】:2021-05-10 09:44:21
【问题描述】:

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

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()

【问题讨论】:

    标签: python user-interface pyqt5 location window


    【解决方案1】:

    您可以在show()之后立即move()新窗口。

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

    【讨论】:

    • 答案很棒。但请提供解释。您只发布代码会使 OP 和未来的 commers 复制并粘贴您的答案,而不了解答案背后的逻辑。请提供一些解释的答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多