안녕에 표시되지 않습니다, 나는 코드 아래 7
가 제대로 작동 PyQt5, 파이썬 3.4 및 Windows를 사용하여 간단한 GUI 응용 프로그램을 만들려고 노력하고 있습니다.
# coding: utf-8
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
class MainWnd(QMainWindow):
def __init__(self):
super().__init__()
self.popup_dlg = None
self.init_ui()
def init_ui(self):
self.setGeometry(100, 100, 300, 200)
self.show()
self.popup_dlg = ChildWnd()
class ChildWnd(QDialog):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.resize(200, 100)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWnd()
sys.exit(app.exec_())
두 개의 창이 생성됩니다. 하나는 기본 창이고 다른 하나는 하위 창 (팝업 창)입니다. 하지만 내가 원하는 것은 자식 윈도우의 기본 위치가 메인 윈도우의 중심에 있도록하는 것입니다.
그래서이 코드를 수정했습니다.
# coding: utf-8
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
class MainWnd(QMainWindow):
def __init__(self):
super().__init__()
self.popup_dlg = None
self.init_ui()
def init_ui(self):
self.setGeometry(100, 100, 300, 200)
self.show()
self.popup_dlg = ChildWnd(self) # make instance with parent window argument.
class ChildWnd(QDialog):
def __init__(self, parent_wnd):
super().__init__()
self.setParent(parent_wnd) # set child window's parent
self.init_ui()
def init_ui(self):
self.resize(200, 100)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWnd()
sys.exit(app.exec_())
하지만이 코드는 문제가됩니다. 자식 창이 표시되지 않습니다. 기본 창 (= 상위 창) 만 표시됩니다. Qt의 QDialog 's 매뉴얼에서 이것을 발견했습니다.
하지만 부모가있는 경우 (이 최상위 자체가 아닌 경우), 기본 위치는 부모의 최상위 위젯의 상단을 중심으로.
이 이유 때문에 setParent()을 추가했습니다.
어떻게해야합니까?
도와주세요!
당신의 슈퍼 생성자에 매개 변수로 부모 개체를 전달하려고 했 당신의 QDialog? -> super() .__ init __ (parent_wnd) –
@Daniele Pantaleone, 자식 개체의 super() .__ init __()에 부모 개체를 추가하고 self.setParent()를 제거했습니다. 그것은 작동합니다! 정확히 내가 원하는 것을 작동 !!! Daniele Pantaleone에게 감사드립니다. 당신은 내 하루 종일을 구했습니다. XD – passion053
@Daniele Pantaleone, 오 그런데, 나는 당신의 대답을 원합니다. 그러나 나는이 논평을 채택 할 수 없다. 답으로 다시 쓸 수 있니? – passion053