How to make a window fullscreen in a secondary display with tkinter?

问题描述 投票:0回答:5

我知道如何在“主”显示器中全屏显示窗口,但即使将我的应用程序窗口移动到连接到我的 PC 的辅助显示器,当我打电话时:

self.master.attributes('-fullscreen', True)

要全屏显示该窗口,它会在“主”显示器中进行,而不是在辅助显示器中进行(应用程序的窗口从辅助显示器中消失,并立即全屏显示在“主”显示器中)。

如何在辅助显示器中全屏显示?

python tkinter screen fullscreen
5个回答
7
投票

这在 Windows 7 上有效:如果第二个屏幕的宽度和高度与第一个屏幕相同,您可以使用以下代码的 win1 或 win2 几何图形,具体取决于其相对位置(leftof 或 rightof),以便在辅助显示中显示全屏:

from Tkinter import *

def create_win():
    def close(): win1.destroy();win2.destroy()
    win1 = Toplevel()
    win1.geometry('%dx%d%+d+%d'%(sw,sh,-sw,0))
    Button(win1,text="Exit1",command=close).pack()
    win2 = Toplevel()
    win2.geometry('%dx%d%+d+%d'%(sw,sh,sw,0))
    Button(win2,text="Exit2",command=close).pack()

root=Tk()
sw,sh = root.winfo_screenwidth(),root.winfo_screenheight()
print "screen1:",sw,sh
w,h = 800,600 
a,b = (sw-w)/2,(sh-h)/2 

Button(root,text="Exit",command=lambda r=root:r.destroy()).pack()
Button(root,text="Create win2",command=create_win).pack()

root.geometry('%sx%s+%s+%s'%(w,h,a,b))
root.mainloop()

3
投票

尝试:

from Tkinter import *

rot = Tk()


wth,hgh = rot.winfo_screenwidth(),rot.winfo_screenheight()
#take desktop width and hight (pixel)
_w,_h = 800,600 #root width and hight
a,b = (wth-_w)/2,(hgh-_h)/2 #Put root to center of display(Margin_left,Margin_top)



def spann():
    def _exit():
        da.destroy()

    da = Toplevel()
    da.geometry('%dx%d+%d+%d' % (wth, hgh,0, 0))

    Button(da,text="Exit",command=_exit).pack()
    da.overrideredirect(1)
    da.focus_set()#Restricted access main menu




Button(rot,text="Exit",command=lambda rot=rot : rot.destroy()).pack()


but = Button(rot,text="Show SUB",command=spann)
but.pack()


rot.geometry('%sx%s+%s+%s'%(_w,_h,a,b))
rot.mainloop()
""" Geometry pattern 'WxH+a+b'
        W = Width
        H = Height
        a = Margin_left+Margin_Top"""

3
投票

2021年有效的超简单方法

即使两个显示器的分辨率不同,这也能正常工作。使用

geometry
将第二个显示器偏移第一个显示器的宽度。
geometry
字符串的格式为
<width>x<height>+xoffset+yoffset
:

root = tkinter.Tk()

# specify resolutions of both windows
w0, h0 = 3840, 2160
w1, h1 = 1920, 1080

# set up a window for first display, if wanted  
win0 = tkinter.Toplevel()
win0.geometry(f"{w0}x{h0}+0+0")

# set up window for second display with fullscreen 
win1 = tkinter.Toplevel()
win1.geometry(f"{w1}x{h1}+{w0}+0") # <- this is the key, offset to the right by w0
win1.attributes("-fullscreen", True)

只要您知道第一个显示器的宽度,就可以正常工作。运行 TK 的 X 系统默认将第二个显示器放在第一个显示器的右侧。


2
投票

Windows、Python 3.8

在此解决方案中,按

F11
将使窗口全屏显示在当前屏幕上。

请注意,根据文档,

self.root.state("zoomed")
是特定于 Windows 的。

self.root.overrideredirect(True)
在 Windows 中很奇怪,可能会产生不需要的副作用。例如,在激活此选项的情况下,我遇到了与更改屏幕配置相关的问题。

#!/usr/bin/env python3
import tkinter as tk


class Gui:
    fullScreen = False

    def __init__(self):
        self.root = tk.Tk()
        self.root.bind("<F11>", self.toggleFullScreen)
        self.root.bind("<Alt-Return>", self.toggleFullScreen)
        self.root.bind("<Control-w>", self.quit)
        self.root.mainloop()

    def toggleFullScreen(self, event):
        if self.fullScreen:
            self.deactivateFullscreen()
        else:
            self.activateFullscreen()

    def activateFullscreen(self):
        self.fullScreen = True

        # Store geometry for reset
        self.geometry = self.root.geometry()

        # Hides borders and make truly fullscreen
        self.root.overrideredirect(True)

        # Maximize window (Windows only). Optionally set screen geometry if you have it
        self.root.state("zoomed")

    def deactivateFullscreen(self):
        self.fullScreen = False
        self.root.state("normal")
        self.root.geometry(self.geometry)
        self.root.overrideredirect(False)

    def quit(self, event=None):
        print("quiting...", event)
        self.root.quit()


if __name__ == '__main__':
    Gui()

0
投票

找了半天实现副屏全屏,最后发现

overrideredirect(1)
+
root.geometry
到副屏的位置就可以实现。(如果实现不了我就去用PyQt5。 :(

而且即使按

root.geometry
移动窗口,
root.wm_attributes('-fullscreen',True)
方法的全屏仍然在主屏幕上。

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()

如何在 Python 中获得显示器分辨率?

使用 tkinter 和 python 3.7.2 在副屏中创建全屏应用程序

© www.soinside.com 2019 - 2024. All rights reserved.