Tuesday, April 3, 2012

Python Tkinter: How to set the window size without using canvas

Reference: http://stackoverflow.com/q/9996599/1276534
Credit: George, Bryan Oakley

Here is the code on how to set the window size without using canvas, it is great if you just starting, or do not want to use canvas to do this. You can specify your dimension in your frame, and then use pack_progate(0) flag to tell tkinter to use your size.
import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root, width=400, height=400)
frame.pack_propagate(0) # set the flag to use the size
frame.pack() # remember to pack it or else it will not be pack

textBox = tk.Label(frame, text="(x,y): ")
textBox.pack()

root.mainloop()
Note: If your frame is not big enough to how the items, it will expand to fit your items). And the pack_progate(0) is a flag, it does not replace pack() method, you still have to call it or otherwise it will not appear. Hope this help.

Reference: stackoverflow 1, stackoverflow 2

No comments:

Post a Comment