Python Forum

Full Version: Newbie question with Tkinter Entry
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
please someone can tell me why this code works
from tkinter import *
root=Tk()
root.title("teste ao programa")
#root.configure(background="black")
lopes=Entry(root,fg="red")
lopes.pack()
lopes.focus_set()
def ola():
    print(lopes.get())
    lopes.delete(0,END)    
b=Button(root,text="OK",width=6,command=ola)
b.pack()
mainloop()
and this code does not work

from tkinter import *
root=Tk()
root.title("teste ao programa")
#root.configure(background="black")
lopes=Entry(root,fg="red").grid(row=1,column=1)
#lopes.pack()
lopes.focus_set()
def ola():
    print(lopes.get())
    lopes.delete(0,END)    
b=Button(root,text="OK",width=6,command=ola).grid(row=1,column=0,sticky=W)
#b.pack()
mainloop()
Thnak you
Separate the line for the grid statement and it will run the same as the other but using grid instead of the pack method..

from tkinter import *
root=Tk()
root.title("teste ao programa")
#root.configure(background="black")
lopes=Entry(root,fg="red")
lopes.grid(row=1,column=1)
#lopes.pack()
lopes.focus_set()
def ola():
    print(lopes.get())
    lopes.delete(0,END)
b=Button(root,text="OK",width=6,command=ola).grid(row=1,column=0,sticky=W)
#b.pack()
mainloop()
from tkinter import *
root = Tk()
root.title("teste ao programa")
root.geometry('165x60')
lopes = Entry(root,fg="red").grid(row=0,column=0)
def ola():
    print(lopes.get())
    lopes.delete(0,END)    
b=Button(root,text="OK",width=6,command=ola).grid(row=1,column=0)
mainloop()
Don't forget row and coulmn start at index 0