gpt4 book ai didi

python - PyQt5按钮点击不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 14:02:57 27 4
gpt4 key购买 nike

我在 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"))

最佳答案

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)

关于python - PyQt5按钮点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47132376/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com