colors = Label(
text="Surprise",
foreground="Bisque",
background="Hot Pink",
width=150,
height=100
)
colors.pack()
Pycharm showing me to remove the attribute height it is not an option
There are a few things to note here. First: in order to make a label, you need to specify which window it should go in, so the first parameter will normally be root. Second: the height option is for the number of lines of text not the number of pixels, this setting you are trying to use will be very very big (same for width). Normally you shouldn't try to set the size of a label in this way because in most cases it will just automatically go to the correct size for the text you put inside of it. Instead, if you want a specific number of pixels, you should create a Frame widget with the specific size and put the Label widget inside of the frame widget
Related
I'm trying to make a little GUI script using tk. After computation on the basis of user input, I generate a simple string that I want to print using a listbox, but it was suffering from alignment problems.
I tried printing the output in the console at the same time to check whether this was a formatting error:
for loop :
string = foo(x)
listbox.insert(END, string)
print string
The problem is that the console is using a fixed width font but the listbox is using a variable width font. In a variable width font, characters like "i" (lowercase I) and "l" (lowercase L) take up less horizontal space that characters like "M" and "0".
If you want characters to line up in a listbox like they do in the console, you need to use a fixed width font. You can configure the font used by the listbox via the font attribute.
Tkinter provides several default fonts, the default fixed-width font is named "TkFixedFont". This default font will be approximately the same vertical height as the default variable width font that is used by other widgets. The exact font that is chosen may be different on different platforms, but is typically a variant of courier.
For example:
import Tkinter as tk
root = tk.Tk()
listbox = tk.Listbox(root, font="TkFixedFont")
If you wish to be explicit about the font family and size, you can provide that as a string, tuple, or as a font object. For example, picking a courier font of size 18 could be specified as font="Courier 18".
listbox = tk.Listbox(root, font="Courier 18")
For more information on fonts, see the TkDocs tutorial on fonts, colors and images and the section Widget Styling on effbot.
Is there a way I can get the dimensions of the contents of a Tk.Text widget. For example with a canvas I can do canvas.bbox('all') to give me the dimensions of the contents. This is crucial to my program so how is this possible with a text widget?
I want to be able to disable a scrollbar when there is not enough contents for the scrollbar to be active.
import tkinter as tk
def getDims():
print('How do I print the dimensions of the text box here')
root=tk.Tk()
text=tk.Text(root)
text.pack(fill='both')
var='How can I find the dimensions of this text inside the tkinter textbox widget in pixels ?'
text.insert('end',var)
b=tk.Button(root,text='Get Dimensions!',command=getDims)
You can use the count method to get the number of pixels between the first and last characters. This method takes two indexes, and then one or more strings representing what you want to count: chars, displaychars, displayindices, displaylines, indices, lines, xpixels, or ypixels. The return value is a tuple containing the count of each requested item.
For example, to count the pixels from the top of the first character to the bottom of the last character you could do this:
ypixels = text.count("1.0", "end", "ypixels")[0]
Another way to determine if you should show the scrollbar is to call the yview method. If it returns (0.0, 1.0), that tells you that the entire text is visible. If any portion is scrolled off of the screen, that function will return something different.
I have a 525x650 window geometry in tkinter. I want to display the list items in multiple spaces such that they cover all the space in width and move to next line as the width space is full. But instead, all items are getting printed on a single line. When I use the code to print each item in a new line, it exceed the height of the main window.
def throat_symp():
#empty_label.config(text=(throat_symplist)) #For printing on same line
empty_label.config(text=("\n".join(throat_symplist))) enter image description here #For printing on seperate line
print(throat_symplist)
You can configure your Label with wraplength=525 to tell it to word-wrap the text at the given width.
Or, instead of a Label, you can use a Text (with state=DISABLED to prevent editing); this will automatically wrap at the widget's width, and is much easier to add scrolling if the list is still too tall to fit in the window.
I am trying to create a roguelike using the Text widget.
I have figured out a few things, namely that I can set the size of the widget using width and height options and that I can find the pixel height or width of said widget. However, what I want to do is have the widget resizable (pack(expand="yes", fill="both")) but be able to refresh the displayed text on a resize. Is there a way to get the character dimensions when the widget is running without resorting to winfo_width() and math based on pixel dimensions of characters?
I've run into that exact same problem a couple times jaccarmac, and to my knowledge there is no way to find the width of a string of characters. Really the only way is to use the winfo_ commands: width, height, geometry. However, it kind of sounds like you just want to make sure that all of the text is displayed if you change the label and add more text. If that is the case, you don't have to worry about it. That should all be taken care of by the widgets themselves. If you don't see them expanding to show all of your label, that usually means one of the widgets containing that label is not set to expand (either using expand=YES with .pack, or columnconfigure(i, weight=1) for .grid).
A final thought; in the pack arguments make sure it's YES, and not "yes". That uppercase YES is not a string, but a variable name defined by Tkinter.
There is no way to automatically get the width in characters, but it's easy to calculate, assuming you're using a fixed width font. One way to do this is to use the font_measure method of a font object. Use font_measure to get the width of a '0' (or any other character for that matter; I think tk users zero internally, not that it matters with a fixed width font), then use this in your calculations.
This is an old question, but after doing some research I've found that there actually is a way to get height/width info directly without maths or playing with font widths using the Text widget's cget() method:
text_widget = tk.Text()
width_in_char = text_widget.cget('width')
height_in_char = text_wdiget.cget('height')
Since the Text widget stores its height and width configuration in characters, simply querying for those parameters will give you what you're looking for.
I try to put sixteen checkbuttons into frame, placing them into four columns like:
c1 = Tkinter.Checkbutton(group.interior(), text = 'Name', indicatoron= 1, variable = self.Checkvar_nr, command=cb)
c1.(row = 0, column = 0)
and so on up to:
c16.(row = 3, column = 3)
Everything's fine except columns vertical alignment because of the differences in the length of the text used.
How to align then horizontally?
I don't quite understand the problem, since columns must be vertically aligned since it's a grid. I think what you're saying is that the items in each column aren't aligned to a column boundary. Try using sticky='w' when adding each checkbutton to the grid. This will cause them to "stick" to the left edge of the column.
As an option, try placing each element in a non-stretchable graphic element.
The thought is that the layout manager is maximizing the use of the screen real estate. Because you want to take up more space, which is contrary to the layout manager's algorithm, you will need to find a graphic container that doesn't "change size".
Sometimes you can do this through manually editing the text string (less preferred). Other times you can use a table like structure (HTML for instance). Other times you can use a frame with defined width and height attributes. These frames are then placed inside of the columns as elements.
Note: It's been a long time since I played with Tk. I'm going by memory. Best of luck!
(edit:) Going from memory, the columns will adjust their width based on content. If there are several three character labels in the first column and five character labels in the second column, the width of the two columns will be different. (Note: This will be depended on the layout manager.) If there is a 'fixed width' option for the layout manager in question, then it should keep all column widths the same.
With layout managers that rearrange with dimensions based on content (HTML, CSS, etc), it is sometimes necessary to place the content inside "immovable" containers. Usually these are frames. The frames work as bounding boxes. This approach works when the element that needs to have a width and height does not have that feature.