Reputation: 478
I'm trying to find a way to make a Tkinter application's main window utilise all screen space. I have to always maximise the screen after the app starts. I have tried this code:
win.state('zoomed')
However, it's not supported on Linux. I need a solution that will work on any platform.
Upvotes: 0
Views: 368
Reputation: 4050
There's lots of information in the answers to this question, but there are issues on different systems with each:
root.state("zoomed")
doesn't work on Linux, but it does work on Windows.
root.wm_attributes("-zoomed", True)
works in Linux (and Mac, as far as I know), but not in Windows.
If you read the comments on the accepted answer, it appears it doesn't work well on all systems/on multiple monitors.
Basically, there doesn't appear to be any single cross-platform solution to this. So what I suggest is that you use a try
/except
block, and if one solution fails, use the other:
import tkinter
root = tkinter.Tk()
try:
root.wm_attributes("-zoomed", True)
except tkinter.TclError:
root.state('zoomed')
root.mainloop()
An alternative for those who don't like try
/except
blocks is to use the built-in platform
module, and some if
statements:
import tkinter, platform
root = tkinter.Tk()
if platform.uname()[0] == "Windows":
root.state('zoomed')
else:
root.wm_attributes("-zoomed", True)
root.mainloop()
I'm 99% sure this works on Windows, but as I don't have access to a Windows for testing, it'd be nice to have have someone confirm that it works on Windows. Hope this helps, and let me know if you have any questions!
Upvotes: 3