Home > Back-end >  Zoomable window with buttons in Tkinter
Zoomable window with buttons in Tkinter

Time:04-06

I'm trying to make a Tkinter window that has many buttons in it and it is zoomable/moveable. This is my current code but I don't know how to let users zoom with a mouse scroll or a button or something like that :

from tkinter import *
main = Tk()
main.geometry("500x500")

for y in range(50):
    for x in range(50):
        exec(f"buttonx{x}y{y} = Button(main, text=\"\", borderwidth=0.5).place(x={x*10}, y={y*10}, height=10, width=10)")

main.mainloop()

Output

CodePudding user response:

For ways to zoom, keys or mouse, you might want to take a look at bind(). Events and Bindings

As for the creation of the buttons I would suggest a more common way of saving the button references using a two-dimensional list:

field = []
for y in range(50):
    row = []
    for x in range(50):
        b = Button(main, borderwidth=0.5)
        b.place(x=x*10, y=y*10, height=10, width=10)
        row.append(b)
    field.append(row)
    

This makes it easier to access the buttons.

But why buttons though? When I run this program it's really slow. Does it have to be buttons or are you planning to have something else there later?

  • Related