wrap text in an entry widget in tkinter - python

I want to create an interactive text box in a tkinter GUI, the text box should get the text to wrap to the next line after the length of 30 characters, like it would do using the wraplength=30 attribute in a label widget. I am trying to get it to work using an Entry widget, this is what I am aiming for (apart from the wraplength attribute needs to be changed to something that works in an Entry widget:
ent = Entry(root, width=30, wraplength=30)
I also need to be able to make the Entry widget taller than one line, is there a way i can do that, for example making it vertically fill a frame (similarly to expand=True making it horizontally fill a frame).
Thank you!

I believe that Entry widgets are single line only, you may want to try Text widget
https://tkdocs.com/tutorial/morewidgets.html#text

The entry widget doesn't support wrapping. If you want to have multiple lines -- even if it's one long line that's wrapped -- you'll need to use either a Text, Label, or Message widget. Only the Text widget supports user input, the other two are strictly for display.
As for making the entry widget taller, you can do that with a geometry manager. For example, you can use the sticky option of grid or the fill and expand options of pack. This will make the widget taller, but the text will still just appear as a single line.

but text can't use (show="")

Related

How to align lines inside Text widget with grid rows outside the widget?

I have a tkinter frame with many rows. Each row has some labels and an entry widget placed with the widget.grid() function. In the last column I want to place a Text widget, in which each line coincides with each grid row outside the Text widget, i.e., the lines in the Text widget should be aligned with the other widgets.
For now I just used text.grid(row=1, column=5, rowspan=rw) where rw is the number of rows, and I tried adjusting the line spacings with text.config(spacing1=...), text.config(spacing2=...) and text.config(spacing3=...). However, adjusting the line spacings for some reason messes up with the row spacing outside the text widget - as I increase the line spacing in the text widget, the padding of the outside rows also seems to increase.
The reason I'm doing this is that I want the last column to be scrollable horizontally, and this seemed shorter/easier than placing a canvas and adding the labels and scrollbar to it. If you have other ideas for how to achieve this I'll be glad to hear them.

I can't for the life of me get this ttk / tkinter separator to expand

I have to write a program for some coursework I'm doing, and I chose to do an A-Level revision game to help others. I'm trying to get use a ttk separator to split up the area of the window where the game part occurs, and the area of the window where the question sits. My window is split up into 3 frames; one with 4 labels in, one with 4 buttons in and one with a ttk.Separator widget and a label. I cannot get the separator however to span the whole window.
I've been looking around and testing things, but nothing seems to work. I've looked at these two previous posts in terms of on this website:
ttk.Separator set the length/width
A Label in a Frame in a window won't stretch, why?
but neither of these solutions seemed to fix my problem, and I'm now out of ideas. Any help would be greatly appreciated.
main_revision_game_question_frame = Frame(parent_window, bg='#c2f0f0')
main_revision_game_question_frame.pack()
main_revision_game_question_frame.grid_propagate(1)
main_revision_game_question_frame.config(width=screen_width, height=40)
separator = ttk.Separator(main_revision_game_question_frame, orient=HORIZONTAL)
separator.grid(column=0, row=0, sticky='ew')
question_label = Label(main_revision_game_question_frame,
text=' Placeholder text for a question goes here!
\n######################################################',
bg='#c2f0f0', fg='#ff2824', font='"Open Sans" 26 bold')
question_label.grid(column=0, row=1, sticky='ew')
main_revision_game_question_frame.grid_columnconfigure(0,weight=1)
main_revision_game_question_frame.grid_rowconfigure(0, weight=1)
This is the code I've ended up with upon trying to combine a few solutions, and this is how it looks:
You can see the separator is there, but it does not stretch across the frame. Thank you for reading.
The separator is expanding to fill the frame, but the frame hasn't been configured to fill the space it has been given.
You need to expand the frame. One way to do that is to use the fill option of pack:
main_revision_game_question_frame.pack(fill="x")
Try to use
main_revision_game_question_frame.pack(fill="both")
so the frame fills the whole space it is given

How to fix the Entry box when adjusting height in tkinter?

So I have a issue for where I type it will be right at the middle which I want to start it at the beginning of the box.
entry_notfor = Entry(root, textvariable=number_notoffer, width=30, bg="#1f1e1e", fg="#ffffff")
entry_notfor.place(x=5, y=390, height=100)
this is what it looks like now:
There's nothing you can do if you make the entry widget taller than it wants to be. An Entry widget is designed to be only one line tall. If you stretch it to be unnaturally tall, the text will always only appear centered vertically.
If your intent is to support multiline input then you should use a Text widget rather than an Entry.

Color lines seperately in tkinter?

I'm trying to write a music program that would display Chordpro files in python. Similar to this image, I want the chords, comments, and lyrics to each have different colours. I've tried these widgets:
I tried separating chords, comments and lyrics into multiple strings that could overlap on a canvas (with a different colour for each string) to make the full song, but sadly whitespace overrides previously rendered text, so I could only see the last layer.
Label/Message doesn't have functionality for multiple colours unless make a label for each line, which is very tedious, considering I want the font size to be adjustable too.
Text is editable, which I don't want.
Is there some kind of module or other tkinter widget that would allow separately coloured lines?
Just draw the separate lines at different y positions (heights) on the canvas. It's the first two parameters of the create_text() function. E.g.
self.canvas = Canvas(root, width=800, height=650, bg = '#afeeee')
self.canvas.create_text(100,10,fill="darkblue",font="Times 20 italic bold",
text="Greensleeves are my...")
So here, change 10 to the line position you want etc. Code copied from Python: how to add text inside a canvas?

How to write into a python tkinter text widget

I have made a text widget in my gui but cannot figure out how to write to it on certain specified lines. Is it possible to make it always have some words in one place when the program is started and then to have other numbers associated with variables entered in other places.
txtReciept = Text(root)
txtReciept = Text(root, width=76, height=50)
txtReciept.pack()
The text widget cannot be written to sparsely. That is, if you want to write to row 10, you must have data on rows 1-9. Likewise, if you want to display something at a specific column, you must have data in all of the columns leading up to it.

Categories