0

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

athegamer
  • 9
  • 2
  • Can you please simplify your code? I am pretty sure that most of the code isn't related to the error. Also the only way you can get that error is if you tried to call `name_share.get()` when the entry/it's master window has been destroyed. Also you shouldn't use a variable for a function and an object (`newpurchase`) – TheLizzard Jun 09 '21 at 21:00
  • See [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) (Hint: use `Toplevel` instead.) – martineau Jun 09 '21 at 21:02
  • @martineau Don't think it is a problem in this case because OP passed in the `master` argument when creating the `StringVar`. Therefore, switching to `Toplevel` would only be a hustle in this case. – TheLizzard Jun 09 '21 at 21:04
  • @TheLizzard: Well, Bryan claims in his answer to the linked question: "The best solution 99.9% of the time is to create exactly one instance of `Tk` that you use for the life of your program", but I suppose it's possible that this is one of those rare 0.1% of the time cases. – martineau Jun 09 '21 at 21:12
  • @martineau I would argue that the %s are the other way around. As long as you always pass in the `master` argument, you shouldn't encounter any problems. I am yet to encounter any problems (and most of my projects use multiple instances of `Tk`) – TheLizzard Jun 09 '21 at 21:14
  • @TheLizzard: Our own experiences are the other way around, too. – martineau Jun 09 '21 at 21:23
  • @TheLizzard , yes you are correct, I did use it outside of the object, I fixed that but now I am not able to make the variable `share_name` global, I used the the global variable command but doesnt work for some reason – athegamer Jun 10 '21 at 02:15
  • You have declared `global share_name` inside `addshare()`, you need to add `global share_name` inside `submit()` as well. Then use `share_name` directly inside `newpurchase()` by removing the line `share_name = name_share.get()` (it is the line causing the exception). – acw1668 Jun 10 '21 at 04:43
  • @acw1668 didn't fix it – athegamer Jun 10 '21 at 04:58
  • It works for me with the changes. – acw1668 Jun 10 '21 at 05:03
  • @acw1668 can you share the code – athegamer Jun 10 '21 at 05:05
  • Just the two changes mentioned in my first comment. – acw1668 Jun 10 '21 at 05:06
  • @acw1668I tried that but maybe I did something wrong which is why if you could share the code, it would be awesome – athegamer Jun 10 '21 at 05:06
  • The modified code can be found [here](https://pastebin.com/vnbqMbAR). As I said, there are only 2 changes. Test procedures: 1) press "Add Share" and enter details; 2) press "Submit" and then close the windows; 3) press "New Purchase". You don't get the exception now. – acw1668 Jun 10 '21 at 06:06
  • @acw1668 Thanks Man helped me totally, thanks again, it must have been difficult cause of the messy code – athegamer Jun 10 '21 at 18:41

1 Answers1

0

so the comment by acw helped me a lot

You have declared global share_name inside addshare(), you need to add global share_name inside submit() as well. Then use share_name directly inside newpurchase() by removing the line share_name = name_share.get() (it is the line causing the exception). – acw1668 This totally solved the code, if you wanna check it out, heres the link to it

athegamer
  • 9
  • 2