Reputation: 418
I wrote a simple code in Python GUI to calculate the tax on button click using PyQt5. It should work on button click. But button click event is not working properly. It is showing the following error.
self.calc_tax_button.clicked.connect(self.CalculateTax) AttributeError: 'AppWindow' object has no attribute 'calc_tax_button'
I am using python on eclipse.
My main code till now.
**gu1.py**
import sys
# from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QMainWindow, QWidget, QPushButton
from PyQt5.QtCore import pyqtSlot
from design import Ui_MainWindow
class AppWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
self.calc_tax_button.clicked.connect(self.CalculateTax)
def CalculateTax(self):
price = int(self.price_box.toPlainText())
tax = (self.tax_rate.value())
total_price = price + ((tax / 100) * price)
total_price_string = "The total price with tax is: " + str(total_price)
self.results_window.setText(total_price_string)
app = QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())
My design code.
**design.py**
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'design.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.price_box = QtWidgets.QTextEdit(self.centralwidget)
self.price_box.setGeometry(QtCore.QRect(240, 100, 104, 71))
self.price_box.setObjectName("price_box")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(140, 130, 51, 21))
font = QtGui.QFont()
font.setFamily("Georgia")
font.setPointSize(14)
self.label.setFont(font)
self.label.setObjectName("label")
self.tax_rate = QtWidgets.QSpinBox(self.centralwidget)
self.tax_rate.setGeometry(QtCore.QRect(240, 220, 101, 31))
self.tax_rate.setProperty("value", 0)
self.tax_rate.setObjectName("tax_rate")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(130, 230, 71, 21))
font = QtGui.QFont()
font.setFamily("Georgia")
font.setPointSize(12)
font.setBold(False)
font.setWeight(50)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.calc_tax_button = QtWidgets.QPushButton(self.centralwidget)
self.calc_tax_button.setGeometry(QtCore.QRect(240, 310, 101, 31))
self.calc_tax_button.setObjectName("calc_tax_button")
self.results_window = QtWidgets.QTextEdit(self.centralwidget)
self.results_window.setGeometry(QtCore.QRect(240, 390, 104, 71))
self.results_window.setObjectName("results_window")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(160, 20, 301, 41))
font = QtGui.QFont()
font.setFamily("Georgia")
font.setPointSize(20)
font.setBold(True)
font.setItalic(True)
font.setUnderline(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Price"))
self.label_2.setText(_translate("MainWindow", "Tax Rate"))
self.calc_tax_button.setText(_translate("MainWindow", "Calculate Tax"))
self.label_3.setText(_translate("MainWindow", "Sales Tax Calculator"))
Upvotes: 4
Views: 7365
Reputation: 69012
The classes generated by pyuic are intended to be used as a mixin, e.g:
import sys
# from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QMainWindow, QWidget, QPushButton
from PyQt5.QtCore import pyqtSlot
from design import Ui_MainWindow
class AppWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.show()
self.calc_tax_button.clicked.connect(self.CalculateTax)
@pyqtSlot()
def CalculateTax(self):
price = int(self.price_box.toPlainText())
tax = (self.tax_rate.value())
total_price = price + ((tax / 100) * price)
total_price_string = "The total price with tax is: " + str(total_price)
self.results_window.setText(total_price_string)
app = QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())
You tried to use it by creating a dedicated ui
object which then holds all ui elements. That can also work, but then you need to access everything through the ui
attribute:
class AppWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
self.ui.calc_tax_button.clicked.connect(self.CalculateTax)
@pyqtSlot()
def CalculateTax(self):
price = int(self.ui.price_box.toPlainText())
tax = (self.ui.tax_rate.value())
total_price = price + ((tax / 100) * price)
total_price_string = "The total price with tax is: " + str(total_price)
self.ui.results_window.setText(total_price_string)
Upvotes: 4
Reputation: 2801
You are creating an object self.ui
which is the constructor class for your GUI. You will have to address your GUI and all widgets in it through that object.
import sys
# from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QMainWindow, QWidget, QPushButton
from PyQt5.QtCore import pyqtSlot
from design import Ui_MainWindow
class AppWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
@pyqtSlot()
def CalculateTax(self):
price = int(self.ui.price_box.toPlainText())
tax = (self.ui.tax_rate.value())
total_price = price + ((tax / 100) * price)
total_price_string = "The total price with tax is: " + str(total_price)
self.ui.results_window.setText(total_price_string)
app = QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())
Upvotes: 2