Keegan Keplinger
Keegan Keplinger

Reputation: 627

calling tkinter's canvas with a position

When you call tkinter's canvas, you define a height and width:

w = Canvas(master, width=ScreenWidth, height=ScreenHeight)

To do this, I first get the user's screen resolution, then use that for height and width. However, on windows systems, this overlaps the task bar. So I try to remedy this by shortening the height. Unfortunately, Canvas is defined from the the bottom, so this just cleaves the top of the canvas down, rather than the bottom up. Ideally, I would simply cleave a whole taskbar-sized chunk around the whole canvas to account for users who place their task bar elsewhere.

Is there a way to call canvas with a position so that I can offset it from the task bar in windows OS (or another solution to the general problem of taskbar overlap)?

note: I have tried:

w.pack(side="top")

and the following similar question didn't seem to help with my situation

Positioning Canvas in window - Tkinter/python

Upvotes: 0

Views: 3131

Answers (2)

Rushy Panchal
Rushy Panchal

Reputation: 17532

To position it in the center, as @Bryan Oakley stated, use something along the lines of:

# note: 'master' is a tk.Toplevel() or a tk.Tk()

w = Canvas(master, width=ScreenWidth, height=ScreenHeight)
w.pack()

master.geometry("+{xPos}+{yPOS}".format(xPos = 0, yPos = 0))

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385860

There is no way to place a canvas relative to any position on a screen, per se. A canvas is not an independent window. All you can do is place it somewhere relative to it's containing window.

However, you have absolute control over the topmost window (either an instance of Toplevel, or an instance of Tk) using the geometry method. That method allows you to specify the width and height of the window, and the location of the window on the screen.

So, if you're trying to put a canvas at a specific location, you must put the window at that location, then make sure the canvas fills the window.

Upvotes: 2

Related Questions