Resize entry box in tkinter - python

The following MWE is for a window with horizontal and vertical scrollbars. The window contains an entry box in which the current working directory is displayed. However, the text in the entry box cannot all be seen as the box is too small. I would like to be able to display more of this text as the user enlarges the window. How can I adapt the following example so that the Entry box (defined in UserFileInput) resizes with the window? I have tried using window.grid_columnconfigure (see below), however this doesn't have any effect. It seems to be a problem with using the canvas, as previously I was able to get the Entry boxes to resize, however I need the canvas in order to place the horizontal and vertical scrollbars on the window.
window.grid(row=0, column=0, sticky='ew')
window.grid_columnconfigure(0, weight=1)
(and also with column = 1) but this doesn't have an effect.
import Tkinter as tk
import tkFileDialog
import os
class AutoScrollbar(tk.Scrollbar):
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
# grid_remove is currently missing from Tkinter!
self.tk.call("grid", "remove", self)
else:
self.grid()
tk.Scrollbar.set(self, lo, hi)
class Window(tk.Frame):
def UserFileInput(self,status,name):
row = self.row
optionLabel = tk.Label(self)
optionLabel.grid(row=row, column=0, sticky='w')
optionLabel["text"] = name
text = status#str(dirname) if dirname else status
var = tk.StringVar(root)
var.set(text)
w = tk.Entry(self, textvariable= var)
w.grid(row=row, column=1, sticky='ew')
w.grid_columnconfigure(1,weight=1)
self.row += 1
return w, var
def __init__(self,parent):
tk.Frame.__init__(self,parent)
self.row = 0
currentDirectory = os.getcwd()
directory,var = self.UserFileInput(currentDirectory, "Directory")
if __name__ == "__main__":
root = tk.Tk()
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0,weight=1)
vscrollbar = AutoScrollbar(root,orient=tk.VERTICAL)
vscrollbar.grid(row=0, column=1, sticky='ns')
hscrollbar = AutoScrollbar(root, orient=tk.HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky='ew')
canvas=tk.Canvas(root,yscrollcommand=vscrollbar.set,xscrollcommand=hscrollbar.set)
canvas.grid(row=0, column=0, sticky='nsew')
vscrollbar.config(command=canvas.yview)
hscrollbar.config(command=canvas.xview)
window = Window(canvas)
canvas.create_window(0, 0, anchor=tk.NW, window=window)
window.update_idletasks()
canvas.config(scrollregion=canvas.bbox("all"))
root.mainloop()

You have several problems in your code that are getting in your way. The biggest obstacle is that you're putting a frame inside a canvas. That's rarely necessary, and it makes your code more complex than it needs to be? Is there a reason you're using a canvas, and is there a reason you're using classes for part of your code but not for everything?
Regardless, you have two problems:
the frame isn't growing when the window grows, so the contents inside the window can't grow
the label won't grow because you're using grid_columnconfigure incorrectly.
Hints for visualizing the problem
When trying to solve layout problems, a really helpful technique is to temporarily give each containing widget a unique color so you can see where each widget is. For example, coloring the canvas pink and the Window frame blue will make it clear that the Window frame is also not resizing.
Resizing the frame
Because you're choosing to embed your widget in a canvas, you are going to have to manually adjust the width of the frame when the containing canvas changes size. You can do that by setting a binding on the canvas to call a function whenever it resizes. The event you use for this is <Configure>. Note: the configure binding fires for more than just size changes, but you can safely ignore that fact.
The function needs to compute the width of the canvas, and thus the desired width of the frame (minus any padding you want). You'll then need to configure the frame to have that width. To facilitate that, you'll need to either keep a reference to the canvas id of the frame, or give the frame a unique tag.
Here is a function that assumes the frame has the tag "frame":
def on_canvas_resize(event):
padding = 8
width = canvas.winfo_width() - padding
canvas.itemconfigure("frame", width=width)
You'll need to adjust how you create the canvas item to include the tag:
canvas.create_window(..., tags=["frame"])
Finally, set a binding to fire when the widget changes size:
canvas.bind("<Configure>", on_canvas_resize)
Using grid_columnconfigure to get the label to resize
You need to use grid_columnconfigure on the containing widget. You want the columns inside the frame to grow and shrink, not the columns inside the label.
You need to change this line:
w.grid_columnconfigure(...)
to this:
self.grid_columnconfigure(...)

Related

Python tkinter window resizes, but widgets never change or move

Python beginner. I placed a scrollbar widget in window and that works, but no matter what I do I can't get the scrollbox widget to change size. Could go with a larger scrollbox or for it to resize when the window resizes, but can't figure out how to force either to happen. Tried lots of different solutions, but feels like the grid and canvas are defaulting to a size and can't figure out how to change that. Help would be appreciated. Code is below:
import tkinter as tk
from tkinter import ttk
import os
import subprocess
class Scrollable(tk.Frame):
"""
Make a frame scrollable with scrollbar on the right.
After adding or removing widgets to the scrollable frame,
call the update() method to refresh the scrollable area.
"""
def __init__(self, frame, width=16):
scrollbar = tk.Scrollbar(frame, width=width)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=True)
self.canvas = tk.Canvas(frame, yscrollcommand=scrollbar.set)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=self.canvas.yview)
self.canvas.bind('<Configure>', self.__fill_canvas)
# base class initialization
tk.Frame.__init__(self, frame)
# assign this obj (the inner frame) to the windows item of the canvas
self.windows_item = self.canvas.create_window(0,0, window=self, anchor=tk.NW)
def __fill_canvas(self, event):
"Enlarge the windows item to the canvas width"
canvas_width = event.width
self.canvas.itemconfig(self.windows_item, width = canvas_width)
def update(self):
"Update the canvas and the scrollregion"
self.update_idletasks()
self.canvas.config(scrollregion=self.canvas.bbox(self.windows_item))
root = tk.Tk()
root.title("application")
root.geometry('750x800')
dbEnvs = ['a','b']
x = 1
header = ttk.Frame(root)
body = ttk.Frame(root)
footer = ttk.Frame(root)
header.pack(side = "top")
body.pack()
footer.pack(side = "top")
#setup Environment selection
envLabel = tk.Label(header, text="Environment:")
envLabel.grid(row=0,column=0,sticky='nw')
dbselection = tk.StringVar()
scrollable_body = Scrollable(body, width=20)
x = 1
for row in range(50):
checkboxVar = tk.IntVar()
checkbox = ttk.Checkbutton(scrollable_body, text=row, variable=checkboxVar)
checkbox.var = checkboxVar # SAVE VARIABLE
checkbox.grid(row=x, column=1, sticky='w')
x += 1
scrollable_body.update()
#setup buttons on the bottom
pullBtn = tk.Button(footer, text='Pull')
pullBtn.grid(row=x, column=2, sticky='ew')
buildBtn = tk.Button(footer, text='Build')
buildBtn.grid(row=x, column=3, sticky='ew')
compBtn = tk.Button(footer, text='Compare')
compBtn.grid(row=x, column=4, sticky='ew')
root.mainloop()
have tried anchoring, changing the window base size and multiple other things (8 or 19 different items, plus reading lots of posts), but they normally involve packing and since I used grids that normally and ends with more frustration and nothing changed.
If you want the whole scrollbox to expand to fill the body frame, you must instruct pack to do that using the expand and fill options:
body.pack(side="top", fill="both", expand=True)
Another problem is that you're setting expand to True for the scrollbar. That's probably not something you want to do since it means the skinny scrollbar will be allocated more space than is needed. So, remove that attribute or set it to False.
scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=False)
tip: when debugging layout problems, the problems are easier to visualize when you temporarily give each widget a unique color. For example, set the canvas to one color, body to another, the instance of Scrollable to another, etc. This will let you see which parts are visible, which are growing or shrinking, which are inside the borders of others, etc.

Tkinter: How to resize a scroll bar if the canvas changes size?

I am working on a project where I need to display information when a button is clicked, and hide information when that button is clicked again. The information can be several rows and extend beyond the window, so I am trying to add a scroll bar to get around this issue. The problem is that when the information is displayed, the scroll bar does not show. Scrolling is still possible but the actual bar is not there. Resizing the window fixes this issue for some reason. Also when the button is clicked again to hide the information, the size of the canvas and the scrollbar remain the same, so you can scroll far beyond the button into empty space. My theory is that the scroll region is not updating when the widgets in the canvas change size - but I'm not sure how to go about fixing that.
I realize this explanation may be a bit confusing, so I have provided a simplified example below. This example has a single button that when clicked reveals several lines of "other info". The scrollbar and/or canvas does not resize properly upon interacting with this button.
My strategy right now was to have a method called add_scrollbar that removes the current scrollbar and creates a new one every time the widgets change in hopes that the new one would be the right size; however this still is not working.
from tkinter import *
from tkinter import ttk
def add_scrollbar(outer_frame, canvas):
if len(outer_frame.winfo_children()) == 2:
# canvas is at index 0 of the outer frame, if a scrollbar has been added it will be at index 1
outer_frame.winfo_children()[1].destroy()
# my strategy here was to destroy the existing scroll bar
# and create a new one each time the widget changes size
scrollbar = ttk.Scrollbar(outer_frame, orient=VERTICAL, command=canvas.yview)
scrollbar.pack(side=RIGHT, fill=Y)
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
return
def create_example():
root = Tk()
root.geometry("1200x1200")
my_outer_frame = Frame(root)
my_outer_frame.pack(fill=BOTH, expand=1)
my_canvas = Canvas(my_outer_frame)
my_canvas.pack(side=LEFT, fill=BOTH, expand=1)
inner_frame = Frame(my_canvas)
my_canvas.create_window((0, 0), window=inner_frame)
# ^^^ Sets up the ability to have a scroll_bar
changing_frame = Frame(inner_frame, borderwidth=4) # this is the frame that will be changing its contents
changing_frame.pack(side=LEFT, anchor="n")
display_frame(changing_frame, my_outer_frame, my_canvas)
# this method re-displays the changing frame depending on the specified size ('big' or 'small'
root.mainloop()
return
def display_frame(frame, outer_frame, canvas, size='small'):
for widget in frame.winfo_children():
widget.destroy()
if size == 'small':
Button(frame, height=5, width=5, text="Show",
command=lambda this_frame=frame: display_frame(this_frame, outer_frame, canvas, size='big')).grid(row=0,
column=0)
elif size == 'big':
Button(frame, height=5, width=5, text="Hide",
command=lambda this_frame=frame: display_frame(this_frame, outer_frame, canvas, size='small')).grid(
row=0, column=0)
for n in range(1, 100):
Label(frame, text="Other Stuff!").grid(row=n, column=0)
frame.pack(side=LEFT)
add_scrollbar(outer_frame, canvas) # this method is supposed to destroy the existing scrollbar and make a new one
return
if __name__ == '__main__':
create_example()

Make Frame occupy whole of its scrollable parent Canvas when it is smaller than the Canvas itself

Situation:
I have a created a scrollable frame as follows - a parent outer frame with two configured columns, canvas on column=0, scrollbar on column=1. The canvas contains the actual frame, placed in it using create_window. My case is like, there may be times when there won't be any content in the frame, and times where there is enough content (it's decided at runtime).
When there is no content, the frame shrinks to a mere 1x1 pixel size; when the content is present but not enough to scroll, the frame covers only the content. My requirement is to set the frame so that when its required size is lesser than the canvas itself, it should take the size of the canvas; ie: the frame's size shouldn't be smaller than the canvas itself.
What I've tried:
1. I have tried -
.itemconfig(<frame_itemID>, width=<the_canvas>.winfo_width(), height=<the_canvas>.winfo_height()) on the concerned canvas after I .grid()ed the outer frame onto the screen in the hopes of it setting the size for the frame, to no avail.
2. Tried binding whenever the canvas get configured -
<the_canvas>.bind("<Configure>", lambda e: <the_canvas>.itemconfig(<frame_itemID>, width=e.width, height=e.height)). The problem with this - the frame is stuck to the on-screen size of the canvas , ie: even on content overflow, it doesn't resize (seems permanently hard-bound to canvas' size).
3. Tried <inner_frame>.configure(width=<the_canvas>.winfo_width(), height=<the_canvas>.winfo_height()) after I placed the outer frame onto the screen. Expected the frame to react and resize the same way it would have if another widget was placed in it, in vain.
The first two attempts were from various suggestions on a question on this site; as I said, don't seem to work.
Code:
Following is the code for the scrolling frame; the one causing the above concerns:
class ScrollFrame(tk.Frame):
def __init__(self, master=None, **kwargs):
super().__init__(master, bd=0)
# creating and placing the widgets as required
self.canvas = tk.Canvas(self, bd=0, bg="#5599FF")
self.scrollbar = ttk.Scrollbar(self)
self.frame = tk.Frame(self.canvas, **kwargs)
# used self._frame_id to store the item_id for the frame for attempt 1 & 2 above
self._frame_id = self.canvas.create_window((0,0), window=self.frame, anchor="nw")
self.canvas.grid(row=0, column=0, sticky="nwes")
self.scrollbar.grid(row=0, column=1, sticky="nwes")
# configuring the grid
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=0, minsize=16)
# configurations to connect the canvas, frame and scrollbar
self.frame.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.configure(orient='vertical', command=self.canvas.yview)
# NOTE: Any keyword arguments provided when the class is called is sent directly to the inner frame, not the outer (parent) frame.
Required imports -
import tkinter as tk
from tkinter import ttk
Sample code to test the class -
# with no content
def no_content(master):
ScrollFrame(master, bg="#30303B", bd=0).place(x=20, y=20, width=240, height=400)
# some smaller-than-canvas content, not enough to enable scrolling
def some_content(master):
Frame = ScrollFrame(master, bg="#30303B", bd=0)
tk.Label(Frame.frame, text="Hello,\nWorld!!!", bg="#FFCCAA", font=["Calibri", 25]).pack(padx=25, pady=25)
Frame.place(x=280, y=20, width=240, height=400)
# enough content to enable scrolling
def overflow_content(master):
Frame = ScrollFrame(master, bg="#30303B", bd=0)
tk.Label(Frame.frame, text="Enough\n\ncontent\n\nto\n\nmake\n\nme\n\noverflow", bg="#FFCCAA", font=["Calibri", 30]).pack(padx=25, pady=25)
Frame.place(x=540, y=20, width=240, height=400)
root = tk.Tk()
root.geometry("800x440")
root.resizable(0, 0)
no_content(root)
some_content(root)
overflow_content(root)
root.mainloop()
Any inputs which would satiate the requirements will be of great help.
EDIT: I have made a workaround of sorts for now where the canvas has the same background as frame (with highlightborder=0) and the frame bound to reconfigure its width to be the same as canvas whenever it's dimensions are changed (on adding widgets, etc). So the frame doesn't have a height when nothing is in it and the canvas is what is seen.
But it still isn't satisfactory in that, I can't really place something like right in the centre of the visible area since the frame really depends on the inner widgets for its height. So an actual solution will be appreciated.

Scrollbar widgets won't work when laid out in a grid

When using Python3 with the tkinter library, it's happened to me several times that I've needed a scrollable frame (with both vertical and horizontal scrolling capability) in order to contain a related set of widgets. Since scrollbars aren't easily attached to frames (I hear you can attach scrollbars to canvases, but not frames), I decided to create my own ScrolledFrame class -- one that uses scrollbars with a canvas widget that displays a frame inside it.
For the most part, it works pretty well. I laid-out the scrollbars and the canvas with the .pack() method. But because they are pack()ed, the vertical widget goes down all the way to the bottom of the screen, instead of leaving a blank space. (Run my code to see what I mean.)
#!/bin/env python3
# File: scrolledframe.py
# Written by: J-L
import tkinter as tk
class ScrolledFrame(tk.Frame):
def __init__(self, parent, **kwargs):
# Create the "scaffolding" widgets:
self.outerFrame = outerFrame = tk.Frame(parent, **kwargs)
self.canvas = canvas = tk.Canvas(outerFrame)
self.vscroll = vscroll = tk.Scrollbar(outerFrame, orient='vertical')
self.hscroll = hscroll = tk.Scrollbar(outerFrame, orient='horizontal')
# Pack/grid the vscroll, hscroll, and canvas widgets into their frame:
usePack = True
if usePack: # use .pack()
vscroll.pack(side='right', fill='y')
hscroll.pack(side='bottom', fill='x')
canvas.pack(fill='both', expand=True)
else: # use .grid()
canvas.grid(row=0, column=0, sticky='nsew')
vscroll.grid(row=0, column=1, sticky='ns')
hscroll.grid(row=1, column=0, sticky='ew')
# Hook up the commands for vscroll, hscroll, and canvas widgets:
vscroll.configure(command=canvas.yview)
hscroll.configure(command=canvas.xview)
canvas.configure(yscrollcommand=vscroll.set,
xscrollcommand=hscroll.set)
# Now create this very obejct (the innerFrame) as part of the canvas:
super().__init__(canvas)
innerFrame = self
canvas.create_window((0, 0), window=innerFrame)
innerFrame.bind('<Configure>',
lambda event: canvas.configure(scrollregion=canvas.bbox('all'),
width=event.width,
height=event.height))
# Accessor methods to access the four widgets involved:
def outer_frame(self): return self.outerFrame
def vscroll(self): return self.vscroll
def hscroll(self): return self.hscroll
def inner_frame(self): return self # (included for completeness)
# When .pack(), .grid(), or .place() is called on this object,
# it should be invoked on the outerFrame, attaching that to its
# parent widget:
def pack(self, **kwargs): self.outerFrame.pack(**kwargs)
def grid(self, **kwargs): self.outerFrame.grid(**kwargs)
def place(self, **kwargs): self.outerFrame.place(**kwargs)
def doExample():
# Create the main window:
root = tk.Tk()
root.title('ScrolledFrame Example')
# Create the scrolledFrame and a quit button:
scrolledFrame = ScrolledFrame(root)
scrolledFrame.pack()
tk.Button(root, text='Quit', command=root.quit).pack(side='bottom')
# Create some labels to display inside the scrolledFrame:
for i in range(1, 30+1):
tk.Label(scrolledFrame,
text='This is the text inside label #{}.'.format(i)
).grid(row=i-1, column=0)
# Start the GUI:
root.mainloop()
if __name__ == '__main__':
doExample()
To be honest, this isn't a big problem, but I then I thought about using the .grid() layout approach, by putting each scrollbar in its own row and own column.
Around line 18 of my code, I've included this line:
usePack = True
If you change it from True to False, the scrollbars and canvas widgets will be laid-out using .grid() instead of .pack(), and then you'll be able to see what I'm talking about.
So when I use .grid() to layout the scrollbars, the space under the vertical scrollbar does indeed appear as I'd expect it to, but now none of the scrollbars work!
This seems strange to me, as I don't understand why simply changing the layout managing of the widgets should make them behave any differently.
Question 1: What am I doing wrong that prevents the scrollbars from working when they are laid-out with .grid()?
Also, I notice that, with both .pack() and .grid(), the "Quit" button will move out-of-window as soon as I resize the window to be shorter than what it started out with.
Question 2: Is there a way I can force the "Quit" button to stay on the window (when the window is resizing), at the expense of my ScrolledFrame?
Thanks in advance for all your help!
What am I doing wrong that prevents the scrollbars from working when they are laid-out with .grid()?
The problem is that you aren't telling grid what to do with extra space, and what to do when there isn't enough space. Because of that, the widgets take up exactly the amount of space that they need.
With pack you're telling it to fill all allocated space with the fill and expand options. With grid, you need to give non-zero weight to the row and column that the canvas is in.
Add these two lines and you'll get the same behavior with grid that you do with pack:
outerFrame.grid_rowconfigure(0, weight=1)
outerFrame.grid_columnconfigure(0, weight=1)
You may also want to use the fill and expand option when packing scrolledFrame, assuming you want it to completely fill the window.
scrolledFrame.pack(fill="both", expand=True)
Is there a way I can force the "Quit" button to stay on the window (when the window is resizing), at the expense of my ScrolledFrame?
Call pack on it before you call pack the other widgets. When there isn't enough room for all the widgets and tkinter simply must reduce the size of one or more widgets to get it to fit, it starts by reducing the size of the last widget that was added.

Change font size without messing with Tkinter button size

I am having trouble changing the font size of a button in Tkinter, when I attempt to do it the button also expands/contracts based on the size of the text. Is there a way I can alter the text size with the button's size anchored in place?
I ran across this while designing a tic-tac-toe application, however to save you the trouble, here is a very minimal example of the problem in practice:
import Tkinter as tk
MyWindow = tk.Tk()
MyWindow.geometry("500x550")
button = tk.Button(MyWindow,text="Hello!",width=17,height=10,font=('Helvetica', '20'))
button.grid(row=1, column=1)
MyWindow.mainloop()
The most important part here is font=('Helvetica', '15') or more specifically, the number 15. If you change that number and run this again, not only will the text be bigger/smaller, but so will the button! How do I get around this?
It's probably a really simple problem. I've just gotten started with Tkinter. Thanks in advance for any help I receive!
Typically, when you give a button a width, that width is measured in characters (ie: width=1 means the width of one average sized character). However, if the button has an image then the width specifies a size in pixels.
A button can contain both an image and text, so one strategy is to put a 1x1 pixel as an image so that you can specify the button size in pixels. When you do that and you change the font size, the button will not grow since it was given an absolute size.
Here is an example that illustrates the technique. Run the code, then click on "bigger" or "smaller" to see that the text changes size but the button does not.
import Tkinter as tk
import tkFont
def bigger():
size = font.cget("size")
font.configure(size=size+2)
def smaller():
size = font.cget("size")
size = max(2, size-2)
font.configure(size=size)
root = tk.Tk()
font = tkFont.Font(family="Helvetica", size=12)
toolbar = tk.Frame(root)
container = tk.Frame(root)
toolbar.pack(side="top", fill="x")
container.pack(side="top", fill="both", expand=True)
bigger = tk.Button(toolbar, text="Bigger", command=bigger)
smaller = tk.Button(toolbar, text="Smaller", command=smaller)
bigger.pack(side="left")
smaller.pack(side="left")
pixel = tk.PhotoImage(width=1, height=1)
for row in range(3):
container.grid_rowconfigure(row, weight=1)
for column in range(3):
container.grid_columnconfigure(column, weight=1)
button = tk.Button(container, font=font, text="x",
image=pixel, compound="center", width=20, height=20)
button.grid(row=row, column=column)
root.mainloop()
All of that being said, there is almost never a time when this is a good idea. If the user wants a larger font, the whole UI should adapt. Tkinter is really good at making that happen, to the point where it all mostly works by default.
The width of the button is defined in units of character width. In your case the button is defined to be 17 characters wide. So changing the character width by (ie changing the font size) changes the width of the button. AFAIK, the only way around that is to put the button into a Frame, because a Frame can define it's size in pixels. Here's a new kind of Button that does exactly that:
import Tkinter as tk
class Warspyking(tk.Frame):
'''A button that has it's width and height set in pixels'''
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master)
self.rowconfigure(0, minsize=kwargs.pop('height', None))
self.columnconfigure(0, minsize=kwargs.pop('width', None))
self.btn = tk.Button(self, **kwargs)
self.btn.grid(row=0, column=0, sticky="nsew")
self.config = self.btn.config
#example usage:
MyWindow = tk.Tk()
MyWindow.geometry("500x550")
from itertools import cycle
fonts = cycle((('Helvetica', '11'),('Helvetica', '15'),('Helvetica', '20')))
def chg():
button.config(font=next(fonts))
button = Warspyking(MyWindow,text="Click me!",width=200,height=100 ,font=next(fonts), command=chg)
button.grid(row=1, column=1)
MyWindow.mainloop()
EDIT: Based on what I learned from Bryan Oakley, here's a much neater implementation:
class Warspyking(tk.Button):
def __init__(self, master=None, **kwargs):
self.img = tk.PhotoImage()
tk.Button.__init__(self, master, image=self.img, compound='center', **kwargs)
I should also add that I very much agree with Bryan: Using this is probably a sign that you are doing something wrong. You should let tkinter handle sizing.
I found solution of this problem. I was trying to solve a similar problem: I want to put image on label. I set the image size equal to label size. When I have been trying to put it with command label.config(image=img) the label size grow. The image have the size I set to it, so it didn't cover label completely. I was using grid manager. All size were not entered in advanced but calculated by Tkinter. I was using grid_columnconfigure and grid_rowconfigure. The solution I found is to put this label with image (or button in Your case) to LabelFrame and set grid_propagate to False.
Code example:
MyWindow = tk.Tk()
MyWindow.geometry("500x550")
#create LabelFrame (200x200)
label = tk.LabelFrame(MyWindow, width=200, height=200)
#grid manager to set label localization
labelk.grid(row=0, column=0)
#label row and column configure: first argument is col or row id
label.grid_rowconfigure(0, weight=1)
label.grid_columnconfigure(0, weight=1)
#cancel propagation
label.grid_propagate(False)
#Create button and set it localization. You can change it font without changing size of button, but if You set too big not whole will be visible
button = t.Button(label, text="Hello!", font=('Helvetica', '20'))
#Use sticky to button took up the whole label area
button.grid(row=0, column=0, sticky='nesw')
MyWindow.mainloop()
Result for font size 40 and 20:
Example for creating button with dynamic size by grid manager:
MyWindow = tk.Tk()
MyWindow.geometry("500x550")
#Divide frame on 3x3 regions
for col in range(3):
MyWindow.grid_columnconfigure(col, weight=1)
for row in range(3):
MyWindow.grid_rowconfigure(row, weight=1)
label = tk.LabelFrame(MyWindow)
#Put label in the middle
label.grid(row=1, column=1, sticky='nesw')
label.grid_propagate(False)
label.grid_rowconfigure(0, weight=1)
label.grid_columnconfigure(0, weight=1)
button = tk.Button(label, text="Hello!", font=('Helvetica', '30'))
button.grid(row=0, column=0, sticky='nesw')
MyWindow.mainloop()
It is late answer, but maybe it will help someone.

Categories

Resources