user2242044
user2242044

Reputation: 9213

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

Upvotes: 0

Views: 1372

Answers (2)

fhdrsdg
fhdrsdg

Reputation: 10532

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.

Upvotes: 2

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19144

What you are calling the 'start menu bar' is usually called the taskbar, at least on Windows. root.iconify() minimizes the root window to the taskbar, wherever it happens to be, just as when one clicks [_] in the upper right of the window. Clicking on the icon de_iconifies it, just as with any other app.

root = tk.Tk()
root.iconify()
<build gui>
root.deiconify()
root.mainloop()

is a common pattern in a polished app when the part takes long enough to cause potentially visible construction activity. I believe putting the gui in a frame and packing the frame as the last step has the same effect (of hiding construction).

Upvotes: 1

Related Questions