Unable to access Parent/main class variables in TopLevel class in Tkinter
I am creating a simple GUI for my personal use to manage monthly expenses between my roommates. I made the simple GUI using Tkinter. The idea is to enter the price (values) in the main window and when click the button, "Calculate Expenses", it opens new window with all the calculations. The GUI part is complete. However the problem is I am unable to access the main window variables/elements into the new window. And I don't know how to work with classes, especially in Tkinter. The logic part is complete. It's simple sum and subtraction. However with Tkinter, I am helpless. Following is the code:
from tkinter import *
from tkinter import ttk
# ----------------------------------------------------------------------------- #
# ---- Output Window ---- #
class Window(Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('1024x600')
self.title('Output Window')
self.configure(bg='white')
self.resizable(False, False)
# ----------------------------------------------------------------------------- #
# Title
Label(self, text="Monthly Expense Calculator", font="Barlow 35 bold", fg='#091747', bg='white',
justify=CENTER).pack(pady=4)
# ---- Expense Details Entries ---- #
Label(self, text="Flat + Society Total", font="Barlow 13 bold", fg='#091747', bg='white').place(x=46, y=80)
flat_society_total = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=50, y=110, width=150, height=35)
Label(self, text="Food Total", font="Barlow 13 bold", fg='#091747', bg='white').place(x=80, y=150)
food_total = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=50, y=180, width=150, height=35)
Label(self, text="Utilities Total", font="Barlow 13 bold", fg='#091747', bg='white').place(x=72, y=225)
utility_total = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=50, y=255, width=150, height=35)
Label(self, text="Per Head: Flat + Security", font="Barlow 13 bold", fg='#091747', bg='white').place(x=20, y=295)
per_head_flat_security = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=50, y=325, width=150,height=35)
Label(self, text="Per Head: Food", font="Barlow 13 bold", fg='#091747', bg='white').place(x=60, y=365)
per_head_food = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=50, y=395,width=150, height=35)
Label(self, text="Per Head: Utility", font="Barlow 13 bold", fg='#091747', bg='white').place(x=60, y=435)
per_head_utility = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=50, y=465, width=150, height=35)
Label(self, text="Per Head: Cook", font="Barlow 13 bold", fg='#091747', bg='white').place(x=60, y=505)
per_head_cook = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=50, y=535, width=150, height=35)
# ----------------------------------------------------------------------------- #
# ---- Members Entries ---- #
Label(self, text="Members", font="Barlow 18 bold", fg='White', bg="#091747").place(x=250, y=90)
# Names
Label(self, text="Huzaifa", font="Barlow 15 bold", fg='#091747', bg='white').place(x=268, y=150)
Label(self, text="Talha", font="Barlow 15 bold", fg='#091747', bg='white').place(x=275, y=200)
Label(self, text="Zubair", font="Barlow 15 bold", fg='#091747', bg='white').place(x=275, y=250)
Label(self, text="Abdullah", font="Barlow 15 bold", fg='#091747', bg='white').place(x=265, y=300)
Label(self, text="Hassan", font="Barlow 15 bold", fg='#091747', bg='white').place(x=272, y=350)
Label(self, text="Imran", font="Barlow 15 bold", fg='#091747', bg='white').place(x=275, y=400)
Label(self, text="Tariq", font="Barlow 15 bold", fg='#091747', bg='white').place(x=275, y=450)
# ---- Food Payable Heading ---- #
Label(self, text="Food Payable", font="Barlow 18 bold", fg='White', bg="#091747").place(x=400, y=90)
# Food Payable Entries
huzaifa_food_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=410, y=155, width=150,
height=35)
talha_food_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=410, y=205, width=150,
height=35)
zubair_food_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=410, y=255, width=150,
height=35)
abdullah_food_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=410, y=305, width=150,
height=35)
hassan_food_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=410, y=355, width=150,
height=35)
imran_food_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=410, y=405, width=150,
height=35)
tariq_food_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER, state='disabled').place(x=410, y=455,
width=150, height=35)
# ---- Utility Payable Heading ---- #
Label(self, text="Utility Payable", font="Barlow 18 bold", fg='White', bg="#091747").place(x=600, y=90)
# Food Payable Entries
huzaifa_utility_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=610, y=155, width=150,
height=35)
talha_utility_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=610, y=205, width=150,
height=35)
zubair_utility_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=610, y=255, width=150,
height=35)
abdullah_utility_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=610, y=305, width=150,
height=35)
hassan_utility_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=610, y=355, width=150,
height=35)
imran_utility_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=610, y=405, width=150,
height=35)
tariq_utility_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=610, y=455, width=150, height=35)
# ---- Total Payable Heading ---- #
Label(self, text="Total Payable", font="Barlow 18 bold", fg='White', bg="#091747").place(x=805, y=90)
# Food Payable Entries
huzaifa_total_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=810, y=155, width=150,
height=35)
talha_total_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=810, y=205, width=150,
height=35)
zubair_total_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=810, y=255, width=150,
height=35)
abdullah_total_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=810, y=305, width=150,
height=35)
hassan_total_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=810, y=355, width=150,
height=35)
imran_total_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=810, y=405, width=150,
height=35)
tariq_total_pay = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=810, y=455, width=150,
height=35)
# ---- Grand Total Payable ---- #
Label(self, text="Grand Total Payable", font="Barlow 18 bold", fg='White', bg="#091747").place(x=250, y=535)
grand_total_payable = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=550, y=535, width=150,
height=35)
# ----------------------------------------------------------------------------- #
# ---- Exit Button ---- #
exit_button = Button(self, text='Exit', font="Barlow 15 bold", width=5, height=1, fg='white',
bg="Red", borderwidth=0, command=self.destroy).place(relx=0.9, rely=0.92, anchor=CENTER)
# ----------------------------------------------------------------------------- #
# ---- Main Window ---- #
class App(Tk):
def __init__(self):
super().__init__()
self.geometry('1024x600')
self.title('Main Window')
self.configure(bg='white')
self.resizable(False, False)
# Title
Label(self, text="Monthly Expense Calculator", font="Barlow 35 bold", fg='#091747', bg='white',
justify=CENTER).pack(pady=4)
# ------------------------------------------------------------------------------------------------ #
# ----- Flat expense ----- #
Label(self, text="Flat Rent", font="Barlow 16 bold", fg='#091747', bg='white').place(x=85, y=150)
flat_rent = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=54, y=195, width=150, height=35)
# ----- Society Charges ----- #
Label(self, text="Society Charges", font="Barlow 16 bold", fg='#091747', bg='white').place(x=45, y=250)
society_charges = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=54, y=295, width=150, height=35)
# ----- Cook Salary ----- #
Label(self, text="Cook's Salary", font="Barlow 16 bold", fg='#091747', bg='white').place(x=58, y=350)
cook_salary = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=54, y=395, width=150, height=35)
# ------------------------------------------------------------------------------------------------ #
# Members Heading
Label(self, text="Members", font="Barlow 18 bold", fg='White', bg="#091747").place(x=280, y=90)
# Members Names
Label(self, text="Huzaifa", font="Barlow 15 bold", fg='#091747', bg='white').place(x=298, y=150)
Label(self, text="Talha", font="Barlow 15 bold", fg='#091747', bg='white').place(x=305, y=200)
Label(self, text="Zubair", font="Barlow 15 bold", fg='#091747', bg='white').place(x=305, y=250)
Label(self, text="Abdullah", font="Barlow 15 bold", fg='#091747', bg='white').place(x=295, y=300)
Label(self, text="Hassan", font="Barlow 15 bold", fg='#091747', bg='white').place(x=302, y=350)
Label(self, text="Imran", font="Barlow 15 bold", fg='#091747', bg='white').place(x=305, y=400)
Label(self, text="Tariq", font="Barlow 15 bold", fg='#091747', bg='white').place(x=305, y=450)
# ------------------------------------------------------------------------------------------------ #
# Food Expense Heading
Label(self, text="Food Expense", font="Barlow 18 bold", fg='White', bg="#091747").place(x=500, y=90)
# Food Entries
huzaifa_food = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=510, y=155, width=150,
height=35)
talha_food = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=510, y=205, width=150,
height=35)
zubair_food = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=510, y=255, width=150,
height=35)
abdullah_food = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=510, y=305, width=150,
height=35)
hassan_food = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=510, y=355, width=150,
height=35)
imran_food = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=510, y=405, width=150,
height=35)
tariq_food = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER, state='disabled').place(x=510, y=455,
width=150, height=35)
# -------------------------------------------------------------------------------------- #
# Utility Expense Heading
Label(self, text="Utilities Expense", font="Barlow 18 bold", fg='White', bg="#091747").place(x=750, y=90)
# Food Entries
huzaifa_utility = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=776, y=155, width=150,
height=35)
talha_utility = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=776, y=205, width=150,
height=35)
zubair_utility = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=776, y=255, width=150,
height=35)
abdullah_utility = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=776, y=305, width=150,
height=35)
hassan_utility = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=776, y=355, width=150,
height=35)
imran_utility = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=776, y=405, width=150,
height=35)
tariq_utility = Entry(self, font='15', bg='#efe4b0', bd=0, justify=CENTER).place(x=776, y=455, width=150,
height=35)
# -------------------------------------------------------------------------------------- #
# ----- Button to Calculate Expenses in new window ----- #
button1 = Button(self, text='Calculate Expenses', font="Barlow 15 bold", width=18, height=1, fg='White',
bg="#091747", borderwidth=0, command=self.open_window).place(relx=0.5, rely=0.92, anchor=CENTER)
button2 = Button(self, text='Exit', font="Barlow 15 bold", width=5, height=1, fg='white',
bg="Red", borderwidth=0, command=self.destroy).place(relx=0.9, rely=0.92, anchor=CENTER)
# ----- Function to open new window ----- #
def open_window(self):
window = Window(self)
window.grab_set()
# -------------------------------------------------------------------------------------- #
if __name__ == "__main__":
app = App()
app.mainloop()
# The Logic
flat_society_total = flat_rent + society_charges
food_total = huzaifa_food + talha_food + zubair_food + abdullah_food + hassan_food + imran_food
utility_total = huzaifa_utility + talha_utility + zubair_utility + abdullah_utility + hassan_utility + imran_utility + tariq_utility
per_head_flat_security = flat_society_total / 7
per_head_food = food_total / 6
per_head_utility = utility_total / 7
per_head_cook = cook_salary / 6
# Food Payable
huzaifa_food_pay = per_head_food - huzaifa_food
talha_food_pay = per_head_food -talha_food
zubair_food_pay = per_head_food - zubair_food
abdullah_food_pay = per_head_food - abdullah_food
hassan_food_pay = per_head_food - hassan_food
imran_food_pay = per_head_food - imran_food
# Utility Payable
huzaifa_utility_pay = per_head_utility - huzaifa_utility
talha_utility_pay = per_head_utility - talha_utility
zubair_utility_pay = per_head_utility - zubair_utility
abdullah_utility_pay = per_head_utility - abdullah_utility
hassan_utility_pay = per_head_utility - hassan_utility
imran_utility_pay = per_head_utility - imran_utility
tariq_utility_pay = per_head_utility - tariq_utility
# Total Payable
huzaifa_total_pay = per_head_flat_security + per_head_cook + huzaifa_food_pay + huzaifa_utility_pay
talha_total_pay = per_head_flat_security + per_head_cook + talha_food_pay + talha_utility_pay
zubair_total_pay = per_head_flat_security + per_head_cook + zubair_food_pay + zubair_utility_pay
abdullah_total_pay = per_head_flat_security + per_head_cook + abdullah_food_pay + abdullah_utility_pay
hassan_total_pay = per_head_flat_security + per_head_cook + hassan_food_pay + hassan_utility_pay
imran_total_pay = per_head_flat_security + per_head_cook + imran_food_pay + imran_utility_pay
tariq_total_pay = per_head_flat_security + tariq_utility_pay
# Grand Total Payable
grand_total_payable = huzaifa_total_pay + talha_total_pay + zubair_total_pay + abdullah_total_pay + hassan_total_pay + imran_total_pay + tariq_total_pay
See the attached pictures. (Please note I manually write the values for reference to what I want)
🔴 No definitive solution yet