Home > database >  I am having problems with tkinter showing .entry rather than text
I am having problems with tkinter showing .entry rather than text

Time:09-20

from tkinter import *

def click():
    UsersName=name.get
    nameList.insert(END,name)
   
    


window=Tk()




window.title("Name List")




window.geometry("400x400")




window.configure(bg="light grey")




label1=Label(text="Enter a name:")




label1.place(x=50,y=70)




button1=Button(text="Add to list",width=10, command=click)




button1.place(x=290,y=70)




nameList=Listbox()




nameList.place(x=150,y=100,width=120,height=150)




name=Entry(text="")




name.place(x=150,y=70,width=120,height=20)




window.mainloop()

CodePudding user response:

It's because you are calling get as an attribute but its not a attribute it's a function so just change name.get to name.get(), append UsersName in the list as you are storing the current name in that variable

from tkinter import *


def click():
    UsersName = name.get()
    nameList.insert(END, UsersName)


window = Tk()
window.title("Name List")
window.geometry("400x400")
window.configure(bg="light grey")
label1 = Label(text="Enter a name:")
label1.place(x=50, y=70)
button1 = Button(text="Add to list", width=10, command=click)
button1.place(x=290, y=70)
nameList = Listbox()
nameList.place(x=150, y=100, width=120, height=150)
name = Entry(text="")
name.place(x=150, y=70, width=120, height=20)
window.mainloop()

Screenshot

enter image description here

CodePudding user response:

For Python 3.8 or later, use Walrus. Simpler and more efficient method.

def click():
    UsersName = name.get()
    nameList.insert(END, UsersName)

to:

def click():      
    nameList.insert(END,(UsersName:=name.get()))
  • Related