tkinter winfo_screenwidth() when used with dual monitors

前端 未结 2 1050
面向向阳花
面向向阳花 2021-01-13 17:46

With tkinter canvas, to calculate the size of the graphics I display, I normally use the function winfo_screenwidth(), and size my objects accordingly. But when

相关标签:
2条回答
  • 2021-01-13 17:58

    Based on this slightly different question, I would suggest the following:

     t.state('zoomed')
     m_1_height= t.winfo_height()
     m_1_width= t.winfo_width() #this is the width you need for monitor 1 
    

    That way the window will zoom to fill one screen. The other monitor's width is just wininfo_screenwidth()-m_1_width

    I also would point you to the excellent ctypes method of finding monitor sizes for windows found here. NOTE: unlike the post says, ctypes is in stdlib! No need to install anything.

    0 讨论(0)
  • 2021-01-13 18:10

    It is an old question, but still: for a cross-platform solution, you could try the screeninfo module, and get information about every monitor with:

    import screeninfo
    screeninfo.get_monitors()
    

    If you need to know on which monitor one of your windows is located, you could use:

    def get_monitor_from_coord(x, y):
        monitors = screeninfo.get_monitors()
    
        for m in reversed(monitors):
            if m.x <= x <= m.width + m.x and m.y <= y <= m.height + m.y:
                return m
        return monitors[0]
    
    # Get the screen which contains top
    current_screen = get_monitor_from_coord(top.winfo_x(), top.winfo_y())
    
    # Get the monitor's size
    print current_screen.width, current_screen.height
    

    (where top is your Tk root)

    0 讨论(0)
提交回复
热议问题