Advertisement
Guest User

Untitled

a guest
Sep 14th, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.10 KB | None | 0 0
  1. from tkinter import *
  2.  
  3.  
  4. class Window:
  5.     def __init__(self, width, height, title="FirstTK", resizable=(False, True), icon=None):
  6.         self.root = Tk()  # корневая переменная которая хранит с себе экземпляр класса Tk
  7.         self.root.title(title)
  8.         self.root.geometry(f"{width}x{height}+200+200")
  9.         self.root.resizable(resizable[0], resizable[1])
  10.         if icon:
  11.             self.root.iconbitmap(icon)
  12.         self.label_out = Label(self.root, text='0', font=("Verdana", 10, "bold"))
  13.  
  14.     def run(self):
  15.         self.draw_widgets()
  16.         self.root.mainloop()
  17.  
  18.     def draw_widgets(self):
  19.         self.draw_menu()
  20.         self.label_out.place(x=30, y=10)
  21.         Button(self.root, width=1, height=1, text='1', font=("Verdana", 10, "bold"),
  22.                command=lambda: self.test_button('1')).place(x=30, y=30)
  23.         Button(self.root, width=1, height=1, text='2', font=("Verdana", 10, "bold"),
  24.                command=lambda: self.test_button('2')).place(x=60, y=30)
  25.         Button(self.root, width=1, height=1, text='3', font=("Verdana", 10, "bold"),
  26.                command=lambda: self.test_button('3')).place(x=90, y=30)
  27.         Button(self.root, width=1, height=1, text='4', font=("Verdana", 10, "bold"),
  28.                command=lambda: self.test_button('4')).place(x=30, y=60)
  29.         Button(self.root, width=1, height=1, text='5', font=("Verdana", 10, "bold"),
  30.                command=lambda: self.test_button('5')).place(x=60, y=60)
  31.         Button(self.root, width=1, height=1, text='6', font=("Verdana", 10, "bold"),
  32.                command=lambda: self.test_button('6')).place(x=90, y=60)
  33.         Button(self.root, width=1, height=1, text='7', font=("Verdana", 10, "bold"),
  34.                command=lambda: self.test_button('7')).place(x=30, y=90)
  35.         Button(self.root, width=1, height=1, text='8', font=("Verdana", 10, "bold"),
  36.                command=lambda: self.test_button('8')).place(x=60, y=90)
  37.         Button(self.root, width=1, height=1, text='9', font=("Verdana", 10, "bold"),
  38.                command=lambda: self.test_button('9')).place(x=90, y=90)
  39.         Button(self.root, width=1, height=1, text='0', font=("Verdana", 10, "bold"),
  40.                command=lambda: self.test_button('0')).place(x=30, y=120)
  41.         Button(self.root, width=1, height=1, text='+', font=("Verdana", 10, "bold"),
  42.                command=lambda: self.test_button('+')).place(x=60, y=120)
  43.         Button(self.root, width=1, height=1, text='-', font=("Verdana", 10, "bold"),
  44.                command=lambda: self.test_button('-')).place(x=90, y=120)
  45.  
  46.         Button(self.root, width=1, height=1, text='*', font=("Verdana", 10, "bold"),
  47.                command=lambda: self.test_button('*')).place(x=30, y=150)
  48.         Button(self.root, width=1, height=1, text='/', font=("Verdana", 10, "bold"),
  49.                command=lambda: self.test_button('/')).place(x=60, y=150)
  50.  
  51.         Button(self.root, width=1, height=1, text='=', font=("Verdana", 10, "bold"),
  52.                command=self.solve).place(x=90, y=150)
  53.  
  54.     def draw_menu(self):
  55.         menu_bar = Menu(self.root)
  56.         file_menu = Menu(menu_bar, tearoff=0)
  57.         color_menu = Menu(file_menu, tearoff=0)
  58.         color_menu.add_command(label='Зеленый', command=lambda: self.change_color('green'))
  59.         color_menu.add_command(label='Красный', command=lambda: self.change_color('red'))
  60.         color_menu.add_command(label='Сброс', command=lambda: self.change_color('white'))
  61.         menu_bar.add_cascade(label='Настройки', menu=file_menu)
  62.         file_menu.add_cascade(label='Цвет фона', menu=color_menu)
  63.         self.root.configure(menu=menu_bar)
  64.  
  65.     def change_color(self, color):
  66.         self.root.configure(bg=color)
  67.  
  68.     def test_button(self, val):
  69.         if self.label_out.cget("text") in ('0', 'ERROR', 'ZERO DIVISION'):
  70.             self.label_out.config(text=val)
  71.         else:
  72.             if (val == '+' and self.label_out.cget("text")[-1] == '+') or (
  73.                     val == '-' and self.label_out.cget("text")[-2] == '-'):
  74.                 return
  75.             else:
  76.                 old_label_val = self.label_out.cget("text")
  77.                 new_val = old_label_val + val
  78.                 self.label_out.config(text=new_val)
  79.  
  80.     def solve(self):
  81.         try:
  82.             str_ = self.label_out.cget("text")
  83.             token_list = []
  84.             token = []
  85.             for i in range(len(str_)):
  86.                 if str_[i].isdigit():
  87.                     token += [str_[i]]
  88.                 else:
  89.                     token_list += [token]
  90.                     token = []
  91.                     token += [str_[i]]
  92.                     token_list += [token]
  93.                     token = []
  94.             if len(token):
  95.                 token_list += [token]
  96.             result = list(filter(lambda a: a, map(lambda x: ''.join(x), token_list)))
  97.  
  98.             def to_number(x):
  99.                 return int(x) if x.isdigit() else x
  100.  
  101.             result2 = list(map(to_number, result))
  102.             for i in range(len(result2)):
  103.                 if not isinstance(result2[i], int) and result2[i + 1] == '-':
  104.                     result2[i + 2] = ~result2[i + 2] + 1
  105.                     result2[i + 1] = None
  106.             final_result1 = list(filter(lambda x: x is not None, result2))
  107.             test_null = False
  108.  
  109.             def insert_change(x):
  110.                 nonlocal test_null
  111.                 if final_result1[i] == x:
  112.                     print(x)
  113.                     if not final_result1[i + 1]:
  114.                         test_null = True
  115.                         return
  116.                     product = final_result1[i - 1] * final_result1[i + 1] if x == '*' else final_result1[i - 1] / \
  117.                                                                                            final_result1[i + 1]
  118.                     print(product)
  119.                     final_result1[i - 1] = None
  120.                     final_result1[i] = None
  121.                     final_result1[i + 1] = None
  122.                     final_result1.insert(i + 1, product)
  123.  
  124.  
  125.             print(test_null)
  126.  
  127.             print('1', final_result1)
  128.             for i in range(len(final_result1)):
  129.                 insert_change('*')
  130.                 insert_change('/')
  131.             print('2', final_result1)
  132.             final_result2 = list(filter(lambda x: not x is None, final_result1))
  133.             print(final_result2)
  134.             if test_null:
  135.                 self.label_out.config(text='ZERO DIVISION')
  136.             else:
  137.                 solution = final_result2[0]
  138.                 for i in range(len(final_result2) - 1):
  139.                     if final_result2[i] == '+':
  140.                         solution += final_result2[i + 1]
  141.                     if final_result2[i] == '-':
  142.                         solution -= final_result2[i + 1]
  143.                 self.label_out.config(text=str(solution))
  144.         except IndexError:
  145.             self.label_out.config(text='ERROR')
  146.         except TypeError:
  147.             self.label_out.config(text='ERROR')
  148.  
  149.  
  150. if __name__ == "__main__":
  151.     window = Window(150, 200)
  152.     window.run()
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement