(windows 7, python 2.7.3)
Here is my code:
from Tkinter import *
root = Tk()
root.geometry('400x400')
Frame(root, width=20, height=20, bg='red').pack(expand=NO, fill=None, side=LEFT)
Label(root, width=20, height=20, bg='black').pack(expand=NO, fill=None, side=LEFT)
root.mainloop()
And the result is like this:
I set same width and height to the Frame and Label, but they show different size. What's more, the Label is even not a square.Please explain it for me, and show me the way to make them same size.
Short answer:
20 is the same as 20, but 20 meters is not the same as 20 kilometers.
Long answer:
The result you got is not as weird as you may think because the width and height options of Tkinter.Frame() are measured in terms of pixels whereas in Tkinter.Label():
width: defines the width of the label in characters
height: defines the height of the label in lines
Reference.
As I know Label is used for text. Label() definition and Frame() might work differently for width and height parameters, correct me if am wrong.
example:
change width and height inside Label() to 1. you will see space for one character filled with black color in tk window.
like
Label(root, width=1, height=1, bg='black').pack(expand=NO, fill=None, side=LEFT)
Related
I am looking to set the Label Widget width to an exact value - width=100 should be 100 pixels.
Is there a way to achieve this or should I be looking at using a different widget?
When using the TKinter Label, the width and height parameters refer to the text size - height=2 will set the label large enough for two lines of text, not 2 pixels as I would expect.
I believe this post might help with your issue if you absolutely need a solution to do sizes pixel-perfect on your widget.
There is however no easy way to do it straight on the widget itself.
Specify the dimensions of a Tkinter text box in pixels
You can assign a blank image to the label, then you can specify width option in pixels:
import tkinter as tk
root = tk.Tk()
blank = tk.PhotoImage()
tk.Label(root, image=blank, text="Hello", width=200, height=50, compound="c", bg="yellow", bd=0, padx=0).pack(padx=100, pady=10)
tk.Label(root, image=blank, text="World", width=200, height=30, compound="c", bg="cyan", bd=0, padx=0).pack(padx=100, pady=10)
root.mainloop()
Result:
I am using Tkinter to code a GUI and I am running into a strange issue. I have specified my Root widget to be about 1/3rd of the screen width. However, when I create a Text widget within the root and specify its width to be the same value, it somehow takes more space than the available screen width. I know this because I resized the window and checked and also because title.winfo_reqscreen_width() is outputting 4130. That does not make sense since my screen resolution is 1440x900.
Here is the code I am using:
from tkinter import *
root = Tk()
app_width = int(root.winfo_screenwidth()/3.5)
root.geometry(f"{app_width}x{app_height}+0+0")
root.resizable(False, False)
root.pack_propagate(0)
title = Text(root, height=1, width=app_width, padx=10, pady=10, font=('NewComputerModern08', 18))
title.insert('1.0', 'Title')
title.bind("<Leave>", leave)
#title.grid(column=0, row=0, padx=5, pady=5)
title.pack()
I have also tried variations with pack_propagate(), grid(), fill=X, fill=BOTH, expand=True, and not adding a width argument to the widget etc. However, I am still facing the same issue.
The width parameter of a Text widget (and Entry and Label) is used to specify the width in characters not pixels. 1440/3.5 is 380, so you're requesting that the window be 380 characters wide. On my machine the default font is 7 pixels or so, which would result in a window of around 2,660 pixels.
I have a music player that displays the song name in a Label, which is in a grid. My entire window resizes if a song is long enough, but it doesn't need to. Is there a way to either make a row that has one column in it, or a way to just make the window stop resizing?
self.search_results = tk.Label(master, text=self.query, bg="#eeac41", fg="#000000")
self.search_results.grid(row=6, column=1)
You can make use of width and height if you want static size
self.search_results = tk.Label(master, text=self.query, height = 10,width = 40 bg="#eeac41", fg="#000000")
Change the values of height and width as per your needs. You can specify only either of these if you want.
# If you use padx and pady inside the label to set the size. For example :
self.search_results = tk.Label(master, text=self.query, bg="#eeac41", fg="#000000",
padx= 35, pady=25)
I'm working on a little text editor and currently making the buttons, on the picture bellow the 'B' button for bold. I would like to reduce his size in order to make him fit his true size, like a square box, and get rid of the extra-height.
How could I go below this size that seems to be the default minimum? Both the frame and button height are set to 1 and expecting an integer, so I'm not allowed to go below with something like 0.5.
top_menu = tk.Frame(root)
bold_button = tk.Button(top_menu, text='B', font=('EB Garamond ExtraBold',)) #here a tuple because tkinter needs it when the font name have multiple spaces
bold_button.pack(side='left', padx=4)
The width and height attributes are in units of characters (eg: 1 means the size of one average character). You can specify the width and height in units of pixels if the button has an image.
A button can have both an image and text, so a simple trick is to add a one-pixel transparent pixel as an image, which will then allow you to specify the size in pixels.
The padx and pady options also contributes to the size of the button. If you are trying to get a precise size, you need to set those to zero.
Here is a simple example:
import tkinter as tk
root = tk.Tk()
root.geometry("200x100")
pixel = tk.PhotoImage(width=1, height=1)
toolbar = tk.Frame(root)
toolbar.pack(side="top")
for size in (12, 16, 24, 32):
button = tk.Button(toolbar, text=str(size), width=size, height=size,
image=pixel, compound="center", padx=0, pady=0)
button.pack(side="left")
root.mainloop()
I think you could try to use a ttk.Button and give it a ttk.Style and set the font size:
s = ttk.Style()
s.configure('my_button', font=('Helvetica', 12))
b = ttk.Button(..., style='my_button')
Configuring a button (or any widget) in Tkinter is done by calling a configure method "config"
To change the size of a button called button1 you simple call
button1.config( height = WHATEVER, width = WHATEVER2 )
If you know what size you want at initialization these options can be added to the constructor.
button1 = Button(self, text = "Send", command = self.response1, height = 100, width = 100)
is there a way to set a width and height on a labelframe in tkinter, because if i enter a specific height and width when i create the labelframe, effectively, it take the correct value, but only if the frame is empty.
Else if I place something in, the label frame take the height and the width of the object with margin.
serverlabel = tk.LabelFrame(root, text="Servers",bg="#11ba71",relief="solid", width=320, height=430,padx=5,pady=5)
serverlabel.place(x=320,y=0)
serverlabel is empty, and it have the good size :
optionlabel = tk.LabelFrame(root, text="Options",bg="#11ba71",relief="solid",width=200, height=50,padx=5,pady=5)
optionlabel.grid(column=0, row=3, sticky=tk.W)
settingsbutton = tk.Button(optionlabel, text="Paramètre", command=parameter)
settingsbutton.pack()
...
but here, optionlabel is only 112x107px instead of 200x50 :
Could you help me please ?