Python PyQt Gui Basics Button Window PyQt4 Pt1
This is just some quick scrap notes on the basics of Python GUI building using PyQt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import sys from PyQt4 import QtGui, QtCore # to help add buttons #app = QtGui.QApplication(sys.argv) # #window = QtGui.QWidget() # ##set the posstion and size of the window #window.setGeometry(50, 50, 500, 500) # ##lets set the title #window.setWindowTitle('Date Incrementor') #window.show() class Window(QtGui.QMainWindow): def __init__(self): super(Window, self).__init__() self.setGeometry(50,50, 500, 500) self.setWindowTitle("Date incrementor") # self.setWindowIcon(QtGui.QIcon('logo.png')) #self.show() self.home() def home(self): #create and name a button btn = QtGui.QPushButton("Quit", self) #define what we will do when the button is clicked #btn.clicked.connect(QtCore.QCoreApplication.instance().quit) btn.clicked.connect(self.closeapplication) #lets resisze the button btn.resize(100,100) #lets position the button btn.move(100,100) #lets show the button self.show() #lets create custom methods def closeapplication(self): print('Application closed') sys.exit() |
1 |
. |
the ‘init‘ statements runs anytime we call call the Window class we have created and it runs all the code in the Window class
the _init__ method calls the super constructor and then sets the geometry and titles and finally shows the window
1 2 3 4 |
def run(): app = QtGui.QApplication(sys.argv) GUI = Window() sys.exit(app.exec_()) |
1 |
run() |