Does anybody know if it's possible to put two lines of text in a single row using grid in TKinter?
If I make the font small enough, can I distribute the text in two lines?
>>> import Tkinter as tk
>>> root = tk.Tk()
>>> tk.Label(master=root, text="Line1\nLine2").grid(row=0)
>>> root.mainloop()
Worked for me and produced an image like this:
You can put multiple items in one cell but it is highly unusual, may have surprising behavior, and there are better ways to accomplish the same effect.
For example, the grid is invisible so you can have as many rows as you want to achieve any look you can imagine. Also, the definition of "item" is pretty loose -- you can create a frame, and in that frame put two labels, and that frame can go in a single row using grid to give the appearance of two lines of text in a single grid row. You can also use a text widget which lets you put as many lines of text that you want.
Related
I have created a window using tkinter, this window contains a grid of Labels and Entries. In the Entries I have edited some text that I want to save back to the source.
Everything I'm reading so far says that I need to create a separate list and save each entry text variable in the list.
But isn't there a better way to get the values directly from the controls themselves? I know I can loop over mywindow.winfo_children or mywindow.children. So if I can do this, then I should be able to get the text values directly, no?
I just don't know which property to get the value from.
Any ideas out there?
This is the answer.
for child in context.grid_slaves():
if(type(child) is label):
print (child['text'])
if(type(child) is entry):
print(child.get())
I can also find out where I am in the grid like this: child.grid_info() and so I can synch back to the source.
Just to elaborate on the answer.
Loop through the window grid items using the grid_slaves method.
Get the row and column using the widget's grid_info method:
for child in window.grid_slaves():
g_info = child.grid_info()
if type(child) is Button:
row = g_info['row']
col = g_info['column']
text = child['text']
print(row, col, text)
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?
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="")
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.
I have an array, of unknown length, of key:val pairs. Each pair occupies a row in a FlexGridSizer. The keys are in the first column, as wx.StaticTexts, and the vals are in the second column, as wx.TextCtrls.
The problem is that there isn't a lot of room available, and some of the vals are relatively long, and don't fit in the wx.TextCtrls. I would like to have all of the wx.TextCtrls be maybe 2 or 3 lines in height.
I've tried using style = wx.TE_MULTILINE, but that just adds a vertical scrollbar, as opposed to the default behaviour of scrolling horizontally with left/right/home/end etc.
Any ideas?
I suggest you use wxGrid.
http://docs.wxwidgets.org/2.9.2/overview_grid.html
According to the documentation for the wx.TextCtrl, you can apply the wx.HSCROLL style to it to make the control have a horizontal scrollbar, but this won't work on GTK1-based systems: http://xoomer.virgilio.it/infinity77/wxPython/Widgets/wx.TextCtrl.html
There's also an ExpandoTextCtrl that you might want to look at: from wx.lib.expando import ExpandoTextCtrl (see the wxPython demo for an example)
I ended up using FlexGridSizer. I made each of the val cells span across two rows, and added empty wx.Size()s below each key. The result is something like this: