Can I adjust the size of message box created by tkMessagebox?

Question:

I want to create information dialogue with tkMessagebox with a fixed width. I didn’t see any options in the tkMessagebox.showinfo function that can handle this. Is there any way? Thanks!

Asked By: Fxyang

||

Answers:

As far as I’m aware you can’t resize the tkMessageBox, but if you’re willing to put in the effort you can create custom dialogs.

This little script demonstrates it:

from tkinter import * #If you get an error here, try Tkinter not tkinter

def Dialog1Display():
    Dialog1 = Toplevel(height=100, width=100) #Here

def Dialog2Display():
    Dialog2 = Toplevel(height=1000, width=1000) #Here

master=Tk()

Button1 = Button(master, text="Small", command=Dialog1Display)
Button2 = Button(master, text="Big", command=Dialog2Display)

Button1.pack()
Button2.pack()
master.mainloop()

When you run the script you should see a master window appear, with two buttons, upon pressing one of the buttons you’ll create a TopLevel window which can be resized as is shown in the script designated by#Here.
These top level windows act just like standard windows and can be resized and have child widgets.
Also if you’re trying to pack or grid child widgets into the TopLevel window then you’ll need to use .geometry not -width or -height, this would go something like this:

from tkinter import *

def Dialog1Display():
    Dialog1 = Toplevel()
    Dialog1.geometry("100x100")

def Dialog2Display():
    Dialog2 = Toplevel()
    Dialog2.geometry("1000x1000")

master=Tk()

Button1 = Button(master, text="Small", command=Dialog1Display)
Button2 = Button(master, text="Big", command=Dialog2Display)

Button1.pack()
Button2.pack()
master.mainloop()

Hope I helped, try reading up on the TopLevel widget here:
https://dafarry.github.io/tkinterbook/toplevel.htm

Answered By: Ethan Field

.option_add may only work for linux operating systems, but you can control font, where the lines wrap, and the width of the box:

    root.option_add('*Dialog.msg.font', 'Helvetica 24')
    root.master.option_add('*Dialog.msg.width', 34)
    root.master.option_add("*Dialog.msg.wrapLength", "6i")

(where “6i” is the length of the line in inches)

Answered By: Ben George
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.