Search code examples
pythontkinter

Exception has occurred: TclError invalid command name ".!listbox"


I have two files, in one of which some numbers are generated and they are sent to another file, but in the insert operation, it gives an error to the listbox.

import a2 
for i in range(1,100,1):
    a2.ins(i)
from tkinter import *



root = Tk() 
root.title("Wallet")
root.configure(height='300px', width='1300px',bd=10)
root.geometry('1300x300')
root.resizable(0,0)
list1 = Listbox(root,width='1300',height='250')
list1.pack()
def ins(value):
    print(f"a2 : {value}")
    list1.insert(END,value)

root.mainloop()



Hello . I have two files, in one of which some numbers are generated and they are sent to another file, but in the insert operation, it gives an error to the listbox.


Solution

  • I have two files, in one of which some numbers are generated and they are sent to another file, but in the insert operation, it gives an error to the listbox.

    The problem can be fixed.

    The first code block must be executed. So, you need to add a namespace tkinter.

    Snippet:

    import a2
    import tkinter as tk
    
    for i in range(1,100,1):
        a2.ins(i)
    
    tk.mainloop()
    

    The second block comment out mainloop()

    Snippet:

    from tkinter import *
    
    root = Tk() 
    root.title("Wallet")
    root.configure(height='300px', width='1300px',bd=10)
    root.geometry('1300x300')
    root.resizable(0,0)
    list1 = Listbox(root,width='1300',height='250')
    list1.pack()
    def ins(value):
        print(f"a2 : {value}")
        list1.insert(END,value)
     
    #root.mainloop()
    

    Screenshot:

    enter image description here