无法更改Tkinter Python 3.x中按钮的状态

huangapple go评论63阅读模式
英文:

Can't change state of a button in Tkinter Python 3.x

问题

我正在创建一个应用程序,其中有一个窗口打开另一个窗口。在这个过程中,我想将初始化此过程的按钮的状态设置为DISABLED,但我一直收到这个错误。

def main():

    def modify():

        mods.config(state=DISABLED)
        dell.config(state=DISABLED)
        close.config(state=DISABLED)

        while True:

            errorbox.destroy()
            win = Replacy(self, dbn, self.coln, self.it, client, tree, item)
            win.resizable(False, False)
            win.grab_set()

            break

        mods.config(state=NORMAL)
        dell.config(state=NORMAL)
        close.config(state=NORMAL)

    def delete():

        mods.config(state=DISABLED)
        dell.config(state=DISABLED)
        close.config(state=DISABLED)

        def deldel():

            tree.delete(item)
            eeeeerrorbox.destroy()
            coln.delete_one(self.it)
            errorbox.destroy()

        eeeeerrorbox = Tk()
        eeeeerrorbox.geometry("255x80")
        eeeeerrorbox.title("Error")
        eeeeerrorbox.iconbitmap(False, fold + "/info.ico")

        l2 = Label(eeeeerrorbox, text='Are you sure you want to delete this entry?')
        l2.grid(row=0, column=0, columnspan=3, pady=(7, 10), padx=10, sticky="w")

        dellll = ttk.Button(eeeeerrorbox, text="Yes", command=deldel, width=7)
        dellll.grid(row=1, column=0, columnspan=2)
        closeee = ttk.Button(eeeeerrorbox, text="No", command=eeeeerrorbox.destroy, width=7)
        closeee.grid(row=1, column=2)
        eeeeerrorbox.resizable(False, False)
        mods.config(state=NORMAL)
        dell.config(state=NORMAL)
        close.config(state=NORMAL)
        eeeeerrorbox.mainloop()

    errorbox = Tk()
    errorbox.geometry("210x80")
    errorbox.title("Error")
    errorbox.iconbitmap(False, fold + "/info.ico")

    l2 = Label(errorbox, text='How would you like to edit this entry?')
    l2.grid(row=0, column=0, columnspan=3, pady=(7, 10), sticky="w")

    mods = ttk.Button(errorbox, text="Modify", command=modify, width=7)
    mods.grid(row=1, column=0)
    dell = ttk.Button(errorbox, text="Delete", command=delete, width=7)
    dell.grid(row=1, column=1)
    close = ttk.Button(errorbox, text="Close", command=errorbox.destroy, width=7)
    close.grid(row=1, column=2)
    mods.config(state=NORMAL)
    dell.config(state=NORMAL)
    close.config(state=NORMAL)
    errorbox.mainloop()

这是我一直收到的错误,无论我尝试缩进或放置代码都是一样的。

Tkinter回调中的异常
Traceback (most recent call last):
File "C:\\Users\\nagia\\AppData\\Local\\Programs\\Python\\Python310\\lib\\tkinter\\__init__.py", line 1921, in __call__
return self.func(*args)
File "g:\\nagia\\Harris\\ProductivityReports 3.0\\history.py", line 65, in modify
mods.config(state=NORMAL)
File "C:\\Users\\nagia\\AppData\\Local\\Programs\\Python\\Python310\\lib\\tkinter\\__init__.py", line 1675, in configure
return self._configure('configure', cnf, kw)
File "C:\\Users\\nagia\\AppData\\Local\\Programs\\Python\\Python310\\lib\\tkinter\\__init__.py", line 1665, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: 无效的命令名称 "!button"

然而,如果我删除第65、66和67行,那么当新窗口出现时,旧窗口与三个按钮将被销毁,好像有一个不可见的错误,但应用程序不会像以前那样崩溃。非常感谢任何帮助!!!

英文:

Im creating an app where there is a window that opens another window.
While this happens i want to set the state of the buttons that initialize this procedure as DISABLED but i keep getting this error.

def main():

    def modify():
    
        mods.config(state=DISABLED)
        dell.config(state=DISABLED)
        close.config(state=DISABLED)
    
        while True:
                    
            errorbox.destroy()
            win = Replacy(self,dbn,self.coln,self.it,client,tree,item)
            win.resizable(False,False)
            win.grab_set()
                    
                    
            break
    
        mods.config(state=NORMAL)
        dell.config(state=NORMAL)
        close.config(state=NORMAL)
    
    def delete():
    
        mods.config(state=DISABLED)
        dell.config(state=DISABLED)
        close.config(state=DISABLED)
    
            def deldel():
    
                tree.delete(item)
                eeeeerrorbox.destroy()
                coln.delete_one(self.it)
                errorbox.destroy()
    
        eeeeerrorbox = Tk()
        eeeeerrorbox.geometry("255x80")
        eeeeerrorbox.title("Error")
        eeeeerrorbox.iconbitmap(False,fold +"/info.ico")
                
        l2=Label(eeeeerrorbox,text='Are you sure you want to delete this entry?')
        l2.grid(row=0, column=0, columnspan=3, pady=(7, 10),padx=10, sticky="w")
                
        dellll = ttk.Button(eeeeerrorbox,text="Yes",command=deldel,width = 7)
        dellll.grid(row=1, column=0,columnspan=2)
        closeee = ttk.Button(eeeeerrorbox,text="No",command=eeeeerrorbox.destroy,width = 7)
        closeee.grid(row=1, column=2)
        eeeeerrorbox.resizable(False,False)
        mods.config(state=NORMAL)
        dell.config(state=NORMAL)
        close.config(state=NORMAL)
        eeeeerrorbox.mainloop()
    
    errorbox = Tk()
    errorbox.geometry("210x80")
    errorbox.title("Error")
    errorbox.iconbitmap(False,fold +"/info.ico")
            
    l2=Label(errorbox,text='How would you like to edit this entry?')
    l2.grid(row=0, column=0, columnspan=3, pady=(7, 10), sticky="w")
            
    mods = ttk.Button(errorbox,text="Modify",command=modify,width = 7)
    mods.grid(row=1, column=0)
    dell = ttk.Button(errorbox,text="Delete",command=delete,width = 7)
    dell.grid(row=1, column=1)
    close = ttk.Button(errorbox,text="Close",command=errorbox.destroy,width = 7)
    close.grid(row=1, column=2)
    mods.config(state=NORMAL)
    dell.config(state=NORMAL)
    close.config(state=NORMAL)
    errorbox.mainloop()

and this is the error i keep getting whatever i try or indenting or placing.

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\\Users\\nagia\\AppData\\Local\\Programs\\Python\\Python310\\lib\\tkinter\__init_\_.py", line 1921, in __call__
return self.func(\*args)
File "g:\\nagia\\Harris\\ProductivityReports 3.0\\history.py", line 65, in modify
mods.config(state=NORMAL)
File "C:\\Users\\nagia\\AppData\\Local\\Programs\\Python\\Python310\\lib\\tkinter\__init_\_.py", line 1675, in configure
return self._configure('configure', cnf, kw)
File "C:\\Users\\nagia\\AppData\\Local\\Programs\\Python\\Python310\\lib\\tkinter_init_.py", line 1665, in \_configure
self.tk.call(\_flatten((self.\_w, cmd)) + self.\_options(cnf))
\_tkinter.TclError: invalid command name ".!button"

however if i remove lines 65 66 67 then when the new window appears, the old one with the three buttons gets destroyed as if there is an invisible error but the app doesnt crash as it did before.

Any help is appreciated!!!!

答案1

得分: 0

例外情况是因为您已销毁窗口 errorbox,因此窗口内的部件也被销毁。@acw1668

我刚刚注意到它在 while 循环中被销毁。

我刚刚在 while 循环中移除了 errorbox.destroy(),一切都像魔术般运行。

英文:

The exception is because you have destroyed the window errorbox, so those widgets inside the windows are also destroyed. @acw1668

I just noticed that it gets destroyed in the while loop.

I just removed errorbox.destroy() in the while loop and everything works like a charm

huangapple
  • 本文由 发表于 2023年2月27日 12:37:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定