coding cp1251 from Tkinter import x1 x2 operation функция кнопок ввода

  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
#-*- coding:cp1251 -*-
from Tkinter import *
x1 = ''
x2 = ''
operation = ''
# функция кнопок ввода цифр
# если действие еще не задано, заполняется х1, а если задано - х2
# и значение переменной отражается на соотв. виджете
# ЗЫ. но этого не происходит! что за ...?
# см. строку 74. press(1) и остальное срабатывает при запуске, без нажатия кнопок. почему?
def press(figure):
global x1, operation, x2
if operation == '':
x1 = x1 + str(figure)
lblx1.configure(text=x1)
else:
x2 = x2 + str(figure)
lblx2.configure(text=x2)
def pressdot():
global x1, operation, x2
if operation=='':
x1=x1+'.'
lblx1.configure(text=x1)
else:
x2=x2+'.'
lblx2.configure(text=x2)
# функция операций
def sign(x): global operation; operation = x; lbloperation.configure(text=operation)
# извлечение корня отличается тем, что собственно извлечение происходит
# по нажатию кнопки 'sqrt', а не '='
def sqrt(): global x1; lblresult.configure(text=float(x1)**(0.5))
# функция кнопки = , при нажатии происходит математическая операция
# в зависимости от содержания переменной operation
def equal():
global x1, operation, x2
if operation == '+':
lblresult.configure(text=float(x1)+float(x2))
elif operation == '-':
lblresult.configure(text=float(x1)-float(x2))
elif operation == '*':
lblresult.configure(text=float(x1)*float(x2))
elif operation == '/':
lblresult.configure(text=float(x1)/float(x2))
elif operation == '':
lblresult.configure(text="O_o")
def c():
global x1, operation, x2
x1 = ''
x2 = ''
operation = ''
lblx1.configure(text=".")
lbloperation.configure(text=".")
lblx2.configure(text=".")
lblresult.configure(text=".")
# GUI
root = Tk()
root.title("pyCalc")
frame = Frame(root)
frame.pack()
lblx1 = Label(frame, text='', width=7, font=("Arial", 12, "bold"))
lbloperation = Label(frame, text='.', width=5, font=("Arial", 12, "bold"))
lblx2 = Label(frame, text='.', width=10, font=("Arial", 12, "bold"))
lbltext = Label(frame, text='Result:', width=10, font=("Arial", 15, "bold"))
lblresult = Label(frame, text='.', width=20, font=("Arial", 12, "bold"))
lblx1.grid(row=0, column=0, columnspan=2)
lbloperation.grid(row=0, column=2)
lblx2.grid(row=0, column=3, columnspan=2)
lbltext.grid(row=1, column=0, columnspan=2)
lblresult.grid(row=1, column=2, columnspan=3)
btn1 = Button(frame, text="1", width=3, font=("Arial", 20, "bold"), command = press(1))
btn2 = Button(frame, text="2", width=3, font=("Arial", 20, "bold"), command = press(2))
btn3 = Button(frame, text="3", width=3, font=("Arial", 20, "bold"), command = press(3))
btn4 = Button(frame, text="4", width=3, font=("Arial", 20, "bold"), command = press(4))
btn5 = Button(frame, text="5", width=3, font=("Arial", 20, "bold"), command = press(5))
btn6 = Button(frame, text="6", width=3, font=("Arial", 20, "bold"), command = press(6))
btn7 = Button(frame, text="7", width=3, font=("Arial", 20, "bold"), command = press(7))
btn8 = Button(frame, text="8", width=3, font=("Arial", 20, "bold"), command = press(8))
btn9 = Button(frame, text="9", width=3, font=("Arial", 20, "bold"), command = press(9))
btn0 = Button(frame, text="0", width=3, font=("Arial", 20, "bold"), command = press(0))
btndot = Button(frame, text=".", width=3, font=("Arial", 20, "bold"), command=pressdot)
btn_sum_up = Button(frame, text="+", width=3, height=3, font=("Arial", 20, "bold"), command=sign('+'))
btn_multiply = Button(frame, text="*", width=3, font=("Arial", 20, "bold"), command=sign('*'))
btn_substract = Button(frame, text="-", width=3, font=("Arial", 20, "bold"), command=sign('-'))
btn_divide = Button(frame, text="/", width=3, font=("Arial", 20, "bold"), command=sign('/'))
btn_equal =Button(frame, text="=", width=3, font=("Arial", 20, "bold"), command=equal)
btn_exit =Button(frame, text="OFF", width=3, font=("Arial", 20, "bold"), command=exit)
btn_sqrt=Button(frame, text="sqrt", width=3, font=("Arial", 20, "bold"), command=sqrt)
btn_c =Button(frame, text="C", width=3, font=("Arial", 20, "bold"), command=c)
btn1.grid(row=4, column=0, pady=4)
btn2.grid(row=4, column=1)
btn3.grid(row=4, column=2)
btn4.grid(row=3, column=0, pady=4)
btn5.grid(row=3, column=1)
btn6.grid(row=3, column=2)
btn7.grid(row=2, column=0, pady=4)
btn8.grid(row=2, column=1)
btn9.grid(row=2, column=2)
btn0.grid(row=5, column=0, padx=4, pady=4)
btndot.grid(row=5, column=1, padx=4)
btn_equal.grid(row=5, column=2, padx=4)
btn_sum_up.grid(row=4, column=3, padx=4, rowspan=2)
btn_c.grid(row=2, column=3)
btn_substract.grid(row=3, column=3)
btn_sqrt.grid(row=3, column=4)
btn_exit.grid(row=2, column=4)
btn_divide.grid(row=4, column=4)
btn_multiply.grid(row=5, column=4, padx=4)
root.mainloop()