Basic setup
In [ ]:
import sys
from PyQt5.QtWidgets import *
Documentation¶
Procedural approach¶
In [ ]:
# Create app
app = QApplication(sys.argv)
# Create main window
dlgMain = QWidget() # dlgMain = QMainWindow()
# configure main window
dlgMain.setWindowTitle('My App')
# ...
# show main window
dlgMain.show()
# execute app
app.exec_()
Alternatively¶
In [ ]:
# Create app
app = QApplication(sys.argv)
# Create main window
dlgMain = QMainWindow() # or dlgMain = QWidget()
# configure main window
dlgMain.setWindowTitle('My App')
# ...
# show main window
dlgMain.show()
# execute app
sys.exit(app.exec_())
Object oriented method¶
In [1]:
import sys
from PyQt5.QtWidgets import *
In [ ]:
# Create class
class DlgMain(QDialog):
# __init__ function
def __init__(self):
super().__init__()
# configure main window
self.setWindowTitle("My App")
if __name__ == "__main__":
app = QApplication(sys.argv)
dlgMain = DlgMain()
# show main window
dlgMain.show()
# execute app
sys.exit(app.exec_())
Adding widget¶
In [2]:
# Create class
class DlgMain(QDialog):
# __init__ function
def __init__(self):
super().__init__()
# configure main window
## windows size
self.resize(200,200)
self.setWindowTitle("My App")
# adding text entry
self.leadtext = QLineEdit('My Text', self)
self.leadtext.move(45,50)
# Button
self.btnupdate = QPushButton('Update window title', self)
self.btnupdate.move(45,80)
self.btnupdate.clicked.connect(self.evt_btnupdate_clicked)
def evt_btnupdate_clicked(self):
self.setWindowTitle(self.leadtext.text())
if __name__ == "__main__":
app = QApplication(sys.argv)
dlgMain = DlgMain()
# show main window
dlgMain.show()
# execute app
sys.exit(app.exec_())
An exception has occurred, use %tb to see the full traceback. SystemExit: 0
c:\Users\ferra\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3452: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)