How to position a TKinter UI window

Moderator: Kathy_9

Post Reply
dangelic0
Posts: 36
Joined: Sat Oct 22, 2011 4:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Dell XPS 8920
processor: Core i7-7700 4.2 GHx
ram: 32GB
Video Card: NVIDIA GeForce GTX 1060 6GB
sound_card: Realtek High Definition Audio
Hard_Drive_Capacity: 4 TB
Monitor/Display Make & Model: LG Ultra HD
Corel programs: PSP. VSP

How to position a TKinter UI window

Post by dangelic0 »

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
LeviFiction
Advisor
Posts: 6831
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: How to position a TKinter UI window

Post by LeviFiction »

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.
https://levifiction.wordpress.com/
dangelic0
Posts: 36
Joined: Sat Oct 22, 2011 4:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Dell XPS 8920
processor: Core i7-7700 4.2 GHx
ram: 32GB
Video Card: NVIDIA GeForce GTX 1060 6GB
sound_card: Realtek High Definition Audio
Hard_Drive_Capacity: 4 TB
Monitor/Display Make & Model: LG Ultra HD
Corel programs: PSP. VSP

Re: How to position a TKinter UI window

Post by dangelic0 »

Fantastic - thanks very much
Dean
Post Reply