打开 Tkinter 窗口,使其位于开始菜单栏上 [英] Open Tkinter Window so it sits on start menu bar

查看:69
本文介绍了打开 Tkinter 窗口,使其位于开始菜单栏上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在屏幕右下角或开始栏所在的位置打开一个 Tkinter 窗口.就像当您单击笔记本电脑上的电池图标并弹出框时一样.我的代码目前将它隐藏在开始菜单栏后面.我基本上喜欢它在右下角但坐在开始菜单栏的顶部.另外,如果开始菜单不在底部,不确定如何解释.

I would like to have a Tkinter window open at the bottom right of the screen or where ever the start bar is. Much like when you click on the battery icon on your laptp and the box pops up. My code currently hides it behind the start menu bar. I would essentially like it at the bottom right but sitting on top of the start menu bar. Also, not sure how to account for things if the start menu is not at the bottom.

我的代码:

from Tkinter import *

def bottom_right(w=300, h=200):
    # get screen width and height
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    # calculate position x, y
    x = (screen_width - w)
    y = (screen_height-h)
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root = Tk()
bottom_right(500, 300)
root.mainloop()

推荐答案

可以使用 win32api.GetMonitorInfo() 来找到监视器,即没有任务栏的监视器区域.然后,查看任务栏的位置,您可以将窗口放置在工作区的一角.看这个例子:

You can use the win32api's .GetMonitorInfo() to find the 'working area' of the monitor, which is the area of the monitor without the taskbar. Then, seeing where the taskbar is, you can place the window in a corner of the working area. See this example:

import win32api
import Tkinter as tk

for monitor in win32api.EnumDisplayMonitors():
    monitor_info = win32api.GetMonitorInfo(monitor[0])
    if monitor_info['Flags'] == 1:
        break

work_area = monitor_info['Work']
total_area = monitor_info['Monitor']

width = 300
height = 200

side = [i for i in range(4) if work_area[i]!=total_area[i]]

# Left
if side ==[0]:
    x = str(work_area[0])
    y = str(work_area[3]-height)
# Top
elif side == [1]:
    x = str(work_area[2]-width)
    y = str(work_area[1])
# Right
elif side == [2]:
    x = str(work_area[2]-width)
    y = str(work_area[3]-height)
# Bottom
elif side == [3]:
    x = str(work_area[2]-width)
    y = str(work_area[3]-height)
else:
    x = str(work_area[2]-width)
    y = str(work_area[3]-height)

geom = '{}x{}+{}+{}'.format(width, height, x, y)

root = tk.Tk()
root.configure(background='red')
root.geometry(geom)
root.overrideredirect(True)
root.mainloop()

请注意,我已经使用 overrideredirect 去除了窗口的框架,因为这会稍微影响放置.

Note that I've used overrideredirect to get rid of the frame of the window, since this messes with the placing a bit.

这篇关于打开 Tkinter 窗口,使其位于开始菜单栏上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆