I’m trying to add to my application a couple of buttons with an image and text. By default all buttons have hidden text by setting:
compound=tk.NONE
When one specific button is pushed – compound is switched to any other value, for example
compound=tk.LEFT
Then button height change can be observed. I can’t understand why. Maybe it is normal behavior or what is more probable – maybe it’s my fault and some additional/different setups are needed.
I read that when a button has specified image and text then its size input values are processed as pixels not as a font character size. In my case its true, checked that a font size doesn’t impact button height.
I have prepared simple application that present this problem:
import tkinter as tk
class psWindow():
def __init__(self, parent) -> None:
self.parent = parent
#Main window geometry
self.setParentGeometry()
#Parent grid weight
self.parent.grid_columnconfigure(0,weight=1)
self.parent.grid_rowconfigure(0,weight=1)
#Frame 1, spread to parent size
self.frame1 = tk.Frame(parent,height=50,
width=50,background='black',borderwidth=0, highlightthickness=0)
self.frame1.grid(column=0,row=0,sticky=tk.NSEW)
#Frame 1 grid weight
self.frame1.grid_columnconfigure(0,weight=1)
self.frame1.grid_rowconfigure(0,weight=1)
self.frame1.grid_rowconfigure(1,weight=1)
#Some button global properties
self.buttonWidth = 50
self.fontList1 = ['Calibri', '12']
#Dummy image, 1x1 pixel
self.dummyImgB1 = tk.PhotoImage(width=1,height=1)
#Button 1
self.b1 = tk.Button(self.frame1, borderwidth=0, highlightthickness=0,
text='Button height changes', font=self.fontList1,
background='orange', width=self.buttonWidth,
height=40, image=self.dummyImgB1, compound=tk.NONE,
command=self.unwrapMenu)
self.b1.grid(column=0,row=0,sticky=tk.NSEW)
#Button 2
self.b2 = tk.Button(self.frame1, borderwidth=0, highlightthickness=0,
text='No text', font=self.fontList1,
background='lime', width=self.buttonWidth,
height=60, image=self.dummyImgB1, compound=tk.NONE)
self.b2.grid(column=0,row=1,sticky=tk.NSEW)
def setParentGeometry(self) -> None:
self.parent.geometry('200x100+200+200')
self.parent.maxsize(200,100)
self.parent.minsize(200,100)
self.parent.update()
def unwrapMenu(self) -> None:
#Switch command, set nonzero compound value to show text
self.b1.configure(command=self.wrapMenu,
compound=tk.LEFT)
#Get size of button 1 and print it
x = self.b1.winfo_width()
y = self.b1.winfo_height()
s = 'Button 1 (wrapped) size: x=' + repr(x) + ', y=' + repr(y)
print(s)
#Get size of button 2 and print it
x = self.b2.winfo_width()
y = self.b2.winfo_height()
s = 'Button 2 (wrapped) size: x=' + repr(x) + ', y=' + repr(y)
print(s)
def wrapMenu(self) -> None:
#Switch command, set compound value to none to hide text
self.b1.configure(command=self.unwrapMenu,
compound=tk.NONE)
#Get size of button 1 and print it
x = self.b1.winfo_width()
y = self.b1.winfo_height()
s = 'Button 1 (unwrapped) size: x=' + repr(x) + ', y=' + repr(y)
print(s)
#Get size of button 2 and print it
x = self.b2.winfo_width()
y = self.b2.winfo_height()
s = 'Button 2 (unwrapped) size: x=' + repr(x) + ', y=' + repr(y)
print(s)
# create main window
window = tk.Tk()
app = psWindow(window)
window.mainloop()
200×100 app window with two buttons, first with 40 pix height, second with 60 pix height. Both have an image (1×1 pixel size) and text (by default hidden). When orange button is clicked then its text is showed and its height changes.
Button text hidden
Button text showed
Printed output:
Button 1 (wrapped) size: x=200, y=40
Button 2 (wrapped) size: x=200, y=60
Button 1 (unwrapped) size: x=200, y=44
Button 2 (unwrapped) size: x=200, y=56
So after text is enabled by setting compound option to tk.LEFT, button height gains additional 4 pixels in aid of second one.
What I am doing wrong? Thank you for your valuable input!
MacSell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.