How to stop Tkinter Message widget from resizing?
I create three "Message Widgets" but they seem to change in width and height according to the content inside, is it possible to prevent this from happening?
from tkinter import *
root = Tk()
root.configure(background = 'Yellow')
root.geometry('500x400')
a = '{}'.format('The Elepthan is Big')
b = '{}'.format('The Bird')
c = '{}'.format('The Lion is big and ferocious, kills any animal')
msg = Message(root, text = a, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack()
msg = Message(root, text = b, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack()
msg = Message(root, text = c, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack()
root.mainloop()
source to share
UPDATE:
You have several options, but as per your comment, widgets resize according to their content, I want them all with same width
I will provide an example that best suits your needs with a geometry manager pack()
.
for a package, at least the simplest option is to do pack(fill = BOTH)
The fastest way to do what you want is to use a frame. The frame will resize to the largest text, and use pack(fill = BOTH)
on all of your post's widgets will increase the smaller dimensions to the size of the frame, which is also the size of the largest widget.
from tkinter import *
root = Tk()
root.configure(background = 'Yellow')
root.geometry('500x400')
a = '{}'.format('The Elepthan is Big')
b = '{}'.format('The Bird')
c = '{}'.format('The Lion is big and ferocious, kills any animal')
frame = Frame(root)
frame.pack()
msg = Message(frame, text = a, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack(fill=BOTH)
msg = Message(frame, text = b, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack(fill=BOTH)
msg = Message(frame, text = c, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack(fill=BOTH)
root.mainloop()
source to share
You can do this by finding the width of the longest post and then centering all of them within that length using the Message
widget option padx
. Here's an example:
from tkinter import *
import tkinter.font as tkFont
root = Tk()
root.configure(background='Yellow')
root.geometry('500x400')
a = 'The Elepthan is Big'
b = 'The Bird'
c = 'The Lion is big and ferocious, kills any animal'
messages = a, b, c
msgfont = tkFont.Font(family='times', size=9)
maxwidth = max(msgfont.measure(text) for text in messages) # get pixel width of longest one
for text in messages:
msg = Message(root, text=text, width=maxwidth, justify='left')
padx = (maxwidth-msgfont.measure(text)) // 2 # padding needed to center text
msg.config(bg='lightgreen', relief=RIDGE, font=msgfont, padx=padx, pady=-2, borderwidth=3)
msg.pack()
root.mainloop()
Results:
source to share