I have tried to find this answer but with little to no luck surprisingly. Here is what I am running:
Python 3.8.5 64 bit
TKinter 8.6
The two sources for screen size that I have found are:
import tkinter as tk
from win32api import GetSystemMetrics
screen_height = GetSystemMetrics(1)
screen_width = GetSystemMetrics(0)
mainWindow = tk.Tk()
mainWindow.configure(width=screen_width, height=screen_height)
mainWindow.mainloop()
The issue with this method is that it creates a window from corner to corner inclusively that is the exact size of my screen, meaning that with the task bar and borders considered, the app clips off the screen and also 'under' the task bar.
There was also this method I found:
import tkinter as tk
mainWindow = tk.Tk()
mainWindow.state("zoomed")
mainWindow.mainloop()
This does exactly what I would want, imitate the way the app would look if I were to click the maximize button, lined up with both edges of the screen and 'resting' on top of the task bar.
However if I want to create a canvas with the same size as the window, but using winfo_height()
and winfo_width()
on my Tk object the returned values are 1 and 1 respectively. I'm not sure if this is because those values are set before setting the state to 'zoomed' but the height and width arguments in tk.Canvas have units of either pixels (default), inches, millimeters, centimeters, or print points. Thus I cannot use those exact values to create the canvas.
import tkinter as tk
mainWindow = tk.Tk()
mainWindow.state("zoomed")
mainCanvas = tk.Canvas(height=mainWindow.winfo_height(), width=mainWindow.winfo_width(), bg="blue")
mainCanvas.pack()
mainWindow.mainloop()
Lastly there is the attributes section that could be utilized:
#blah code the same as before
mainWindow.attributes("-fullscreen", True)
#blah code till the end as before
This unfortunately creates a fullscreen app as if you hit F11 on most browsers or fullscreened a youtube video, etc. This is not what I want obviously.
Do I need to create a listener that waits until the window state has been changed to zoom before grabbing the height and width of the application? Or is there some way to simply start the application in a 'maximized' state as if the maximize button were pressed by default.
I couldn't believe the difficulty in finding anything on the tk class' .state method, nor any information really about simply maximizing a TK app window. Everything I found landed in one of the three examples I shared and I would greatly appreciate any help anyone could offer!
Thank you