I searched for a long time to achieve fullscreen on the subscreen, and finally found that overrideredirect(1)
+ root.geometry
to the location of the secondary screen can be achieved.(If this can not be achieved I will go to use PyQt5. :(
And even if the window is moved by root.geometry
, the fullscreen of the root.wm_attributes('-fullscreen',True)
method is still on the main screen。
from tkinter import *
import ctypes.wintypes
def get_monitors_info():
"""Obtain all monitors information and return information for the second monitor"""
"""Windows only - using user32 eliminates the need to install the pywin32 software package"""
user32 = ctypes.windll.user32
def _get_monitors_resolution():
monitors = []
monitor_enum_proc = ctypes.WINFUNCTYPE(
ctypes.c_int, ctypes.c_ulong, ctypes.c_ulong, ctypes.POINTER(ctypes.wintypes.RECT), ctypes.c_double)
# Callback function,to obtain information for each display
def callback(hMonitor, hdcMonitor, lprcMonitor, dwData):
monitors.append((lprcMonitor.contents.left, lprcMonitor.contents.top,
lprcMonitor.contents.right - lprcMonitor.contents.left,
lprcMonitor.contents.bottom - lprcMonitor.contents.top))
return 1
# Enumerate all Monitors
user32.EnumDisplayMonitors(None, None, monitor_enum_proc(callback), 0)
return monitors
# All monitors information
monitors = _get_monitors_resolution()
return monitors
import tkinter as tk
from PIL import Image, ImageTk
import tkinter.font as tkFont
class MyApp:
def __init__(self, master):
self.master = master
master.title("My App")
monitors = get_monitors_info()
if len(monitors) >= 2:
x1=monitors[1][0]
y1=monitors[1][1]
w1=monitors[1][2]
h1=monitors[1][3]
print("%dx%d+%d+%d" % (w1, h1, x1, y1))
"Can move the window via root.geometry, but it cannot be moved to full screen on the secondary monitor via root.wm_attributes('-fullscreen',True) either"
"The fullscreen top-level window created with overrideredirect(1) can be fullscreen on the secondary screen after moving the position。"
root.geometry("%dx%d+%d+%d" % (w1, h1, x1, y1))
# root.wm_attributes('-fullscreen', True)
root.overrideredirect(1)
# root.attributes("-topmost", True)
else:
w1=monitors[0][2]
h1 = monitors[0][3]
root.geometry("%dx%d+%d+%d" % (w1, h1, 0, 0))
root.overrideredirect(1)
master.bind('<Double-Button-1>', self.toggle_fullscreen)
master.bind("<F11>", self.toggle_fullscreen)
master.bind('<Escape>', self.close)
def toggle_fullscreen(self, event=None):
overrideredirect_value = root.overrideredirect()
if(overrideredirect_value):
root.overrideredirect(0)
else:
root.overrideredirect(1)
def close(self, event=None):
# set the running flag to False to stop updating the image
self.running = False
# close the window
self.master.destroy()
root = tk.Tk()
app = MyApp(root)
root.mainloop()
How do I get monitor resolution in Python?
Create a fullscreen app in the secondary screen using tkinter and python 3.7.2