Alexander_Smirnov

Untitled

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