I am aiming for make GUI that changes depending on the canvas size. I need to be able to actively check the window size so i know when to display extras. Using Python 3.8.2 GuiZero
3 Answers
You can use tkinter
event <Configure>
on the canvas:
def on_resize(event):
print(app.height)
...
app.tk.bind('<Configure>', on_resize)

- 40,144
- 5
- 22
- 34
-
It seems to be off when you first start it. – PurpleLlama Mar 26 '20 at 14:02
-
I don't understand what you mean. – acw1668 Mar 26 '20 at 15:03
-
After the code runs it gives the wrong number in-tell it its resize guess i could set a min size so it only changes after the increase than the start number should not matter. – PurpleLlama Mar 26 '20 at 15:10
-
1I have updated my answer using `app.height` instead of `event.height`. – acw1668 Mar 27 '20 at 02:57
-
Perfect and a lot faster – PurpleLlama Mar 27 '20 at 13:55
I was finally able to make something but it does throw and error after the app quits.
w=None
while True:
x=app.tk.winfo_height()
if x!=w:
print(x)
w=app.tk.winfo_height()
app.update()

- 168
- 1
- 2
- 14
I came across this problem when creating a digital etch-a-sketch, using a Raspberry Pi and two potentiometers as the horizontal and vertical controls. How to get the current size of the canvas? Annoyingly when you set height and width to "fill" and then try to interrogate these values all you get is "fill", which is no use if you're trying to determine the upper bounds of the available canvas. I dug down into the object hierarchy and discovered that .tk_winfo_height() and .tk.winfo_width() return integer values. For this purpose I've removed the code that reacts to the twiddling of the potentiometers and put a row of buttons at the bottom of the screen to control the vertical and horizontal movement.
from guizero import App, Box, Drawing, PushButton
x = 0
y = 0
def clear_screen():
drawing.clear()
def move_left():
global x, y
if x > 0 :
drawing.line(x, y, x - 1, y)
x = x - 1
def move_right():
global x, y
if x < drawing.tk.winfo_width() :
drawing.line(x, y, x + 1, y)
x = x + 1
def move_up():
global x, y
if y > 0 :
drawing.line(x, y, x, y - 1)
y = y - 1
def move_down():
global x, y
if y < drawing.tk.winfo_height() :
drawing.line(x, y, x, y + 1)
y = y + 1
app = App()
drawing = Drawing(app, height="fill", width="fill")
drawing.bg="white"
bbox = Box(app, align="bottom")
lbtn = PushButton(bbox, align="left", command=move_left, text="Left")
ubtn = PushButton(bbox, align="left", command=move_up, text="Up")
cbtn = PushButton(bbox, align="left", command=clear_screen, text="Clear")
rbtn = PushButton(bbox, align="left", command=move_right, text="Right")
dbtn = PushButton(bbox, align="left", command=move_down, text="Down")
app.display()

- 1,183
- 10
- 10