import PySide2.QtCore
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QTableWidgetItem
class MainWindow(PySide2.QtCore.QObject):
def __init__(self):
super().__init__()
# 加载ui
self.ui = QUiLoader().load(r'./ui/calc.ui')
self.ui.cb_switch.setChecked(False)
self.ui.table.horizontalHeader().setSectionResizeMode(PySide2.QtWidgets.QHeaderView.Stretch)
self.switch()
# 定义三个变量,num是计算结果,method是运算符,if_method判断是否是计算结果,如果是,则点数字时重新输入,而不是在原有基础上向后加
self.num = 0
self.method = ''
self.if_result = False
# 操作
self.ui.b_1.clicked.connect(self.set_num)
self.ui.b_1.clicked.connect(self.test)
self.ui.b_2.clicked.connect(self.set_num)
self.ui.b_3.clicked.connect(self.set_num)
self.ui.b_4.clicked.connect(self.set_num)
self.ui.b_5.clicked.connect(self.set_num)
self.ui.b_6.clicked.connect(self.set_num)
self.ui.b_7.clicked.connect(self.set_num)
self.ui.b_8.clicked.connect(self.set_num)
self.ui.b_9.clicked.connect(self.set_num)
self.ui.b_0.clicked.connect(self.set_num)
self.ui.b_00.clicked.connect(self.set_num)
self.ui.b_decimal.clicked.connect(self.set_num)
self.ui.b_multiply.clicked.connect(self.set_method)
self.ui.b_devide.clicked.connect(self.set_method)
self.ui.b_add.clicked.connect(self.set_method)
self.ui.b_subtract.clicked.connect(self.set_method)
self.ui.b_calculate.clicked.connect(self.set_method)
self.ui.b_ac.clicked.connect(self.my_init)
self.ui.rb_black.clicked.connect(self.change_color)
self.ui.rb_white.clicked.connect(self.change_color)
self.ui.cb_switch.stateChanged.connect(self.switch)
def test(self):
print('test')
def switch(self):
if self.ui.cb_switch.isChecked():
self.ui.tab.setEnabled(True)
self.ui.rb_black.setEnabled(True)
self.ui.rb_white.setEnabled(True)
else:
self.ui.tab.setEnabled(False)
self.ui.rb_black.setEnabled(False)
self.ui.rb_white.setEnabled(False)
def set_num(self):
if self.if_result:
self.ui.lcd.setText('')
self.if_result = False
source = self.sender().text()
num = self.ui.lcd.text() + source
self.ui.lcd.setText(num)
def set_method(self):
# 运算,并保存当前点按的运算符
this_num = float(self.ui.lcd.text()) # 读取当前屏幕值
num1_item = QTableWidgetItem(str(self.num)) # 这三个item是table的单元,用于向table填充内容
method_item = QTableWidgetItem(self.method)
num2_item = QTableWidgetItem(str(this_num))
if self.method == '+': # 首先,根据上次点按运算符计算结果
self.num += this_num
elif self.method == '-':
self.num -= this_num
elif self.method == '*':
self.num *= this_num
elif self.method == '/':
self.num /= this_num
elif self.method == '': # 如果是第一次运算,则把当前输入数字当成上次结果,并保存新按的运算符
self.num = this_num
self.if_result = True # 表示当前显示为运算结果,点击数字时重新输入
self.method = self.sender().text() # 保存当前点按运算符
return
result_item = QTableWidgetItem(str(self.num))
self.ui.table.setRowCount(self.ui.table.rowCount()+1)
self.ui.table.setItem(self.ui.table.rowCount() - 1, 0, num1_item)
self.ui.table.setItem(self.ui.table.rowCount() - 1, 1, method_item)
self.ui.table.setItem(self.ui.table.rowCount() - 1, 2, num2_item)
self.ui.table.setItem(self.ui.table.rowCount() - 1, 3, result_item)
self.ui.lcd.setText(str(self.num)) # 显示运算结果
self.if_result = True # 表示当前显示为运算结果,点击数字时重新输入
self.method = self.sender().text() # 保存当前点按运算符
if self.method == '=': # 如果点的是等于号,则重置计算器,并在LCD显示当前运行结果。
this_num = self.num
self.my_init()
self.ui.lcd.setText(str(this_num))
def my_init(self):
self.num = 0
self.method = '+'
self.ui.lcd.setText('')
def change_color(self):
source = self.sender().objectName()
if source == 'rb_black':
self.ui.rb_black.setChecked(True)
self.ui.rb_white.setChecked(False)
self.ui.lcd.setStyleSheet(('font: 75 36pt "Times New Roman";color: rgb(255,255,255);background-color: rgb(0, 0, 0);'))
else:
self.ui.rb_white.setChecked(True)
self.ui.rb_black.setChecked(False)
self.ui.lcd.setStyleSheet(('font: 75 36pt "Times New Roman";color: rgb(0,0,0);background-color: rgb(255,255,255);'))
if __name__ == '__main__':
app = QApplication([])
main_ui = MainWindow()
main_ui.ui.show()
app.exec_()