3

I have been looking all over the web trying to find an a way to size a TopLevel() window in tkinter. I read the documentation and have been looking at effbot.org's tkinterbook. I see the options but I just can't seem to get them to work.

def test():
  top = Toplevel()
  top.title("test")

  msg = Message(top, text="test")
  msg.pack(pady=10, padx=10)


def test1():
  top = Toplevel()
  top.title("test1")

  msg = Message(top, text="test1")
  msg.pack(pady=10, padx=10)

I know that there are height and width options and there is also root.geometry. However I just cant seem to get anything with TopLevel to work...

Also, is the best way to create a new window through a definition and toplevel? All I am wanting is a pop-up window with some formatted text in it.

ali_m
  • 71,714
  • 23
  • 223
  • 298
Stagnent
  • 71
  • 1
  • 2
  • 9
  • If all you want is a pop-up window with some text, why not just let `tkinter` automatically set the size? It'll adjust the window's size to fit its contents. – TigerhawkT3 Apr 02 '15 at 23:29
  • It just doesn't look right to me. I just want to resize it would top level be the best way of creating a new window if I wanted bigger windows? – Stagnent Apr 02 '15 at 23:31
  • If you hard-code the size it will break if you have different fonts or different resolution – stark Apr 02 '15 at 23:41

1 Answers1

7

The following creates a root window then sets its size to 400x400, then creates a Toplevel widget and sets its size to 500x100:

>>> import tkinter as tk
>>> root = tk.Tk()
>>> root.geometry('400x400')
''
>>> top = tk.Toplevel()
>>> top.geometry('500x100')
''

Another option is to use a container widget, like a Canvas or Frame, of your desired size into the window you'd like to work with. Then you can place other objects into this container:

>>> top2 = tk.Toplevel()
>>> frame = tk.Frame(top2, width=100, height=100)
>>> frame.pack()

When you pack() the frame, top2 is automatically adjusted to fit frame's size.

Using geometry() to set a size of AxB results in the same size window as placing an AxB widget into a dynamically-sized window - that is, if you put in a widget (or use geometry() to set the window size to) the width of your screen, the window's borders will be just outside the visible area of your screen.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97