2

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

Darren Woodson
  • 68
  • 1
  • 11
  • Look at [this](https://stackoverflow.com/a/15982406/11106801). – TheLizzard Mar 05 '21 at 20:09
  • TheLizzard - This is exactly one of the articles I looked at prior to asking this question and my issue is that, as stated above, the winfo_height, and winfo_width values are 1, and 1 as I assume they're set before the "zoomed" state takes affect, or if I don't have the "zoomed" state it defaults to I think "200x200". I could create a resize event handler but I'm not sure what the event would be at that point as it would have to be after .state("zoomed") or after some initialization event. – Darren Woodson Mar 05 '21 at 20:13
  • Or rather the screen_width and screen_height causes the issues I described about the screen clipping past the task bar or into my second monitor. – Darren Woodson Mar 05 '21 at 20:14
  • You have to call `.update()` so that tkinter can update itself and then call `winfo_height` and `winfo_width`. But that will show the window for 1 frame. – TheLizzard Mar 05 '21 at 20:16
  • Thank you, I feel ridiculous now, but all it did take was a mainWindow.update() after the .state("zoomed") statement. I would have sworn on all I owned that I had already done so but perhaps I put it in the wrong order before. Thank you again! – Darren Woodson Mar 05 '21 at 20:18
  • Btw this is one of the best questions I have seen. You actually showed what you tried. +1 from me. – TheLizzard Mar 05 '21 at 20:19

1 Answers1

0

Instead of trying to create the canvas at the correct size, create it at the minimum size you think it should be, and then let one of tkinter's geometry managers expand it to fill the window.

For example, if you do mainCanvas.pack(fill="both", expand=True) it will cause the canvas to fill the window without you having to compute the size of the canvas, and without having to call update during startup.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685