''' Worksheet.py Solution tothe Example on the Worksheet''' from PyQt5 import QtWidgets, QtCore, QtGui from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout, QGridLayout, QLabel, QLineEdit, QComboBox from PyQt5.QtGui import QIcon from PyQt5.QtCore import pyqtSlot import sys,os ''' Main class that sets up the application. It inherits from Main Window ''' class Worksheet(QtWidgets.QMainWindow): def __init__(self): QtWidgets.QMainWindow.__init__(self) self.setup() def setup(self): self.setWindowTitle('QT Example') #Title Bar self.setGeometry(200,200,700,350) #Size of the Window self.setAutoFillBackground(True) #Set background to standard color self.ds = DoStuff(self) #Object of class that contains the widgets self.setCentralWidget(self.ds) #Set the central widget to the object self.show() #Show the window ''' class that contains all the widgets. Inhetirs from standard widget and is set to the central widget of the main window. ''' class DoStuff(QtWidgets.QWidget): def __init__(self,parent): QtWidgets.QWidget.__init__(self,parent) self.setup() ''' Function sets up all the individual elements in a grid layout ''' def setup(self): self.layout = QtWidgets.QGridLayout() #Set up a grid layout self.setLayout(self.layout) #assign the grid layout to the window ''' Add a label to the grid on position 0,0. Get the label to say "Chicken Attaaaaaaack" ''' self.layout.addWidget(QLabel('Chicken Attaaaaaaack'),0,0) ''' Set up a combo box. Add the 5 items to the combo box Then add the combo box to the grid, in position 0,1 (first row, second column)''' self.combo = QComboBox(self) self.combo.addItem('Original Sandwich') self.combo.addItem('Spicy Chicken Sandwich') self.combo.addItem('8 count Nuggets') self.combo.addItem('Waffle Fries') self.combo.addItem('Milkshake') self.layout.addWidget(self.combo,0,1) ''' Set up a text box to hold the number of items ordered. Set the width to about half the window and the initial value to be 0. Add it to the gris at position 1,0 ''' self.text1 = QLineEdit(self) self.text1.setFixedWidth(350) self.text1.setText('0') self.layout.addWidget(self.text1,1,0) ''' Create a Button. Get it to say "calculate". On click, set it up to call the function "calculate". Add it to the grid at position 1,1''' self.calc = QPushButton('Calculate') self.calc.clicked.connect(self.calculate) self.layout.addWidget(self.calc,1,1) ''' Set up another text box to hold the total price. Leave it empty for now Add it to the grid at position 2,0. Just to make sure position 2,1 is empty, assign an empty label to that spot''' self.text2 = QLineEdit(self) self.text2.setFixedWidth(350) self.layout.addWidget(self.text2,2,0) self.layout.addWidget(QLabel(""),2,1) ''' Create an empty label. Set it up to be a Pixmap that contains an image. The image is in the same directory as this program. Then add the label to the grid on row 3 and make sure it spans both columns ''' self.pic = QLabel() self.pic.setPixmap(QtGui.QPixmap(os.getcwd() + "/chicken.png")) self.layout.addWidget(self.pic,3,0,1,2) ''' This function is automatically called when the button is clicked.''' def calculate(self): self.num = 0 self.price = 0.0 self.val = self.text1.text() #Get the value from the text box if self.val != '': self.num = int(self.val) #Convert to integer self.it = str(self.combo.currentText()) # Get value from combo box # Assign price based on item if self.it == 'Original Sandwich': self.price = 3.5 elif self.it == 'Spicy Chicken Sandwich': self.price = 3.85 elif self.it == '8 count Nuggets': self.price = 2.79 elif self.it == 'Waffle Fries': self.price = 1.99 elif self.it == 'Milkshake': self.price = 3.69 self.price = self.price * self.num #Total price #Set the message on the second text box self.text2.setText("You purchased " + str(self.num) + " " + self.it + \ ". Your total is $ " + str(self.price)) #Open the application if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) main_window = Worksheet() app.exec_()