Python Forum
[Tkinter] Hi, Keep postition of main window after iconify()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Hi, Keep postition of main window after iconify()
#1
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.
Reply
#2
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)
"Often stumped... But never defeated."
Reply
#3
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
Reply
#4
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
"Often stumped... But never defeated."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 347 Mar-17-2024, 09:37 AM
Last Post: deanhystad
Exclamation [Tkinter] Error when closing the main window with destroy TomasSanchexx 1 728 Aug-06-2023, 01:54 AM
Last Post: deanhystad
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 3,399 Aug-02-2021, 08:47 PM
Last Post: JayCee
  [PyQt] How to clip layout to sides and bottom of main window? Valmont 9 4,840 Mar-24-2021, 10:00 PM
Last Post: deanhystad
  "tkinter.TclError: NULL main window" Rama02 1 5,786 Feb-04-2021, 06:45 PM
Last Post: deanhystad
  [Tkinter] Auto re-fit frames sizes in main window Gilush 2 2,608 Jun-06-2020, 03:14 AM
Last Post: Gilush
  [Tkinter] How to add multiple frames to main window Dandy_Don 13 7,791 Apr-29-2020, 09:21 PM
Last Post: Dandy_Don
  tkinter window and turtle window error 1885 3 6,625 Nov-02-2019, 12:18 PM
Last Post: 1885
  “main thread is not in main loop” in Tkinter Long_r 1 24,090 Jun-26-2019, 11:00 PM
Last Post: metulburr
  pyqt main window refresh poblems duende 0 5,335 Apr-13-2018, 05:05 PM
Last Post: duende

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020