7

In python, I am attempting the change the width of the tkinter messagebox window so that text can fit on one line.

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
messagebox.showinfo("info","this information goes beyond the width of the messagebox")
root.mainloop()
btobers
  • 175
  • 1
  • 1
  • 7

4 Answers4

4

It's not possible to adjust the size of messagebox.

When to use the Message Widget

The widget can be used to display short text messages, using a single font. You can often use a plain Label instead. If you need to display text in multiple fonts, use a Text widget. -effbot

Also see:

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
3

@CharleyPathak is correct. You either need to put a newline in the middle of the text, because message boxes can display multiple lines, or create a custom dialog box.

Legorooj
  • 2,646
  • 2
  • 15
  • 35
1

Heres another method that gets the effect youre looking for but doesnt use messagebox. it looks a lot longer but it just offers much more in terms of customization.

def popupmsg():
    popup = tk.Tk()

    def leavemini():
        popup.destroy()

    popup.wm_title("Coming Soon")
    popup.wm_attributes('-topmost', True)     # keeps popup above everything until closed.
    popup.wm_attributes("-fullscreen", True)  # I chose to make mine fullscreen with transparent effects.
    popup.configure(background='#4a4a4a')     # this is outter background colour
    popup.wm_attributes("-alpha", 0.95)       # level of transparency
    popup.config(bd=2, relief=FLAT)           # tk style

    # this next label (tk.button) is the text field holding your message. i put it in a tk.button so the sizing matched the "close" button
    # also want to note that my button is very big due to it being used on a touch screen application.

    label = tk.Button(popup, text="""PUT MESSAGE HERE""", background="#3e3e3e", font=headerfont,
                      width=30, height=11, relief=FLAT, state=DISABLED, disabledforeground="#3dcc8e")
    label.pack(pady=18)
    close_button = tk.Button(popup, text="Close", font=headerfont, command=leavemini, width=30, height=6,
                             background="#4a4a4a", relief=GROOVE, activebackground="#323232", foreground="#3dcc8e",
                             activeforeground="#0f8954")
    close_button.pack()
0

I managed to have a proper size for my "tkMessageBox.showinfo(title="Help", message = str(readme))" this way:

I wanted to show a help file (readme.txt).

def helpfile(filetype):
    if filetype==1:
        with open("readme.txt") as f:
            readme = f.read()
            tkMessageBox.showinfo(title="Help", message = str(readme))

I opened the file readme.txt and EDITED IT so that the length of all lines did not exeed about 65 chars. That worked well for me. I think it is important NOT TO HAVE LONG LINES which include CR/LF in between. So format the txt file properly.