Thursday, 15 January 2015

python - How to find out the required window size? -



python - How to find out the required window size? -

i have window containing label w text can of given variety (in case "hello world! " once, twice , thrice):

from tkinter import * import time root = tk() text = "hello, world! " w = label(root) in range(1, 4): w["text"] = text*i w.update_idletasks() w.grid() w.update_idletasks() time.sleep(1) root.mainloop()

i set size of window fixed width. width should 1 required longest text w can get. how can easily? have cycle through possible texts, read respective window width , set final width maximum of these values? if best way, how can without window appearing on screen?

how can know size of largest text if don't cycle through them ? possibility knowing size of each 1 earlier, have solved problem already. hence have set label , check required width window, check whether width larger current maximum , update if needed. if window happens show during process, can phone call withdraw, need, , phone call deiconify.

import time import random import tkinter root = tkinter.tk() root.withdraw() text = "hello, world! " w = tkinter.label() w.grid() maxwidth = 0 _ in range(10): = random.randint(1, 5) w["text"] = text*i print w.update_idletasks() maxwidth = max(maxwidth, root.winfo_reqwidth()) root.wm_minsize(maxwidth, root.winfo_reqheight()) root.wm_maxsize(maxwidth, int(1e6)) root.deiconify() root.mainloop()

python tkinter

No comments:

Post a Comment