How can I make a frame in Tkinter display in fullscreen mode? I saw this code, and it's very useful…:
>>> import Tkinter
>>> root = Tkinter.Tk()
>>> root.overrideredirect(True)
>>> root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
…but is it possible to edit the code so that hitting Esc automatically makes the window "Restore down"?
I think this is what you're looking for:
Tk.attributes("-fullscreen", True) # substitute `Tk` for whatever your `Tk()` object is called
You can use wm_attributes
instead of attributes
, too.
Then just bind the escape key and add this to the handler:
Tk.attributes("-fullscreen", False)
An answer to another question alluded to this (with wm_attributes
). So, that's how I found out. But, no one just directly went out and said it was the answer for some reason. So, I figured it was worth posting.
Here's a working example (tested on Xubuntu 14.04) that uses F11 to toggle fullscreen on and off and where escape will turn it off only:
import sys
if sys.version_info[0] == 2: # Just checking your Python version to import Tkinter properly.
from Tkinter import *
else:
from tkinter import *
class Fullscreen_Window:
def __init__(self):
self.tk = Tk()
self.tk.attributes('-zoomed', True) # This just maximizes it so we can see the window. It's nothing to do with fullscreen.
self.frame = Frame(self.tk)
self.frame.pack()
self.state = False
self.tk.bind("<F11>", self.toggle_fullscreen)
self.tk.bind("<Escape>", self.end_fullscreen)
def toggle_fullscreen(self, event=None):
self.state = not self.state # Just toggling the boolean
self.tk.attributes("-fullscreen", self.state)
return "break"
def end_fullscreen(self, event=None):
self.state = False
self.tk.attributes("-fullscreen", False)
return "break"
if __name__ == '__main__':
w = Fullscreen_Window()
w.tk.mainloop()
If you want to hide a menu, too, there are only two ways I've found to do that. One is to destroy it. The other is to make a blank menu to switch between.
self.tk.config(menu=self.blank_menu) # self.blank_menu is a Menu object
Then switch it back to your menu when you want it to show up again.
self.tk.config(menu=self.menu) # self.menu is your menu.
This creates a fullscreen window. Pressing Escape
resizes the window to '200x200+0+0' by default. If you move or resize the window, Escape
toggles between the current geometry and the previous geometry.
import Tkinter as tk
class FullScreenApp(object):
def __init__(self, master, **kwargs):
self.master=master
pad=3
self._geom='200x200+0+0'
master.geometry("{0}x{1}+0+0".format(
master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
master.bind('<Escape>',self.toggle_geom)
def toggle_geom(self,event):
geom=self.master.winfo_geometry()
print(geom,self._geom)
self.master.geometry(self._geom)
self._geom=geom
root=tk.Tk()
app=FullScreenApp(root)
root.mainloop()
I think if you are looking for fullscreen only, no need to set geometry or maxsize etc.
You just need to do this:
-If you are working on ubuntu:
root=tk.Tk()
root.attributes('-zoomed', True)
-and if you are working on windows:
root.state('zoomed')
Now for toggling between fullscreen, for minimising it to taskbar you can use:
Root.iconify()
Here's a simple solution with lambdas:
root = Tk()
root.attributes("-fullscreen", True)
root.bind("<F11>", lambda event: root.attributes("-fullscreen",
not root.attributes("-fullscreen")))
root.bind("<Escape>", lambda event: root.attributes("-fullscreen", False))
root.mainloop()
This will make the screen exit fullscreen when escape is pressed, and toggle fullscreen when F11 is pressed.
This will create a completely fullscreen window on mac (with no visible menubar) without messing up keybindings
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
root.mainloop()
Yeah mate i was trying to do the same in windows, and what helped me was a bit of lambdas with the root.state()
method.
root = Tk()
root.bind('<Escape>', lambda event: root.state('normal'))
root.bind('<F11>', lambda event: root.state('zoomed'))
Just use this:
from tkinter import *
root = Tk()
root.state('zoomed') # for full screen
root.mainloop()
If You Want The Window To Be Maximized Then This should Help:
from tkinter import *
root = Tk()
root.state('zoomed')
root.mainloop()
But If You Want To Put It in Fullscreen:
from tkinter import *
root = Tk()
root.attributes('-fullscreen', True)
root.mainloop()
Here's what you can do to maximise your screen by default:
from tkinter import *
root = Tk()
# Your code goes here
root.state(“zoomed”)
root.mainloop()
import tkinter as tk
root = tk.Tk()
root.title('Game')
screen.attributes('-fullscreen', True) #True, fullscreen/False, exit fullscreen
I think this would be helpful (especially for those that are new in coding with Python).
I can think of 2 ways to solve this problem.
First one: Using attributes() function. Syntax of this function is following:
window_name.attributes('-fullscreen',True)
So, full example is:
# Here we are importing tkinter for gui
import tkinter as tk
# Simply creating window
example = tk.Tk()
# We're setting attribute
example.attributes('-fullscreen', True)
example.mainloop()
Disadvantage of this approach is that we don't have toolbar.
Second one: Using geometry() function So, syntax of geometry function is following:
width= window_name.winfo_screenwidth()
height= window_name.winfo_screenheight()
window_name.geometry(“%dx%d” % (width, height))
And full example:
import tkinter as tk
example=tk.Tk()
#getting screen width and height of display
width= example.winfo_screenwidth()
height= example.winfo_screenheight()
example.geometry("%dx%d" % (width, height))
example.title("Example")
label = tk.Label(window, text="EXAMPLE WITH TOOLBAR")
label.pack()
example.mainloop()
Just use:
# importing tkinter for gui
import tkinter as tk
# creating window
window = tk.Tk()
# setting attribute
window.state('zoomed')
window.title("Full window")
# creating text label to display on window screen
label = tk.Label(window, text="Hello world!")
label.pack()
window.mainloop()
If you want to hide everything except the window, you can also use:
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
root.title("FullScreen")
label = tk.Label(root, text="Hello! Press 🪟 logo on the keypad > select the python logo > Close window to close")
labela = tk.Label(root, text="🎉🎉🎉")
label.pack()
labela.pack()
root.mainloop()
root = Tk()
root.geomentry('1599x1499')
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.