4

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!

Fxyang
  • 293
  • 3
  • 6
  • 12

2 Answers2

4

.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)

Ben George
  • 76
  • 4
2

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

Jack Griffin
  • 1,228
  • 10
  • 17
Ethan Field
  • 4,646
  • 3
  • 22
  • 43
  • Thanks. This helps. Two small problems in your code: 1. For me it's Tkinter, not tkinter. Don't know if there is any distribution that uses tkinter though. 2. The code need master.mainloop() to work. Better edit it for future readers. – Fxyang Feb 10 '15 at 07:18
  • The version that I'm running uses tkinter not Tkinter, but I'm aware that different versions of tkinter and python use different module definitions, and I've modified it to include .mainloop() – Ethan Field Feb 11 '15 at 11:23
  • Also you should probably change the title of the question to be "size" not "side" – Ethan Field Feb 11 '15 at 11:30
  • I see. I haven't switch to python 3 yet. BTW, while trying TopLevel. I found that any height or width will be overwritten if I pack something into it. Only geometry works. Is this true? And I still have no idea how to have a message dialog with fixed width whereas the height is adjusted by the amount of message. Do you know how to do this? Thanks! – Fxyang Feb 12 '15 at 16:10
  • Yeah, if you pack or grid something in then tkinter ignores -width and-height parameters. So using .geometry is the easiest way I've found. In terms of fixing the window size in certain axis it acts much like a normal window. This should help you: http://stackoverflow.com/questions/21958534/setting-the-window-to-a-fixed-size-with-tkinter – Ethan Field Feb 13 '15 at 11:51
  • Edited the link to tkinterbook. effbot.org has expired. – Jack Griffin Feb 18 '23 at 10:28