Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import subprocess
- import sys
- def install_package(package_name):
- try:
- subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])
- except subprocess.CalledProcessError as e:
- print(f"Ошибка установки пакета {package_name}: {e}")
- try:
- import customtkinter
- except ModuleNotFoundError:
- install_package('customtkinter')
- finally:
- import customtkinter as ctk
- class Window:
- def __init__(self, width, height, title="FirstTK", resizable=(False, True), icon=None):
- self.root = Tk() # корневая переменная которая хранит с себе экземпляр класса Tk
- self.root.title(title)
- self.root.geometry(f"{width}x{height}+200+200")
- self.root.resizable(resizable[1], resizable[0])
- if icon:
- self.root.iconbitmap(icon)
- self.label_out = Label(self.root, text='0', font=("Verdana", 15, "bold"), bg='#4a7bba', fg='white')
- def run(self):
- self.draw_widgets()
- self.root.mainloop()
- def draw_widgets(self):
- self.draw_menu()
- self.label_out.place(x=30, y=10)
- self.root.config(bg='#4a7bba')
- button = ctk.CTkButton(self.root, width=43, height=49, text='1', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('1')).place(x=30, y=40)
- button = ctk.CTkButton(self.root, width=43, height=49, text='2', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('2')).place(x=75, y=40)
- button = ctk.CTkButton(self.root, width=43, height=49, text='3', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('3')).place(x=120, y=40)
- button = ctk.CTkButton(self.root, width=43, height=49, text='4', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('4')).place(x=30, y=90)
- button = ctk.CTkButton(self.root, width=43, height=49, text='5', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('5')).place(x=75, y=90)
- button = ctk.CTkButton(self.root, width=43, height=49, text='6', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('6')).place(x=120, y=90)
- button = ctk.CTkButton(self.root, width=43, height=49, text='7', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('7')).place(x=30, y=140)
- button = ctk.CTkButton(self.root, width=43, height=49, text='8', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('8')).place(x=75, y=140)
- button = ctk.CTkButton(self.root, width=43, height=49, text='9', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('9')).place(x=120, y=140)
- button = ctk.CTkButton(self.root, width=43, height=49, text='0', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('0')).place(x=30, y=190)
- button = ctk.CTkButton(self.root, width=43, height=49, text='+', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('+')).place(x=75, y=190)
- button = ctk.CTkButton(self.root, width=43, height=49, text='-', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('-')).place(x=120, y=190)
- button = ctk.CTkButton(self.root, width=43, height=49, text='*', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('*')).place(x=30, y=240)
- button = ctk.CTkButton(self.root, width=43, height=49, text='/', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('/')).place(x=75, y=240)
- button = ctk.CTkButton(self.root, width=43, height=49, text='(', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button('(')).place(x=120, y=240)
- button = ctk.CTkButton(self.root, width=43, height=49, text=')', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=lambda: self.test_button(')')).place(x=30, y=290)
- button = ctk.CTkButton(self.root, width=43, height=49, text='=', font=("Verdana", 20, "bold"), corner_radius=10,
- fg_color='#2d496e', command=self.output).place(x=75, y=290)
- button = ctk.CTkButton(self.root, width=43, height=49, text='CL', font=("Verdana", 12, "bold"),
- fg_color='#2d496e',
- command=self.clear).place(x=120, y=290)
- def draw_menu(self):
- menu_bar = Menu(self.root)
- file_menu = Menu(menu_bar, tearoff=0)
- color_menu = Menu(file_menu, tearoff=0)
- color_menu.add_command(label='Зеленый', command=lambda: self.change_color('green'))
- color_menu.add_command(label='Красный', command=lambda: self.change_color('red'))
- color_menu.add_command(label='Сброс', command=lambda: self.change_color('white'))
- menu_bar.add_cascade(label='Настройки', menu=file_menu)
- file_menu.add_cascade(label='Цвет фона', menu=color_menu)
- self.root.configure(menu=menu_bar)
- def change_color(self, color):
- self.root.configure(bg=color)
- def test_button(self, val):
- if self.label_out.cget("text") in ('0', 'ERROR', 'ZERO DIVISION'):
- self.label_out.config(text=val)
- else:
- if (val == '+' and self.label_out.cget("text")[-1] == '+') or (
- val == '-' and self.label_out.cget("text")[-2] == '-'):
- return
- else:
- old_label_val = self.label_out.cget("text")
- new_val = old_label_val + val
- self.label_out.config(text=new_val)
- def solve_main(self):
- try:
- str_ = self.label_out.cget("text")
- token_list = []
- token = []
- for i in range(len(str_)):
- if str_[i].isdigit():
- token += [str_[i]]
- else:
- token_list += [token]
- token = []
- token += [str_[i]]
- token_list += [token]
- token = []
- if len(token):
- token_list += [token]
- result = list(filter(lambda a: a, map(lambda x: ''.join(x), token_list)))
- def to_number(x):
- return int(x) if x.isdigit() else x
- result2 = list(map(to_number, result))
- for i in range(len(result2) - 1):
- if not isinstance(result2[i], int) and result2[i + 1] == '-':
- result2[i + 2] = ~result2[i + 2] + 1
- result2[i + 1] = None
- if result2[0] == '-':
- result2[1] = ~result2[1] + 1
- result2.pop(0)
- final_result1 = list(filter(lambda x: x is not None, result2))
- test_null = False
- for i in range(len(final_result1) - 1):
- if final_result1[i] == '/' and not final_result1[i + 1]:
- test_null = True
- break
- if test_null:
- return 'ZERO DIVISION'
- def solve(s):
- def seek(stack):
- if stack:
- return stack[-1]
- ops = {
- '+': 1,
- '-': 1,
- '/': 2,
- '*': 2
- }
- stack = []
- out = []
- for i in s:
- if isinstance(i, int):
- out.append(i)
- elif i in ops:
- while stack and seek(stack) != '(' and ops[seek(stack)] >= ops[i]:
- out.append(stack.pop())
- stack.append(i)
- elif i == '(':
- stack.append(i)
- elif i == ')':
- while stack and seek(stack) != '(':
- out.append(stack.pop())
- if stack and seek(stack) == '(':
- stack.pop()
- while stack:
- out.append(stack.pop())
- stack2 = []
- for i in out:
- if isinstance(i, int):
- stack2 += [i]
- else:
- a = stack2[-2:]
- stack2.pop()
- stack2.pop()
- if i == '+':
- stack2 += [sum(a)]
- if i == '*':
- stack2 += [a[0] * a[1]]
- if i == '-':
- stack2 += [a[0] - a[1]]
- if i == '/':
- stack2 += [a[0] / a[1]]
- return stack2[0]
- return solve(final_result1)
- except (IndexError, TypeError):
- return 'ERROR'
- def output(self):
- self.label_out.config(text=str(self.solve_main()))
- def clear(self):
- self.label_out.config(text='0')
- if __name__ == "__main__":
- window = Window(250, 380)
- window.run()
- # -54*(-232+319)
- # #90ddf5
- # #2d496e
Advertisement
Add Comment
Please, Sign In to add comment