Positioning frames on screen

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lestat BZH
    New Member
    • Oct 2011
    • 4

    Positioning frames on screen

    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
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

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

    Comment

    • Lestat BZH
      New Member
      • Oct 2011
      • 4

      #3
      Ok, thanks, I didn't know "geometry" could do that ^^

      So, in my code I have this for one of my window:
      Code:
      class Starting_window(Tkinter.Tk):	#First instruction window
      	def __init__(self,parent):
      		Tkinter.Tk.__init__(self,parent)
      		self.parent = parent
      		self.initialize()
      		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:

      Code:
      class Starting_window(Tkinter.Tk):	#First instruction window
      	def __init__(self,parent):
      		Tkinter.Tk.__init__(self,parent)
      		self.parent = parent
      		self.initialize()
      		self.geometry("350x270-5+5")
      Right?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        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".

        Comment

        • Lestat BZH
          New Member
          • Oct 2011
          • 4

          #5
          Thanks, you're great, it's working fine :)

          Comment

          • Lestat BZH
            New Member
            • Oct 2011
            • 4

            #6
            I even found a way to center my windows:

            Code:
            class Starting_window(Tkinter.Tk):	#First instruction window
            	def __init__(self, parent):
            		Tkinter.Tk.__init__(self, parent)
            		self.parent = parent
            		self.initialize()
            		self.geometry("350x270+%d+%d" %((self.winfo_screenwidth() - 350) / 2,
            										(self.winfo_screenheight() - 270) / 2))
            
            	def initialize(self):
            		self.grid()
            		texte = Tkinter.Label(self, text = input_text, fg = "white",
            																	bg = "blue")
            		texte.grid(column = 0, row = 0, sticky = 'N')
            		button_continue = Tkinter.Button(self, text = u"Continue",
            										command = self.OnButtonClickContinue)
            		button_continue.grid(column = 0, row = 5, sticky = 'W')
            		button_kill = Tkinter.Button(self, text = u"Quit",
            											command = self.OnButtonClickKill)
            		button_kill.grid(column = 0, row = 5, sticky = 'E')
            		self.resizable(False, False)
            
            	def OnButtonClickContinue(self):
            		input_values.completion = 1
            		self.destroy()
            		app = First_Variable_Choice(None)
            		app.title('DetectorCL - Testing Tool')
            		
            	def OnButtonClickKill(self):
            		input_values.completion = 0
            		self.destroy()
            Much better this way, thanks all :)

            Comment

            Working...