This is just some quick scrap notes on the basics of Python GUI building using PyQt.
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()
.
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
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()