user13524876
user13524876

Reputation:

Why cant I type into a tkinter Text widget?

I have the following code for my program:

main = Tk()
text = Text(main).place(relx=0.5, rely=0.5, anchor=CENTER)

Button(main, text="SEND", command=_tosend).place(relx=0.3, rely=0.8, anchor=CENTER)
Button(main, text="BACK", command=lambda: next(_main, True, main)).place(relx=0.6, rely=0.8, anchor=CENTER)
      
main.mainloop()

The problem is, when I run this code, I cant type into the Text widget at all. Ive never used the Text widget before so I've probably done something massively wrong.

How can I make it so that I can enter text into the widget?

Upvotes: 1

Views: 958

Answers (1)

TheEagle
TheEagle

Reputation: 5992

In fact, you can type into the text widget, and text is displayed, you just don't see it, because it is hidden, and that again is because the text widget is bigger than the main window. You either have to manually resize the window, or resize it in your code like this

main = Tk()
main.configure(width=615, height=415)
text = Text(main)
text.place(relx=0.5, rely=0.5, anchor=CENTER)

button_send = Button(main, text="SEND", command=_tosend)
button_send.place(relx=0.3, rely=0.8, anchor=CENTER)
button_back = Button(main, text="BACK", command=lambda: next(_main, True, main))
button_back.place(relx=0.6, rely=0.8, anchor=CENTER)
      
main.mainloop()

or, even better, drop the use of place and use pack instead.

main = Tk()
text = Text(main)
text.pack()
button_send = Button(main, text="SEND", command=_tosend)
button_send.pack()
button_back = Button(main, text="BACK", command=lambda: next(_main, True, main))
button_back.pack()
      
main.mainloop()

Upvotes: 1

Related Questions