_tkinter.TclError:无效的命令名“!条目”

2024-09-29 23:16:57 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个tkinter的问题,我得到了这个错误代码

 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"

因此,如果我按下“添加共享”按钮并输入详细信息,按下提交并关闭窗口,当我按下“新购买”按钮时会出现此错误,但如果我不关闭“添加共享”对话框,只需单击“新购买”,效果会很好

下面给出的是完整的代码,请忽略第三个按钮,它尚未制作

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()

我知道这很笨拙,我为此道歉


Tags: textnamesharenewgetrateplacebutton
1条回答
网友
1楼 · 发布于 2024-09-29 23:16:57

所以acw的评论对我帮助很大

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 这完全解决了代码问题,如果您想查看它,heres the link to it

相关问题 更多 >

    热门问题