NEW! PaintShop Pro 2019

How to position a TKinter UI window

Moderator: Kathy_9

How to position a TKinter UI window

Postby dangelic0 » Sat Mar 16, 2019 12:19 am

I am a reasonably capable scripter but I could not find any mechanism to position a UI dialog box created using TKinter and StartForeignWindow

It always seems to be in the upper left

Looked at various online resources. Any hints would be much appreciated

Using PSP 2019, Windows10

Dean
dangelic0
 
Posts: 21
Joined: Sat Oct 22, 2011 4:07 pm
operating_system: Windows XP Pro
System_Drive: C
32bit or 64bit: 32 Bit
motherboard: Dell Inc. 0J584C
processor: 2.50 gigahertz Intel Core2 Quad
ram: 4GB
Video Card: NVIDIA GeForce 8800 GT
sound_card: Realtek High Definition Audio
Hard_Drive_Capacity: 960GB
Monitor/Display Make & Model: DELL SP2309W

Re: How to position a TKinter UI window

Postby LeviFiction » Sat Mar 16, 2019 1:43 am

Use the geometry method and only include the position elements.

Setting the exact pixel coordinate on the screen is very simple. In the init method of your TKinter class, or any time before the mainloop() really.

The geometry() method takes a string with the format 'widthxheight+x+y' if you leave out the 'widthxheight' portion you only nee to pass in '+x+y'. In these examples we'll use the string formatter "%" symbol to fill in the string with the appropriate values. But anything that does this is fine. For example '+' + str(x) + '+' + str(y) would be perfectly acceptable. And depending on your version of PSP so would '+{}+{}'.format(x,y).

Code: Select all
self.master.geometry('+%d+%d'%(x,y))


In this example I've created a custom class based on the Frame class. And in my init method the default attribute "master" points to the root window. If you've set your root window manually you would just use that variable.

Code: Select all
root = Tk()
root.geometry('+%d+%d'%(x,y))


So, let's say, for example, that I wanted the upper left hand corner of the dialog to appear at the center of my screen. I can use the winfo_screenwidth and winfo_screenheight commands to calculate where the center of my screen is. Then enter that as the x and y of my window.

Code: Select all
x = self.master.winfo_screenwidth()/2
y = self.master.winfo_screenheight()/2
self.master.geometry('+%d+%d'%(x,y))


In this example I'm still in the init method of my class, so I don't yet know how large he geometry manager is going to make my dialog so I don't take that into account at all. Though I could figure this out by calling self.master.update_idletasks() then check self.master.winfo_width() and self.master.winfo_height() at the end of my init class.
LeviFiction
Advisor
 
Posts: 5410
Joined: Thu Oct 02, 2008 1:07 pm
Location: USA
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PSP: 8, 9, X, XI, X2, X8,X9, 2018, 2019

Re: How to position a TKinter UI window

Postby dangelic0 » Tue Mar 26, 2019 5:28 pm

Fantastic - thanks very much
Dean
dangelic0
 
Posts: 21
Joined: Sat Oct 22, 2011 4:07 pm
operating_system: Windows XP Pro
System_Drive: C
32bit or 64bit: 32 Bit
motherboard: Dell Inc. 0J584C
processor: 2.50 gigahertz Intel Core2 Quad
ram: 4GB
Video Card: NVIDIA GeForce 8800 GT
sound_card: Realtek High Definition Audio
Hard_Drive_Capacity: 960GB
Monitor/Display Make & Model: DELL SP2309W


Return to PSP Scripting

Who is online

Users browsing this forum: No registered users and 1 guest