Simple tk toplevel window


/ Published in: Python
Save to your folder(s)

with focus() and center() methods


Copy this code and paste it in your HTML
  1. try:
  2. from Tkinter import *
  3. except:
  4. from tkinter import * #py3k
  5.  
  6. class tkWindow(Toplevel):
  7. def __init__(self,name,**k):
  8. Toplevel.__init__(self,border=4,**k)
  9. self.master.withdraw() # hide real tk root
  10. self.title(name)
  11.  
  12. def run(self):
  13. self.center()
  14. self.focus()
  15. self.wait_window()
  16.  
  17. def focus(self):
  18. self.grab_set()
  19. self.focus_set()
  20.  
  21. def center(self):
  22. self.update_idletasks()
  23. w= self["width"]!=0 and self["width"] or self.winfo_width()
  24. h= self["height"]!=0 and self["height"] or self.winfo_height()
  25. ws,hs = self.winfo_screenwidth(),self.winfo_screenheight()
  26. self.geometry('%dx%d+%d+%d' % (w, h, (ws/2) - (w/2), (hs/2) - (h/2)))

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.