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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QGridLayout from PyQt5.QtCore import Qt
class CustomLineEdit(QLineEdit): def keyPressEvent(self, event): if event.key() in [Qt.Key_Plus, Qt.Key_Minus]: self.parent().keyPressEvent(event) else: super().keyPressEvent(event)
class ExpectedValueCalculator(QWidget): def __init__(self): super().__init__() self.initUI()
def initUI(self): self.value_entries = [] self.setGeometry(300, 300, 260, 220) self.setWindowTitle('数学期望计算器')
layout = QVBoxLayout()
cost_layout = QHBoxLayout() cost_layout.addStretch() cost_layout.addWidget(QLabel('成本:')) self.cost_entry = CustomLineEdit(self) cost_layout.addWidget(self.cost_entry) cost_layout.addStretch() layout.addLayout(cost_layout)
self.grid_layout = QGridLayout() self.grid_layout.addWidget(QLabel('几率%'), 0, 0) self.grid_layout.addWidget(QLabel('产出价值'), 0, 1) layout.addLayout(self.grid_layout)
btn_layout = QHBoxLayout() btn_layout.addStretch() clear_btn = QPushButton('清除', self) clear_btn.clicked.connect(self.clear_entries) btn_layout.addWidget(clear_btn) add_btn = QPushButton('+', self) add_btn.clicked.connect(self.add_value_entry) btn_layout.addWidget(add_btn) remove_btn = QPushButton('-', self) remove_btn.clicked.connect(self.remove_value_entry) btn_layout.addWidget(remove_btn) btn_layout.addStretch() layout.addLayout(btn_layout)
calc_layout = QHBoxLayout() calc_layout.addStretch() calc_btn = QPushButton('计算', self) calc_btn.clicked.connect(self.calculate_expected_value) calc_layout.addWidget(calc_btn) calc_layout.addStretch() layout.addLayout(calc_layout)
self.result_label = QLabel('数学期望: 0') layout.addWidget(self.result_label)
self.setLayout(layout) self.add_value_entry() self.add_value_entry() self.add_value_entry()
def keyPressEvent(self, event): if event.key() == Qt.Key_Plus: self.add_value_entry() elif event.key() == Qt.Key_Minus: self.remove_value_entry() elif event.key() == Qt.Key_Return: self.calculate_expected_value() elif event.key() == Qt.Key_Escape: self.clear_entries()
def add_value_entry(self): probability_entry = CustomLineEdit(self) value_entry = CustomLineEdit(self) row = len(self.value_entries) + 2 self.grid_layout.addWidget(probability_entry, row, 0) self.grid_layout.addWidget(value_entry, row, 1) self.value_entries.append((probability_entry, value_entry))
def remove_value_entry(self): if len(self.value_entries) > 1: probability_entry, value_entry = self.value_entries.pop() probability_entry.deleteLater() value_entry.deleteLater()
def clear_entries(self): self.cost_entry.clear() for probability_entry, value_entry in self.value_entries: probability_entry.clear() value_entry.clear() self.result_label.setText('数学期望: 0')
def calculate_expected_value(self): try: cost = float(self.cost_entry.text()) total_value = sum(float(entry[1].text()) * float(entry[0].text()) / 100 for entry in self.value_entries) expected_value = total_value - cost percentage_value = (expected_value / cost) * 100 if cost != 0 else 0 self.result_label.setText(f'数学期望: {expected_value:.2f} ({percentage_value:.2f}%)') except ValueError as e: self.result_label.setText(f'错误: {e}')
def main(): app = QApplication(sys.argv) ex = ExpectedValueCalculator() ex.show() sys.exit(app.exec_())
if __name__ == '__main__': main()
|