By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Positioning frames on screen

P: 4
Hi there,

I wrote a code that chains multiple frames containing widgets on screen with TkInter, and almost everything is fine except:
the windows created with TkInter seem to be randomly placed by Windows. I would rather have them always popping at the same location on screen, but I really don't know how to do that...

Someone may have the answer :)
Thanks in advance
Oct 17 '11 #1

✓ answered by dwblas

Use .geometry() for the Toplevel http://www.tkdocs.com/tutorial/windows.html

Share this Question
Share on Google+
5 Replies


Expert 100+
P: 621
Use .geometry() for the Toplevel http://www.tkdocs.com/tutorial/windows.html
Oct 17 '11 #2

P: 4
Ok, thanks, I didn't know "geometry" could do that ^^

So, in my code I have this for one of my window:
Expand|Select|Wrap|Line Numbers
  1. class Starting_window(Tkinter.Tk):    #First instruction window
  2.     def __init__(self,parent):
  3.         Tkinter.Tk.__init__(self,parent)
  4.         self.parent = parent
  5.         self.initialize()
  6.         self.geometry("350x270")
So, if I want this to be placed close to the top left of the screen, I have to modify the last line above this way:

Expand|Select|Wrap|Line Numbers
  1. class Starting_window(Tkinter.Tk):    #First instruction window
  2.     def __init__(self,parent):
  3.         Tkinter.Tk.__init__(self,parent)
  4.         self.parent = parent
  5.         self.initialize()
  6.         self.geometry("350x270-5+5")
Right?
Oct 17 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
The geometry string "350x270-5+5" will place the right side of the window 5 pixels from the right side of the screen. If you want your window near the upper left corner, use "350x270+5+5".
Oct 17 '11 #4

P: 4
Thanks, you're great, it's working fine :)
Oct 18 '11 #5

P: 4
I even found a way to center my windows:

Expand|Select|Wrap|Line Numbers
  1. class Starting_window(Tkinter.Tk):    #First instruction window
  2.     def __init__(self, parent):
  3.         Tkinter.Tk.__init__(self, parent)
  4.         self.parent = parent
  5.         self.initialize()
  6.         self.geometry("350x270+%d+%d" %((self.winfo_screenwidth() - 350) / 2,
  7.                                         (self.winfo_screenheight() - 270) / 2))
  8.  
  9.     def initialize(self):
  10.         self.grid()
  11.         texte = Tkinter.Label(self, text = input_text, fg = "white",
  12.                                                                     bg = "blue")
  13.         texte.grid(column = 0, row = 0, sticky = 'N')
  14.         button_continue = Tkinter.Button(self, text = u"Continue",
  15.                                         command = self.OnButtonClickContinue)
  16.         button_continue.grid(column = 0, row = 5, sticky = 'W')
  17.         button_kill = Tkinter.Button(self, text = u"Quit",
  18.                                             command = self.OnButtonClickKill)
  19.         button_kill.grid(column = 0, row = 5, sticky = 'E')
  20.         self.resizable(False, False)
  21.  
  22.     def OnButtonClickContinue(self):
  23.         input_values.completion = 1
  24.         self.destroy()
  25.         app = First_Variable_Choice(None)
  26.         app.title('DetectorCL - Testing Tool')
  27.  
  28.     def OnButtonClickKill(self):
  29.         input_values.completion = 0
  30.         self.destroy()
  31.  
Much better this way, thanks all :)
Oct 18 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.