Home > Software engineering >  tkinter- how to increase button's size repeatedly until reaching window's max size?
tkinter- how to increase button's size repeatedly until reaching window's max size?

Time:06-22

My code should do the following:
Start button starts to increase its size by one pixel when clicked
When reached to the window's size, it returns to its initial size. Can't really do the last part

from tkinter import *
def increase():
    global counter, x, y
    x  = 1
    y  = 1
    b1.config(padx=x, pady=y)
    b1.after(100, increase)

root=Tk()
x=5
y=2
# buttons
b1=Button(root, text="Start", bg="blue",
          command=increase, width=x, height=y)
b1.place(x=10,y=10)
b2=Button(root, text="Stop",  command=quit)
b2.place(x=10, y=260)

root.geometry("320x300")
root.resizable(False, False)
root.mainloop()

CodePudding user response:

Something like this ?

#!/usr/bin/python3

from tkinter import *
def increase():
    global counter, x, y, root
    x  = 3
    y  = 3
    window_height=root.winfo_height()
    window_width=root.winfo_width()
    if x >= window_height:
        x=0
    if y >= window_width:
        y=0
    b1.config(padx=x/3, pady=y/3)
    b1.after(100, increase)

root=Tk()
x=5
y=2
# buttons
b1=Button(root, text="Start", bg="blue",
          command=increase, width=x, height=y)
b1.place(x=10,y=10)
b2=Button(root, text="Stop",  command=quit)
b2.place(x=10, y=260)

root.geometry("320x300")
root.resizable(False, False)
root.mainloop()

There are some differences on the button size, if you use width/height or padx/pady. I tried adapting the offset for the padx/pady.

CodePudding user response:

You can adapt this code using to your needs. It is not the best way to do it but it will do the work.

from tkinter import *

root = Tk()

count = 0


def reset_count():
    global count
    count = 0


def btn_size_changer():
    global count
    if count <= 21:
        if btn['width'] < 50 and btn['height'] < 40:
            btn['width']  = 2
            btn['height']  = 1
            root.after(100, btn_size_changer)
            count  = 1
    else:
        if btn['width'] >= 7 and btn['height'] >= 3:
            btn['width'] -= 2
            btn['height'] -= 1
            root.after(100, btn_size_changer)
        else:
            reset_count()


btn = Button(root, text="Click ME", command=lambda: btn_size_changer(), width=6, height=3)
btn.pack(anchor=CENTER)


root.geometry('350x350')
root.mainloop()
  • Related