sum_all 函数在 sum_all_invoice 函数内部不可用

问题描述 投票:0回答:1
def sum_all():
    total = 0

    # Iterating through the labels of each meal using a loop
    for i in range(1, 7):
        label_name = f"meal_{i}_line"

        # Accessing the values directly by reaching the labels
        label = getattr(self.ui, label_name, None)

        label_text = label.text()

        try:
            # Adding the number inside the label to the total variable
            total += int(label_text)
        except ValueError:
            # If the value inside the label is not a numerical expression, an error is displayed.
            # You can handle the error situation here.
            print(f"Error: No numerical expression found inside the {label_name} label. Defaulting to 0.")
            total += 0

    self.ui.price_line.setText(str(total))  # print to the price

    # VAT calculation
    def vat(total):
        vat_value = total * 0.18
        return vat_value

    vat_value_to_write = vat(total)
    self.ui.vat_line.setText(str(vat_value_to_write))  # print to the VAT line

    # Service charge calculation
    def service(total):
        service_charge = total * 0.1
        return service_charge

    service_charge_to_write = service(total)
    self.ui.service_charge_line.setText(str(service_charge_to_write))  # print to the service charge line

    # Calculate all
    def sum_all_invoice():
        meal_value = sum_all()  # causes an error when added
        vat_value = vat(total)
        service_value = service(total)

        total1 = vat_value + service_value + meal_value
        return total1

    sausage = sum_all_invoice()
    self.ui.subtotal_line.setText(str(sausage))

# Call the sum_all function when the button is clicked
self.ui.total_button.clicked.connect(sum_all)

我可以打印price_line中的sum_all数据,但是, 我无法在

sum_all_invoice()
函数内部使用它。

sum_all_invoice()
函数正在收集所有函数以实现此目的:

total1 = vat_value + service_value + meal_value

但它不需要

meal_value

当我没有添加时,meal_value 为total1,如下所示:

total1 = vat_value + service_value
工作正常。

错误:

meal_value = sum_all()
RecursionError: maximum recursion depth exceeded while calling a Python object
python qt pyqt
1个回答
0
投票
def sum_all():
    total = 0

    # Iterating through the labels of each meal using a loop
    for i in range(1, 7):
        label_name = f"meal_{i}_line"

        # Accessing the values directly by reaching the labels
        label = getattr(self.ui, label_name, None)

        label_text = label.text()

        try:
            # Adding the number inside the label to the total variable
            total += int(label_text)
        except ValueError:
            # If the value inside the label is not a numerical expression, an error is displayed.
            # You can handle the error situation here.
            print(f"Error: No numerical expression found inside the {label_name} label. Defaulting to 0.")
            total += 0

    self.ui.price_line.setText(str(total))  # print to the price

    vat_value_to_write = vat(total)
    self.ui.vat_line.setText(str(vat_value_to_write))  # print to the VAT line

    service_charge_to_write = service(total)
    self.ui.service_charge_line.setText(str(service_charge_to_write))  # print to the service charge line

    sausage = sum_all_invoice(total)
    self.ui.subtotal_line.setText(str(sausage))


# VAT calculation
def vat(total):
    vat_value = total * 0.18
    return vat_value

# Service charge calculation
def service(total):
    service_charge = total * 0.1
    return service_charge

# Calculate all
def sum_all_invoice(total):
    vat_value = vat(total)
    service_value = service(total)

    total1 = vat_value + service_value + total
    return total1

# Call the sum_all function when the button is clicked
self.ui.total_button.clicked.connect(sum_all)

这只是对代码的稍微更好的安排。

各个功能现在是独立的,这意味着可以单独测试它们。

© www.soinside.com 2019 - 2024. All rights reserved.