1

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

PurpleLlama
  • 168
  • 1
  • 2
  • 14

3 Answers3

1

You can use tkinter event <Configure> on the canvas:

def on_resize(event):
    print(app.height)

...
app.tk.bind('<Configure>', on_resize)
acw1668
  • 40,144
  • 5
  • 22
  • 34
0

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()
PurpleLlama
  • 168
  • 1
  • 2
  • 14
0

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()
Clarius
  • 1,183
  • 10
  • 10