I am making a calculator project in python using tkinter with oop concept and the i cannot figure out the problem with the function class . The function class is responsible for division multiplication etc function. The window class is the canvas and i have made two other class for buttons and display, The function class is messy .
i think its a conceptual problem in the code but i really cont figure it out
import tkinter as tk
from tkinter import ttk
from tkinter import *
class Window(tk.Tk):
def __init__(self,size,title):
super().__init__()
self.geometry(f"{size[0]}x{size[1]}")
self.minsize(size[0],size[1])
self.title(title)
# self.attributes('-alpha',0.85)
#placing button frame
self.button_frame = Button_frame(self)
self.display_frame = Display_frame(self)
#crating frame for the widgets
#animated slidebar
class Window_Frame(ttk.Frame):
def __init__(self,parent,start_pos,end_pos):
super().__init__(master= parent)
#general attributes
self.start_pos= start_pos
self.end_pos = end_pos
self.width = abs(start_pos - end_pos)
self.place(relx=self.start_pos,rely=0, relheight=1,relwidth=self.width)
#frame for the display
class Display_frame(ttk.Frame):
def __init__(self,parent):
super().__init__(master= parent)
self.pack(expand = True, fill = 'both',side = 'top')
self.text_input = StringVar()
self.entry_fild = ttk.Entry(self, textvariable=self.text_input)
self.entry_fild.pack(expand=True, fill='both')
#frame for the button widget
class Button_frame(ttk.Frame):
def __init__(self,parent):
super().__init__(master= parent)
self.pack(side='bottom',expand = True, fill = 'both')
Frame_Buttons(self)
class Frame_Buttons(ttk.Button):
def __init__(self,parent):
super().__init__(master=parent)
self.pack(side='bottom',expand = True,fill = 'both',pady=5,padx = 5)
self.buttons()
self.funct = Function()
self.delete = self.funct.delete
self.clear = self.funct.clear
self.eval = self.funct.eval
def buttons(self):
button1 = ttk.Button(self, text="1",command=lambda: self.funct.numb_press('1'))
button2 = ttk.Button(self, text="2", command=lambda: self.funct.numb_press('2'))
button3 = ttk.Button(self, text="3", command=lambda: self.funct.numb_press('3'))
button4 = ttk.Button(self, text="4", command=lambda: self.funct.numb_press('4'))
button5 = ttk.Button(self, text="5", command=lambda: self.funct.numb_press('5'))
button6 = ttk.Button(self, text="6", command=lambda: self.funct.numb_press('6'))
button7 = ttk.Button(self, text="7", command=lambda: self.funct.numb_press('7'))
button8 = ttk.Button(self, text="8", command=lambda: self.funct.numb_press('8'))
button9 = ttk.Button(self, text="9", command=lambda: self.funct.numb_press('9'))
button0 = ttk.Button(self, text="0", command=lambda: self.funct.numb_press('0'))
# opoaration button
but_add = ttk.Button(self, text="+",command=lambda: self.funct.numb_press('+'))
but_sub = ttk.Button(self, text="-",command=lambda: self.funct.numb_press('-'))
but_mul = ttk.Button(self, text="*",command=lambda: self.funct.numb_press('*'))
but_div = ttk.Button(self, text="/",command=lambda: self.funct.numb_press('/'))
but_per = ttk.Button(self, text="%",command=lambda: self.funct.numb_press('%'))
but_bra = ttk.Button(self, text='(',command=lambda: self.funct.numb_press('('))
but_bra2 = ttk.Button(self, text=')',command=lambda:self.funct.numb_press(')'))
but_eql = ttk.Button(self, text="=", command=self.eval)
but_del = ttk.Button(self, text="DEL", command=self.delete)
but_clr = ttk.Button(self, text="CLR", command=self.clear)
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
button3.grid(row=0, column=2)
button4.grid(row=1, column=0)
button5.grid(row=1, column=1)
button6.grid(row=1, column=2)
button7.grid(row=2, column=0)
button8.grid(row=2, column=1)
button9.grid(row=2, column=2)
button0.grid(row=3, column=1)
but_add.grid(column=3, row=0)
but_sub.grid(column=3, row=1)
but_mul.grid(column=3, row=2)
but_div.grid(column=4, row=0)
but_del.grid(column=4, row=1)
but_clr.grid(column=4, row=2)
but_per.grid(column=3, row=3)
but_eql.grid(column=4, row=3)
but_bra.grid(column=0, row=3)
but_bra2.grid(column=2, row=3)
class Function():
def __init__(self):
# definingg variables for oparation functions
self.Expression = ""
# defining the functions
def numb_press(self, numb):
self.expression = numb
self.display_frame
def clear(self):
self.expression = ""
self.text_variable.set(self.expression)
def delete(self):
temp = self.text_variable.get()
self.text_variable.set(str(temp[:-1]))
def eval (self):
temp = self.text_variable.get()
self.text_variable.input(str(eval(temp)))
if __name__ == '__main__':
app =Window((500,450),'app')
app.mainloop()