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 ?
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 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)
I am trying this so I can place buttons in the medFrame, but they appear in the topFrame. When using topFrame, the button is against the top of the screen, which looks bad, so I figured this could be solved by using a third frame.
from tkinter import *
root = Tk()
root.title('BulletHead')
root.attributes('-fullscreen', True)
root.resizable(width = NO, height=NO)
topFrame=Frame(root)
topFrame.pack(side=TOP)
medFrame=Frame(root)
medFrame.pack()
botFrame = Frame(root)
botFrame.pack(side=BOTTOM)
botonJugar = Button(medFrame, text = 'Jugar')
botonJugar.bind("<Button-1>",jugar)
botonJugar.pack()
botonTabla = Button(medFrame, text = 'Tabla de puntajes')
botonTabla.bind("<Button-1>",tabla)
botonTabla.pack()
root.mainloop()
The reason the widgets appear at the top is because topFrame is empty. Since it has no height or width, and no children, it is only one pixel in size. If you give it a width and height or put some widgets in it, you'll see that the button is in fact in the middle frame.
Here is the image I get when I give the frames an artificial width and height, and force the frames to fill in the "x" direction (and also shrink the window down for illustrative purposes):
Here is what I changed (note that I also added color to make the frames easier to see):
topFrame = Frame(root, bg="pink", height=100)
medFrame = Frame(root, bg="bisque")
botFrame = Frame(root, bg="yellow", height=100)
topFrame.pack(side=TOP, fill="x")
medFrame.pack(fill="x")
botFrame.pack(side=BOTTOM, fill="x")
If you want the medium frame to take up all of the extra space, add the expand option and have it fill in both directions:
medFrame.pack(fill="both", expand=True)
Once you add widgets to the top and bottom frames, they will shrink in height to fit their contents, making it really easy top create tool bars and status bars.
The elements have successfully been added to the middle frame however it appears as though they are in the top frame because the topFrame and botFrame have no dimensions so they don't appear (unless you can perceive one pixel). To get some spacing for the middle frame you want to supply some dimensions to the other frames. try this to give the middle frame some spacing:
topFrame=Frame(root, height=200, width=200)
botFrame = Frame(root, height=200, width=200)
An alternative to this is to check out some other options for using the pack geometry manager with only one frame to get the desired results. Particularly the expand= option is helpful for centering objects in a window.
from tkinter import *
root = Tk()
root.title('BulletHead')
root.attributes('-fullscreen', True)
root.resizable(width = NO, height=NO)
medFrame=Frame(root)
medFrame.pack(expand=True)
botonJugar = Button(medFrame, text = 'Jugar')
botonJugar.bind("<Button-1>",jugar)
botonJugar.pack()
botonTabla = Button(medFrame, text = 'Tabla de puntajes')
botonTabla.bind("<Button-1>",tabla)
botonTabla.pack()
root.mainloop()
(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)