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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
''' 版本说明: TODO 还有一堆BUG没搞定(多次按运算符按键,导致结果计算不对) TODO 等于号多次按下进行重复运算 TODO 改这些儿地方太费劲了,拉倒吧 TODO __init__ 初始化魔术方法 使用不够熟练 '''
import tkinter
class Calculation:
but_chang = 80 but_kuan = 60 win_chang = 400 win_kuan = 500 isys = False yslist = []
def __init__(self): self.run_logic() pass
def press_num(self,no): print(self.yslist) if self.isys == True: self.num.set(no) self.isys = False pass else: oldno = self.num.get() if oldno == '0': self.num.set(no) else: self.num.set(self.num.get() + no)
pass
def press_sym(self,ysflag): self.isys = True
self.yslist.append(self.num.get()) print(self.yslist) self.cal_res() if ysflag == '=': self.yslist.clear() elif ysflag == 'clear': self.yslist.clear() self.num.set('') else: self.yslist.append(ysflag)
pass
def cal_res(self): res = eval(''.join(self.yslist)) self.num.set(res) pass
def button_put(self,contain,com_func, x, y): btn = tkinter.Button(self.root, text=contain, command=lambda: com_func(contain)) btn.place(relx = (self.but_chang * x ) / self.win_chang, rely = (self.but_kuan * y) / self.win_chang, relwidth = (self.but_chang) / self.win_chang, relheight = (self.but_kuan) / self.win_chang)
def run_logic(self):
self.root = tkinter.Tk() self.root.title('最强计算器') self.root.minsize(self.win_chang, self.win_kuan)
self.num = tkinter.StringVar() self.num.set(0)
label = tkinter.Label(self.root, textvariable = self.num, bg='white', font=('黑体', 30), anchor='e') label.place(relx=1/40, rely=1/50, relwidth=38/40, relheight=4/30)
for i in range(1, 10): self.button_put(str(i), self.press_num, x=(i + 2) % 3, y=4 - (i // 3.1))
sym_list = [ ['0', self.press_num,1,5], ['.', self.press_num,2,5], ['=', self.press_sym,3,5], ['+', self.press_sym,3,4], ['-', self.press_sym,3,3], ['*', self.press_sym,3,2], ['/', self.press_sym,3,1], ['clear',self.press_sym,0,5], ['**',self.press_sym,2,1], ['//',self.press_sym,1,1], ] for i in sym_list: self.button_put(*i)
self.root.mainloop()
my_cal = Calculation()
|