new_window_test.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import sys
  2. from PyQt5.Qt import *
  3. from PyQt5 import QtGui, QtWidgets
  4. class MyPopup(QWidget):
  5. def __init__(self):
  6. QWidget.__init__(self)
  7. lay = QtWidgets.QVBoxLayout()
  8. self.setLayout(lay)
  9. lay.setContentsMargins(0, 0, 0, 0)
  10. le = QtWidgets.QLineEdit()
  11. le.setText("Abracadabra")
  12. le.setReadOnly(True)
  13. # le.setStyleSheet("QLineEdit { qproperty-frame: false }")
  14. le.setFrame(False)
  15. le.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
  16. # lay.addStretch()
  17. but = QtWidgets.QPushButton("OK")
  18. hlay = QtWidgets.QHBoxLayout()
  19. hlay.setContentsMargins(0, 5, 5, 5)
  20. hlay.addStretch()
  21. hlay.addWidget(but)
  22. lay.addWidget(le)
  23. lay.addLayout(hlay)
  24. # def paintEvent(self, e):
  25. # dc = QtGui.QPainter(self)
  26. # dc.drawLine(0, 0, 100, 100)
  27. # dc.drawLine(100, 0, 0, 100)
  28. class MainWindow(QMainWindow):
  29. def __init__(self, *args):
  30. QtWidgets.QMainWindow.__init__(self, *args)
  31. self.cw = QtWidgets.QWidget(self)
  32. self.setCentralWidget(self.cw)
  33. self.btn1 = QtWidgets.QPushButton("Click me", self.cw)
  34. self.btn1.setGeometry(QRect(0, 0, 100, 30))
  35. self.btn1.clicked.connect(self.doit)
  36. self.w = None
  37. def doit(self):
  38. print("Opening a new popup window...")
  39. self.w = MyPopup()
  40. self.w.setGeometry(QRect(100, 100, 400, 200))
  41. self.w.show()
  42. class App(QApplication):
  43. def __init__(self, *args):
  44. QtWidgets.QApplication.__init__(self, *args)
  45. self.main = MainWindow()
  46. # self.lastWindowClosed.connect(self.byebye)
  47. self.main.show()
  48. def byebye(self):
  49. self.exit(0)
  50. def main(args):
  51. global app
  52. app = App(args)
  53. app.exec_()
  54. if __name__ == "__main__":
  55. main(sys.argv)