我正在寻找一种通用的方法来获得以像素为单位的屏幕尺寸,使用matplotlib与任何交互式后端(例如TkAgg,Qt4Agg或macosx)。
我正在尝试写一个函数,它可以在屏幕上的一组标准位置打开一个窗口,比如屏幕的右半部分或右上角。
我写了一个有效的解决方案here,复制如下,但它需要使用full_screen_toggle() (按照建议的here)创建一个全屏窗口来测量它的大小。
我正在寻找一种方法来获得屏幕大小,而不是创建一个全屏幕窗口,然后改变它的大小。
import matplotlib.pyplot as plt
def move_figure(position="top-right"):
'''
Move and resize a window to a set of standard positions on the screen.
Possible positions are:
top, bottom, left, right, top-left, top-right, bottom-left, bottom-right
'''
mgr = plt.get_current_fig_manager()
mgr.full_screen_toggle() # primitive but works to get screen size
py = mgr.canvas.height()
px = mgr.canvas.width()
d = 10 # width of the window border in pixels
if position == "top":
# x-top-left-corner, y-top-left-corner, x-width, y-width (in pixels)
mgr.window.setGeometry(d, 4*d, px - 2*d, py/2 - 4*d)
elif position == "bottom":
mgr.window.setGeometry(d, py/2 + 5*d, px - 2*d, py/2 - 4*d)
elif position == "left":
mgr.window.setGeometry(d, 4*d, px/2 - 2*d, py - 4*d)
elif position == "right":
mgr.window.setGeometry(px/2 + d, 4*d, px/2 - 2*d, py - 4*d)
elif position == "top-left":
mgr.window.setGeometry(d, 4*d, px/2 - 2*d, py/2 - 4*d)
elif position == "top-right":
mgr.window.setGeometry(px/2 + d, 4*d, px/2 - 2*d, py/2 - 4*d)
elif position == "bottom-left":
mgr.window.setGeometry(d, py/2 + 5*d, px/2 - 2*d, py/2 - 4*d)
elif position == "bottom-right":
mgr.window.setGeometry(px/2 + d, py/2 + 5*d, px/2 - 2*d, py/2 - 4*d)
if __name__ == '__main__':
# Usage example for move_figure()
plt.figure(1)
plt.plot([0, 1])
move_figure("top-right")
plt.figure(2)
plt.plot([0, 3])
move_figure("bottom-right")发布于 2017-03-22 20:31:04
这适用于TKagg
import matplotlib.pyplot as plt
window = plt.get_current_fig_manager().window
screen_x, screen_y = window.wm_maxsize()
#or
screen_y = window.winfo_screenheight()
screen_x = window.winfo_screenwidth()唯一奇怪的是,我看到的是,有时y尺寸的偏差约为22像素。
发布于 2015-03-19 04:01:06
这适用于OS X:
import AppKit
[(screen.frame().size.width, screen.frame().size.height) for screen in AppKit.NSScreen.screens()]NSScreen类上的苹果文档。
https://stackoverflow.com/questions/29087072
复制相似问题