Tkinter Top Level window Sizing

Stagnent picture Stagnent · Apr 3, 2015 · Viewed 9.2k times · Source

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.

Answer

TigerhawkT3 picture TigerhawkT3 · Apr 3, 2015

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.