Alexander_Smirnov

Untitled

Jun 28th, 2024
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.69 KB | None | 0 0
  1. from tkinter import *
  2. import customtkinter as ctk
  3.  
  4.  
  5. class Window:
  6.     def __init__(self, width, height, title="FirstTK", resizable=(False, True), icon=None):
  7.         self.root = Tk()  # корневая переменная которая хранит с себе экземпляр класса Tk
  8.         self.root.title(title)
  9.         self.root.geometry(f"{width}x{height}+200+200")
  10.         self.root.resizable(resizable[1], resizable[0])
  11.         if icon:
  12.             self.root.iconbitmap(icon)
  13.         self.label_out = Label(self.root, text='0', font=("Verdana", 15, "bold"), bg='#4a7bba', fg='white')
  14.  
  15.     def run(self):
  16.         self.draw_widgets()
  17.         self.root.mainloop()
  18.  
  19.     def draw_widgets(self):
  20.         self.draw_menu()
  21.         self.label_out.place(x=30, y=10)
  22.         self.root.config(bg='#4a7bba')
  23.         button = ctk.CTkButton(self.root, width=43, height=49, text='1', font=("Verdana", 20, "bold"), corner_radius=10,
  24.                                fg_color='#2d496e', command=lambda: self.test_button('1')).place(x=30, y=40)
  25.         button = ctk.CTkButton(self.root, width=43, height=49, text='2', font=("Verdana", 20, "bold"), corner_radius=10,
  26.                                fg_color='#2d496e', command=lambda: self.test_button('2')).place(x=75, y=40)
  27.         button = ctk.CTkButton(self.root, width=43, height=49, text='3', font=("Verdana", 20, "bold"), corner_radius=10,
  28.                                fg_color='#2d496e', command=lambda: self.test_button('3')).place(x=120, y=40)
  29.         button = ctk.CTkButton(self.root, width=43, height=49, text='4', font=("Verdana", 20, "bold"), corner_radius=10,
  30.                                fg_color='#2d496e', command=lambda: self.test_button('4')).place(x=30, y=90)
  31.         button = ctk.CTkButton(self.root, width=43, height=49, text='5', font=("Verdana", 20, "bold"), corner_radius=10,
  32.                                fg_color='#2d496e', command=lambda: self.test_button('5')).place(x=75, y=90)
  33.         button = ctk.CTkButton(self.root, width=43, height=49, text='6', font=("Verdana", 20, "bold"), corner_radius=10,
  34.                                fg_color='#2d496e', command=lambda: self.test_button('6')).place(x=120, y=90)
  35.         button = ctk.CTkButton(self.root, width=43, height=49, text='7', font=("Verdana", 20, "bold"), corner_radius=10,
  36.                                fg_color='#2d496e', command=lambda: self.test_button('7')).place(x=30, y=140)
  37.         button = ctk.CTkButton(self.root, width=43, height=49, text='8', font=("Verdana", 20, "bold"), corner_radius=10,
  38.                                fg_color='#2d496e', command=lambda: self.test_button('8')).place(x=75, y=140)
  39.         button = ctk.CTkButton(self.root, width=43, height=49, text='9', font=("Verdana", 20, "bold"), corner_radius=10,
  40.                                fg_color='#2d496e', command=lambda: self.test_button('9')).place(x=120, y=140)
  41.         button = ctk.CTkButton(self.root, width=43, height=49, text='0', font=("Verdana", 20, "bold"), corner_radius=10,
  42.                                fg_color='#2d496e', command=lambda: self.test_button('0')).place(x=30, y=190)
  43.         button = ctk.CTkButton(self.root, width=43, height=49, text='+', font=("Verdana", 20, "bold"), corner_radius=10,
  44.                                fg_color='#2d496e', command=lambda: self.test_button('+')).place(x=75, y=190)
  45.         button = ctk.CTkButton(self.root, width=43, height=49, text='-', font=("Verdana", 20, "bold"), corner_radius=10,
  46.                                fg_color='#2d496e', command=lambda: self.test_button('-')).place(x=120, y=190)
  47.         button = ctk.CTkButton(self.root, width=43, height=49, text='*', font=("Verdana", 20, "bold"), corner_radius=10,
  48.                                fg_color='#2d496e', command=lambda: self.test_button('*')).place(x=30, y=240)
  49.         button = ctk.CTkButton(self.root, width=43, height=49, text='/', font=("Verdana", 20, "bold"), corner_radius=10,
  50.                                fg_color='#2d496e', command=lambda: self.test_button('/')).place(x=75, y=240)
  51.         button = ctk.CTkButton(self.root, width=43, height=49, text='(', font=("Verdana", 20, "bold"), corner_radius=10,
  52.                                fg_color='#2d496e', command=lambda: self.test_button('(')).place(x=120, y=240)
  53.         button = ctk.CTkButton(self.root, width=43, height=49, text=')', font=("Verdana", 20, "bold"), corner_radius=10,
  54.                                fg_color='#2d496e', command=lambda: self.test_button(')')).place(x=30, y=290)
  55.         button = ctk.CTkButton(self.root, width=43, height=49, text='=', font=("Verdana", 20, "bold"), corner_radius=10,
  56.                                fg_color='#2d496e', command=self.output).place(x=75, y=290)
  57.         button = ctk.CTkButton(self.root, width=43, height=49, text='CL', font=("Verdana", 12, "bold"),
  58.                                fg_color='#2d496e',
  59.                                command=self.clear).place(x=120, y=290)
  60.  
  61.     def draw_menu(self):
  62.         menu_bar = Menu(self.root)
  63.         file_menu = Menu(menu_bar, tearoff=0)
  64.         color_menu = Menu(file_menu, tearoff=0)
  65.         color_menu.add_command(label='Зеленый', command=lambda: self.change_color('green'))
  66.         color_menu.add_command(label='Красный', command=lambda: self.change_color('red'))
  67.         color_menu.add_command(label='Сброс', command=lambda: self.change_color('white'))
  68.         menu_bar.add_cascade(label='Настройки', menu=file_menu)
  69.         file_menu.add_cascade(label='Цвет фона', menu=color_menu)
  70.         self.root.configure(menu=menu_bar)
  71.  
  72.     def change_color(self, color):
  73.         self.root.configure(bg=color)
  74.  
  75.     def test_button(self, val):
  76.         if self.label_out.cget("text") in ('0', 'ERROR', 'ZERO DIVISION'):
  77.             self.label_out.config(text=val)
  78.         else:
  79.             if (val == '+' and self.label_out.cget("text")[-1] == '+') or (
  80.                     val == '-' and self.label_out.cget("text")[-2] == '-'):
  81.                 return
  82.             else:
  83.                 old_label_val = self.label_out.cget("text")
  84.                 new_val = old_label_val + val
  85.                 self.label_out.config(text=new_val)
  86.  
  87.     def solve_main(self):
  88.         try:
  89.             str_ = self.label_out.cget("text")
  90.             token_list = []
  91.             token = []
  92.             for i in range(len(str_)):
  93.                 if str_[i].isdigit():
  94.                     token += [str_[i]]
  95.                 else:
  96.                     token_list += [token]
  97.                     token = []
  98.                     token += [str_[i]]
  99.                     token_list += [token]
  100.                     token = []
  101.             if len(token):
  102.                 token_list += [token]
  103.             result = list(filter(lambda a: a, map(lambda x: ''.join(x), token_list)))
  104.  
  105.             def to_number(x):
  106.                 return int(x) if x.isdigit() else x
  107.  
  108.             result2 = list(map(to_number, result))
  109.             for i in range(len(result2) - 1):
  110.                 if not isinstance(result2[i], int) and result2[i + 1] == '-':
  111.                     result2[i + 2] = ~result2[i + 2] + 1
  112.                     result2[i + 1] = None
  113.             if result2[0] == '-':
  114.                 result2[1] = ~result2[1] + 1
  115.                 result2.pop(0)
  116.             final_result1 = list(filter(lambda x: x is not None, result2))
  117.             test_null = False
  118.             for i in range(len(final_result1) - 1):
  119.                 if final_result1[i] == '/' and not final_result1[i + 1]:
  120.                     test_null = True
  121.                     break
  122.  
  123.             if test_null:
  124.                 return 'ZERO DIVISION'
  125.  
  126.             def solve(s):
  127.                 def seek(stack):
  128.                     if stack:
  129.                         return stack[-1]
  130.  
  131.                 ops = {
  132.                     '+': 1,
  133.                     '-': 1,
  134.                     '/': 2,
  135.                     '*': 2
  136.                 }
  137.  
  138.                 stack = []
  139.                 out = []
  140.  
  141.                 for i in s:
  142.                     if isinstance(i, int):
  143.                         out.append(i)
  144.                     elif i in ops:
  145.                         while stack and seek(stack) != '(' and ops[seek(stack)] >= ops[i]:
  146.                             out.append(stack.pop())
  147.                         stack.append(i)
  148.                     elif i == '(':
  149.                         stack.append(i)
  150.                     elif i == ')':
  151.                         while stack and seek(stack) != '(':
  152.                             out.append(stack.pop())
  153.                         if stack and seek(stack) == '(':
  154.                             stack.pop()
  155.  
  156.                 while stack:
  157.                     out.append(stack.pop())
  158.                 stack2 = []
  159.                 for i in out:
  160.                     if isinstance(i, int):
  161.                         stack2 += [i]
  162.                     else:
  163.                         a = stack2[-2:]
  164.                         stack2.pop()
  165.                         stack2.pop()
  166.                         if i == '+':
  167.                             stack2 += [sum(a)]
  168.                         if i == '*':
  169.                             stack2 += [a[0] * a[1]]
  170.                         if i == '-':
  171.                             stack2 += [a[0] - a[1]]
  172.                         if i == '/':
  173.                             stack2 += [a[0] / a[1]]
  174.                 return stack2[0]
  175.  
  176.             return solve(final_result1)
  177.         except IndexError:
  178.             return 'ERROR'
  179.         except TypeError:
  180.             return 'ERROR'
  181.  
  182.     def output(self):
  183.         self.label_out.config(text=str(self.solve_main()))
  184.  
  185.     def clear(self):
  186.         self.label_out.config(text='0')
  187.  
  188.  
  189. if __name__ == "__main__":
  190.     window = Window(250, 380)
  191.     window.run()
  192.  
  193. # -54*(-232+319)
  194. # #90ddf5
  195.  
  196. # #2d496e
Advertisement
Add Comment
Please, Sign In to add comment