Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...