Python Forum
[Tkinter] Hi, Keep postition of main window after iconify() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Hi, Keep postition of main window after iconify() (/thread-28255.html)



Hi, Keep postition of main window after iconify() - delphinis - Jul-11-2020

Hi,
i write an app using iconify after creating the window:

   root = tk.Tk()
   app = Window(root)
   root.iconify()
   #root.wm_withdraw()
   root.mainloop()
problem: When clicking on it in the Windows task bar, the window doesn't appear at the previous position but on a positon somewhere top left.
Trying a few times it appears always top left but shifting a bit each time a bit to botoom right.
Note: This is just done the first time after start of the program and clicking it in the windows task bar. Moving the window afterwards and click on the [-] symbol making it invisible and then clicking it in the windows task bar to make it visible, it is apearing at the position where it was moved before.
Using Windows 10

If i try with the withdraw the problem doesn't happen, but withdraw does completely hiding the program. It even doesn't appear in the tasklist. The only way to test was to make it visible after a few seconds using deiconify(). But i whant the user to make it visible by clicking to the icon.


RE: Hi, Keep postition of main window after iconify() - DT2000 - Jul-11-2020

You can setup the configuration for your window size and monitor display this way. When you iconify the window it will again appear in the same position as it was initially displayed, centered in the frame when you click the tray icon to bring it back into view.
from tkinter import *
import pywintypes
import win32con
import win32api

root = Tk()
devmode = pywintypes.DEVMODEType()

#set you window display configuration to what you want it below
devmode.PelsWidth = 1280
devmode.PelsHeight = 1024
devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT
win32api.ChangeDisplaySettings(devmode, 0)
# set your window size below
w = 500
h = 500
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.resizable(height=FALSE,width=FALSE)
def resize():
    root.iconify()
minimize = Button(root,text="Minimize", command = resize)
minimize.pack()

root.mainloop()
win32api.ChangeDisplaySettings(None, 0)



RE: Hi, Keep postition of main window after iconify() - delphinis - Jul-12-2020

Hi,
how to install those 3 special packages ? Sorry i am a bit new on python. Tried to install with pip install "package" but i get always: "ERROR: Could not find a version that satisfies the requirement win64api (from versions: none)
ERROR: No matching distribution found for win32api"
Working on a 64bit win 10


RE: Hi, Keep postition of main window after iconify() - DT2000 - Jul-12-2020

I am not sure that an update has been published on pypi however the above 32bit packages work the same on 64bit as they do on 32bit systems.

wincore download
win-api download