简体   繁体   中英

resizing tkinter window for full screen

I know there's a ton of postings like this one. However, I don't think I've come across one with quite this question.

I know the following will grab the current width and height of the screen and set the tkinter window to be this size and then similarly for the canvas should we wish to have one.

import tkinter as tk
root = tk.Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry("%dx%d" % (width, height))
canvas = tk.Canvas(root, .....)
canvas.pack(fill = "both", expand = True)

However, what I've noticed using this is that there is always a portion of the tkinter window that is cut off from the taskbar at the bottom of the screen. I find this to be a bit annoying, especially if you have labels or or other such features that you wish to be visible at the bottom.

Is there anyway to get around this? I know we could subtract an offset factor from the height to account for a possible taskbar at the bottom of the screen, but then this awkwardly displaces the tkinter canvas.

I believe there's no way to actually get the taskbar information from inside Tkinter on Windows.

But you don't have to. If you set the main window's state to "zoomed", Tkinter will ask Windows to maximize your window, and Windows will take care of fitting you in with the taskbar—and also handle any other weird decorations you might have, and handle auto-hiding taskbars, and so on.

root.state('zoomed')

As far as I can tell, Tkinter doesn't document this value (the state docs in the Tkinter book say only 'normal', 'iconic', 'withdraw', and 'icon' are allowed).

But, as is so often the case, if you turn to the Tcl/Tk docs, you discover that Tkinter's documentation is just incomplete. Even in the tutorial , it mentions:

On most systems, you can temporarily remove the window from the screen by iconifying it. In Tk, whether or not a window is iconified is referred to as the window's state. The possible states for a window include "normal" and "iconic" (for an iconified window), as well as several others: "withdrawn", "icon" or "zoomed".

The Tk reference documentation for wm state says:

… either normal, iconic, withdrawn, icon, or (Windows and Mac OS X only) zoomed.

Also, IIRC, you can use "zoom" for "zoomed" (and also "withdraw" for "withdrawn", "iconified" or "iconify" for "iconic", and "iconwindow" for "icon"), but Tk tries to be inconsistently friendly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM