1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| from PyQt6.QtCore import Qt from PyQt6.QtWidgets import (QWidget, QPushButton, QLineEdit, QInputDialog, QApplication, QLabel, QDialog, QDialogButtonBox, QVBoxLayout) import sys import time
f_ret = [] def f(total, price, ix, args = []): p = price[ix - 1] if ix == len(price): if total % p == 0: f_ret.append(args + [total // p]) return for i in range(1, total // p): f(total - p * i, price, ix + 1, args + [i])
class AnsDialog(QDialog): def __init__(self, t, p): super().__init__() self.t = t self.p = p self.setWindowTitle("结果")
self.layout = QVBoxLayout() for i in range(len(f_ret)): ret = f_ret[i] s = "" for j in range(len(ret)): s = s + str(ret[j]) + " * " + str(self.p[j]) + " + " s = s[:-3] + ' = ' print(f"{s}{self.t}") message = QLabel(f"{s}{self.t}") self.layout.addWidget(message) self.setLayout(self.layout)
class Example(QWidget):
def __init__(self): super().__init__()
self.initUI()
def initUI(self): self.total_label = QLabel(self) self.total_label.setText("总价") self.total_label.move(20, 20) self.total_price = QLineEdit(self) self.total_price.move(50, 40)
self.unit_label = QLabel(self) self.unit_label.setText("单价") self.unit_label.move(20, 80) self.unit_price_list = [] unit_price = QLineEdit(self) unit_price.move(50, 110) self.unit_price_list.append(unit_price)
self.add_btn = QPushButton('+', self) self.add_btn.move(50, 80) self.add_btn.clicked.connect(self.add_unit_price)
self.sub_btn = QPushButton('-', self) self.sub_btn.move(120, 80) self.sub_btn.clicked.connect(self.sub_unit_price)
self.calc_btn = QPushButton('计算', self) self.calc_btn.move(200, 80) self.calc_btn.clicked.connect(self.calc_price)
self.setGeometry(300, 300, 450, 350) self.setWindowTitle('王工专用') self.show()
def add_unit_price(self): unit = self.unit_price_list[-1] x, y = unit.x(), unit.y()
unit = QLineEdit(self) unit.move(x, y + 25) unit.show() self.unit_price_list.append(unit)
def sub_unit_price(self): if len(self.unit_price_list) > 1: unit = self.unit_price_list.pop() unit.close() def calc_price(self): total = int(self.total_price.text()) price = [i.text() for i in self.unit_price_list] price = list(set(int(i) for i in price)) f_ret.clear() f(total, price, 1) dlg = AnsDialog(total, price) dlg.exec()
def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec())
if __name__ == '__main__': main()
|