|
发表于 2018-3-2 16:29:29
|
显示全部楼层
求助:我这个计算器在按数字的过程中,label1、label2的显示数字不会变化???
- from tkinter import *
- master = Tk()
- group2 = LabelFrame(master).grid(padx=0, pady=0)
- group1 = LabelFrame(master).grid(padx=0, pady=50)
- # 存放正在输入的数字、以及两个用于实际计算的数字:
- num = []
- operator = ''
- operator_result = 0
- num_result = 0
- counter1 = 0
- counter2 = 0
- def num_get():
- num2 = num.copy()
- num1 = ''
- global num_result
- if num2[-1] == '.':
- num2.append('0')
- for each in num2:
- num1 += each
- num_result = float(num1)
- print(num_result)
-
- def cbp():
- num.append('.')
- num_get()
- def cb0():
- num.append('0')
- num_get()
- def cb1():
- num.append('1')
- num_get()
- def cb2():
- num.append('2')
- num_get()
- def cb3():
- num.append('3')
- num_get()
- def cb4():
- num.append('4')
- num_get()
- def cb5():
- num.append('5')
- num_get()
- def cb6():
- num.append('6')
- num_get()
- def cb7():
- num.append('7')
- num_get()
- def cb8():
- num.append('8')
- num_get()
- def cb9():
- num.append('9')
- num_get()
- def cba():
- global num, operator, operator_result, num_result, counter1, counter2
- if not counter1:
- counter1 = num_result
- else:
- counter2 = num_result
- num = []
- operator = '+'
- def cbm():
- global num, operator, operator_result, num_result, counter1, counter2
- if not counter1:
- counter1 = num_result
- else:
- counter2 = num_result
- num = []
- operator = '-'
- def cbl():
- global num, operator, operator_result, num_result, counter1, counter2
- if not counter1:
- counter1 = num_result
- else:
- counter2 = num_result
- num = []
- operator = '*'
- def cbd():
- global num, operator, operator_result, num_result, counter1, counter2
- if not counter1:
- counter1 = num_result
- else:
- counter2 = num_result
- num = []
- operator = '/'
- def cbe():
- global num, operator, operator_result, num_result, counter1, counter2
- if not counter1:
- operator_result = counter1
- else:
- counter2 = num_result
- if operator == '+':
- operator_result = counter1 + counter2
- if operator == '-':
- operator_result = counter1 - counter2
- if operator == '*':
- operator_result = counter1 * counter2
- if operator == '/':
- operator_result = counter1 / counter2
- print(operator_result)
- counter1 = operator_result
- def cbc():
- global num, operator, operator_result, num_result, counter1, counter2
- num = []
- operator = ''
- operator_result = 0
- num_result = 0
- counter1 = 0
- counter2 = 0
- # 数字1-9
- bp = Button(group1, text = '.', width = 2, command = cbp)
- bp.grid(row=7, column=2)
- b0 = Button(group1, text = '0', width = 6, command = cb0)
- b0.grid(row=7, column=0, columnspan=2)
- b1 = Button(group1, text = '1', width = 2, command = cb1)
- b1.grid(row=6, column=0)
- b2 = Button(group1, text = '2', width = 2, command = cb2)
- b2.grid(row=6, column=1)
- b3 = Button(group1, text = '3', width = 2, command = cb3)
- b3.grid(row=6, column=2)
- b4 = Button(group1, text = '4', width = 2, command = cb4)
- b4.grid(row=5, column=0)
- b5 = Button(group1, text = '5', width = 2, command = cb5)
- b5.grid(row=5, column=1)
- b6 = Button(group1, text = '6', width = 2, command = cb6)
- b6.grid(row=5, column=2)
- b7 = Button(group1, text = '7', width = 2, command = cb7)
- b7.grid(row=4, column=0)
- b8 = Button(group1, text = '8', width = 2, command = cb8)
- b8.grid(row=4, column=1)
- b9 = Button(group1, text = '9', width = 2, command = cb9)
- b9.grid(row=4, column=2)
- # 运算符号
- ba = Button(group1, text = '+', width = 4, height = 3, command = cba)
- ba.grid(row=4, rowspan=2, column=3)
- bm = Button(group1, text = '-', width = 4, command = cbm)
- bm.grid(row=3, column=3)
- bp = Button(group1, text = '*', width = 2, command = cbl)
- bp.grid(row=3, column=2)
- bd = Button(group1, text = '/', width = 2, command = cbd)
- bd.grid(row=3, column=1)
- be = Button(group1, text = 'Enter', width = 4, height = 3, command = cbe)
- be.grid(row=6, rowspan=2, column=3)
- bc = Button(group1, text = 'c', width = 2, command = cbc)
- bc.grid(row=3, column=0)
- # 运算结果
- v1 = StringVar()
- label1 = Label(group2, background="white", textvariable=v1).grid(row=1, rowspan=4)
- v1.set(str(operator_result))
- v2 = StringVar()
- label2 = Label(group2, background="white", textvariable=v2).grid(row=0, rowspan=4)
- v2.set(str(counter1) + str(operator) + str(counter2))
- mainloop()
复制代码
|
|