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)
Related
How are you supposed to go around sizing all the buttons to be the same size regardless of the text you insert inside, they should all size according to the biggest one.
There is an answer to a similar question like mine already, but it is done using grid and I am using a canvas to place a background image in the window and to place the buttons.
Is it even worth the hassle to get your buttons to the same size according to text automatically, since my text will always be around the same...
I tried getting the size of the buttons using cget() but that returns 0. Where does it store its width then since it has to size itself somehow even if it does it according to text? Can access that in any way? I was thinking of using that value to adjust the value of other buttons somehow, but it turned out as a fail.
If you are wondering why am I making it into a class, idk either, wanted to try it.
I had it working by putting all the buttons in a frame and telling them to fill=x but using a frame destroys the point of using a canvas since the background can't be seen because the frame covers it. Is there a way to make the frame transparent in the canvas, that could also potentially solve my problem.
from tkinter import *
class ThreeButtonMenu():
def __init__(self, button1_text, button2_text, button3_text, image_height = 600, image_width = 500, bg_input = 'space_background.png'):
self.root = Tk()
HxW = str(image_height)+'x'+str(image_width)
self.root.geometry(HxW)
self.root.maxsize(image_height,image_width)
self.root.minsize(image_height,image_width)
self.root.title('Guess')
bg = PhotoImage(file=bg_input)
background_canvas = Canvas(self.root, width = 600, height=500)
background_canvas.pack(fill="both", expand=True)
background_canvas.create_image(0,0, image=bg, anchor='nw')
button1 = Button(self.root, text=button1_text, font = ('Lato',28))
button2 = Button(self.root, text=button2_text, font = ('Lato',28))
button3 = Button(self.root, text=button3_text, font = ('Lato',28), command = self.root.destroy)
button1_window = background_canvas.create_window(300,45, anchor=N, window=button1)
button2_window = background_canvas.create_window(300,160, anchor=N, window=button2)
button3_window = background_canvas.create_window(300,275, anchor=N, window=button3)
print(button1.cget('width'))
print(button2.cget('width'))
print(button3.cget('width'))
self.root.mainloop()
start_menu = ThreeButtonMenu('Start Game', 'Leaderboard', 'Quit')
Thank you for your answers.
You would typically do this when you use a geometry manager (pack, place, or grid).
For example, you need to call pack on each of the buttons. See example of pack below.
import tkinter as tk
root = tk.Tk()
for text in (
"Hello", "short", "All the buttons are not the same size",
"Options", "Test2", "ABC", "This button is so much larger"):
button = tk.Button(root, text=text)
button.pack(side="top", fill="x")
root.mainloop()
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 ?
I want to make entrys move while resizing. an example is this code.
i didnt want to upload the 300 line code because it does not have to do anything with the design.
as you can see when you resize it horizontaly the entry2 is moving horizontaly how do i do the same for entry1?
i tried the pack(expand=True) on entry1 but then the entry2 is placed in the position of button.
im sorry if i cant make the problem clear this is my first time asking questions about programming...
import tkinter as tk
app = tk.Tk()
app.geometry("600x350")
Canvas = tk.Canvas(height=600, width=1000, bg="#343434")
Canvas.pack(fill="both", expand=True)
Frame = tk.Frame(Canvas, height=600, width=1000, bg="#1A1A1A")
Frame.pack(fill="both", expand=True)
large_font = ('Verdana', 17)
entry1 = tk.Entry(Frame, bg="#F7F5EB", font=large_font, width=25)
entry1.place(rely=0.3, relx=0.17)
entry1.configure(foreground="gray")
entry2 = tk.Entry(Frame, bg="#F7F5EB", width=22, font=("Verdana", 11))
entry2.pack(expand=True)
entry2.configure(foreground="gray")
generate_button_font = ('Arial', 12)
Generate_button = tk.Button(
Frame, bg="#f0f0f0", font=generate_button_font, foreground="#525252")
Generate_button.place(rely=0.7, relx=0.35, relwidth=0.31, relheight=0.12)
Generate_button.config(text="Generate")
help_button_font = ("Arial", 10)
help_button = tk.Button(Frame, bg="#F7F5EB", font=help_button_font, foreground="#525252")
help_button.place(rely=0.03, relx=0.88, relwidth=0.1, relheight=0.075)
help_button.config(text="Help")
about_button_font = ("Arial", 10)
about_button = tk.Button(Frame, bg="#F7F5EB", font=help_button_font, foreground="#525252")
about_button.place(rely=0.03, relx=0.02, relwidth=0.1, relheight=0.075)
about_button.config(text="About")
app.mainloop()
You can use the .place() geometry manager.
Read more about it here.
Place has 3 main paramaters which I think you need.
The first is relx. This places any widget at a certain position in a widget, relative to the widgets x legnth.
For example, if the relx = 0.5, then no matter how much you resize the widget, the widget stays in the middle.
The second parameter is rely, which works the same as relx, except it applies to the y axis.
The third paramater is anchor. Anchor basically tells the program which part of the widget should go to the specified coordinate. The default is "nw" or top left. You can put it as center, or anything else.
Hopefully this helps!!
(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)