[英]Display fullscreen mode on Tkinter
如何在全屏模式下顯示 Tkinter 中的幀? 我看到了這段代碼,它非常有用……:
>>> import Tkinter
>>> root = Tkinter.Tk()
>>> root.overrideredirect(True)
>>> root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
…但是是否可以編輯代碼以便自動按下Esc使 window“恢復”?
我認為這就是你要找的:
Tk.attributes("-fullscreen", True) # substitute `Tk` for whatever your `Tk()` object is called
您也可以使用wm_attributes
而不是attributes
。
然后只需綁定轉義鍵並將其添加到處理程序中:
Tk.attributes("-fullscreen", False)
另一個問題的答案暗示了這一點(使用wm_attributes
)。 所以,我就是這樣發現的。 只是,不知為何,沒有人直接出來說這就是答案。 所以,我認為值得發布。
這是一個工作示例(在Xubuntu 14.04上測試),它使用F11來打開和關閉全屏,而escape只會將其關閉:
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()
如果你也想隱藏菜單,我發現只有兩種方法可以做到這一點。 一種是摧毀它。 另一種是做一個空白菜單來切換。
self.tk.config(menu=self.blank_menu) # self.blank_menu is a Menu object
然后在您希望它再次顯示時將其切換回您的菜單。
self.tk.config(menu=self.menu) # self.menu is your menu.
這將創建一個全屏窗口。 默認情況下,按Escape
會將窗口大小調整為“200x200+0+0”。 如果您移動或調整窗口大小, Escape
會在當前幾何圖形和前一幾何圖形之間切換。
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()
我想如果你只是在尋找全屏,不需要設置幾何或 maxsize 等。
你只需要這樣做:
- 如果你在 ubuntu 上工作:
root=tk.Tk()
root.attributes('-zoomed', True)
-如果您在 Windows 上工作:
root.state('zoomed')
現在要在全屏之間切換,為了將其最小化到任務欄,您可以使用:
Root.iconify()
這是一個簡單的 lambda 解決方案:
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()
這將使屏幕在按下 Esc 時退出全屏,並在按下 F11 時切換全屏。
這將在 mac 上創建一個完全全屏的窗口(沒有可見的菜單欄)而不會弄亂鍵綁定
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
root.mainloop()
是的,伙計,我試圖在 Windows 中做同樣的事情,而對我有幫助的是使用root.state()
方法的一些 lambdas。
root = Tk()
root.bind('<Escape>', lambda event: root.state('normal'))
root.bind('<F11>', lambda event: root.state('zoomed'))
只需使用這個:
from tkinter import *
root = Tk()
root.state('zoomed') # for full screen
root.mainloop()
如果您希望窗口最大化,那么這應該會有所幫助:
from tkinter import *
root = Tk()
root.state('zoomed')
root.mainloop()
但是如果你想把它全屏顯示:
from tkinter import *
root = Tk()
root.attributes('-fullscreen', True)
root.mainloop()
默認情況下,您可以執行以下操作來最大化屏幕:
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
我認為這會有所幫助(尤其是對於那些剛開始使用 Python 進行編碼的人)。
我可以想到兩種方法來解決這個問題。
第一個:使用 attributes() 函數。 該函數的語法如下:
window_name.attributes('-fullscreen',True)
所以,完整的例子是:
# 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()
這種方法的缺點是我們沒有工具欄。
第二個:使用 geometry() 函數所以,幾何函數的語法如下:
width= window_name.winfo_screenwidth()
height= window_name.winfo_screenheight()
window_name.geometry(“%dx%d” % (width, height))
和完整的例子:
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()
只需使用:
# 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()
如果你想隱藏除 window 之外的所有內容,你也可以使用:
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.