so I have a problem with tkinter, I am getting this error code
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\Shourya Kumar.DESKTOP-M2TQLD2\Desktop\papa project\main.py", line 105, in newpurchase
share_name = name_share.get()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 3043, in get
return self.tk.call(self._w, 'get')
_tkinter.TclError: invalid command name ".!entry"
so if I press the "Add Share" button and enter the details, press submit and close the window, I get this error when I press "New Purchase" button, but if I dont close the "add share" dialog box and just click on "New Purchase", works like a charm
Given below is the whole code please ignore the third button, it is not yet made
from tkinter import *
dialogmain = Tk()
dialogmain.title("Main Box")
dialogmain.geometry("1920x1080")
dialogmainlabel = Label(text="WELCOME")
dialogmainlabel.pack()
global share_name
global name_share
#code for add share button starts
def addshare():
print("Add Share button has been clicked")
dialogaddshare = Tk()
dialogaddshare.title("Add New Share")
dialogaddshare.geometry("500x400")
dialogaddsharelabel = Label(dialogaddshare, text="Add New Share")
dialogaddsharelabel.pack()
#globalling variables
global name_share
global share_name
global share_nick
#defining submit button
def submit():
share_name = name_share.get()
share_nick = nickname_share.get()
print(share_name)
print(share_nick)
popup_one = Tk()
popup_one.title("Registerd")
popup_one.geometry("400x50")
confirming_label = Label(popup_one, text=(share_name + " Registered with nickname " + share_nick + "!!!"))
confirming_label.pack()
popup_one.mainloop()
#name of share entry
name_share_label = Label(dialogaddshare, text="Enter Name of share")
name_share_label.place(x=100, y=100)
name_share = Entry(dialogaddshare, bd=5)
name_share.place(x=300, y=100)
#nickname of share
nickname_share_label = Label(dialogaddshare, text="Enter Nickname of share")
nickname_share_label.place(x=100, y=150)
nickname_share = Entry(dialogaddshare, bd=5)
nickname_share.place(x=300, y=150)
#submit button
sub_btn = Button(dialogaddshare, text='Submit', command=submit)
sub_btn.place(x=225, y=250)
addsharebutton = Button(dialogmain, text="Add Share", command=addshare, height = 3, width = 20)
addsharebutton.place(x=100, y=100)
#code for add share button ends
#code for New purchase button starts
def newpurchase():
print("New Purchase button has been clicked")
newpurchase = Tk()
newpurchase.title("Add New Share")
newpurchase.geometry("600x600")
newpurchaselabel = Label(newpurchase, text="Add New Purchase")
newpurchaselabel.pack()
# globalling vars
global entered_text
def submit():
bill_number = bill_entry.get()
date = date_entry.get()
share_name_final = variable.get()
rate = rate_entry.get()
quantity = quantity_entry.get()
confirmation_popup = Tk()
confirmation_popup.title("Confirmation")
confirmation_popup.geometry("800x100")
confirmation_variable = "Bill Number is " + bill_number + " Date is " + date + " share name is " + share_name_final + " at the rate of " + rate + " with a quantity of " + quantity + " amounting to " + amount_str
print(confirmation_variable)
confirmation_label = Label(confirmation_popup, text=confirmation_variable)
confirmation_label.pack()
# bill no
bill_label = Label(newpurchase, text="Enter Bill Number")
bill_label.place(x=100, y=100)
bill_entry = Entry(newpurchase, bd=5)
bill_entry.place(x=300, y=100)
# Date
date_label = Label(newpurchase, text="Enter the Date of purchase")
date_label.place(x=100, y=150)
date_entry = Entry(newpurchase, bd=5)
date_entry.place(x=300, y=150)
# select share
share_name = name_share.get()
shares = [
share_name
]
variable = StringVar(newpurchase)
variable.set(shares[0]) # default value
w = OptionMenu(newpurchase, variable, *shares)
w.place(x=270, y=200)
# def ok():
# print ("value is:" + variable.get())
# button = Button(newpurchase, text="OK", command=ok)
# button.place(x=300, y=235)
# quantity
quantity_label = Label(newpurchase, text="Enter quantity")
quantity_label.place(x=100, y=250)
quantity_entry = Entry(newpurchase, bd=5)
quantity_entry.place(x=300, y=250)
# rate
rate_label = Label(newpurchase, text="Enter rate")
rate_label.place(x=100, y=300)
rate_entry = Entry(newpurchase, bd=5)
rate_entry.place(x=300, y=300)
# amount
def amount():
global amount_str
quantity = quantity_entry.get()
print(quantity)
rate = rate_entry.get()
print(rate)
rate_int = int(rate)
quantity_int = int(quantity)
amount = rate_int * quantity_int
print(amount)
amount_str = str(amount)
amount_label = Label(newpurchase, text=("amount = " + amount_str))
amount_label.place(x=100, y=350)
amount_button = Button(newpurchase, text="Enter to check Amount", command=amount)
amount_button.place(x=300, y=350)
# submit button
sub_btn = Button(newpurchase, text='Submit', command=submit)
sub_btn.place(x=350, y=400)
newpurchase.mainloop()
newpurchasebutton = Button(dialogmain, text="New Purchase", command=newpurchase, height = 3, width = 20)
newpurchasebutton.place(x=600, y=100)
#code for New Purchase button ends
#code for New Sale button starts
def newsale():
print("New Sale button has been clicked")
newpurchasebutton = Button(dialogmain, text="New Sale", command=newsale, height = 3, width = 20)
newpurchasebutton.place(x=1000, y=100)
#code for New Sale button ends
dialogmain.mainloop()
I know its pretty clumsy and I apologise about that