PyQt5按钮单击无法正常工作

时间:2017-11-06 08:12:21

标签: python pyqt5

我在Python GUI中编写了一个简单的代码来计算使用PyQt5点击按钮的税。它应该按下按钮。但是按钮点击事件无法正常工作。它显示以下错误。

self.calc_tax_button.clicked.connect(self.CalculateTax) AttributeError:'AppWindow'对象没有属性'calc_tax_button'

我在eclipse上使用python。

我的主要代码到现在为止。

**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_())

我的设计代码。

**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"))

2 个答案:

答案 0 :(得分:3)

pyuic生成的类旨在用作mixin,例如:

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_())

您尝试通过创建专用的ui对象来使用它,然后该对象包含所有ui元素。这也可以,但是您需要通过ui属性访问所有内容:

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)

答案 1 :(得分:2)

您正在创建一个对象self.ui,它是GUI的构造函数类。您必须通过该对象处理GUI及其中的所有小部件。

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_())