1

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?

  • 1
    You _can_ type into the text widget. It's just that the upper-left corner is invisible, and that's where the text goes when you type. Try resizing your window and you'll see. It would be good if you could explain what you expect to happen. Calling `place` with the arguments you've chosen doesn't seem to make sense, but it's not clear what you're trying to accomplish. – Bryan Oakley Feb 04 '21 at 15:16
  • @BryanOakley ahh that makes sense, i need to make the widget smaller then? Also, the arguments for `place` are the results of a search i did about a year and a half ago to finetune positioning so i can have more control over where the widgets are placed. My only issue was that i could not see the text so now i know that it was just the size of the `Text` widget, my solution is to just make it smaller or make the window bigger. –  Feb 04 '21 at 17:21
  • If you're just now learning tkinter, I strongly encourage you to _not_ use `place`. You don't want fine control because that means you must exercise fine control. Tkinter is exceptionally good at making widgets fit. `pack` and `grid` make writing responsive UIs much easier than when using `place`. – Bryan Oakley Feb 04 '21 at 17:37
  • @LegacyCoding "my solution is to just make it smaller or make the window bigger" - see my answer for details, i edited it just now – TheEagle Feb 04 '21 at 17:49

1 Answers1

1

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()
TheEagle
  • 5,808
  • 3
  • 11
  • 39
  • They don't _have_ to call `pack`. They are already using `place`, they are just using it poorly. – Bryan Oakley Feb 04 '21 at 15:17
  • @BryanOakley what do you mean with poorly ? – TheEagle Feb 04 '21 at 15:19
  • @BryanOakley it works, thats the main thing isn't it ? – TheEagle Feb 04 '21 at 15:20
  • No, just providing a working solution isn't the only point. Your answer needs to be accurate, and preferably explain why the OP's code doesn't work. By "using it poorly" I mean that the widget is on the screen and they can type in it. The problem is that the upper-left corner of the text widget is off screen because of how they called `place` so what they type is invisible. – Bryan Oakley Feb 04 '21 at 15:22
  • BTW, if you call `place` and then call `pack`, anything that was done by `place` is forgotten. Use one or the other, not both. – Bryan Oakley Feb 04 '21 at 17:39
  • @BryanOakley i am currently fixing my example, they just have to resize the window so that the textview fits into it. With their current code, you just must type in enough text, then parts of it will be visible. – TheEagle Feb 04 '21 at 17:40