Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

python - Open Tkinter Window so it sits on start menu bar

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.

My Code:

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...