I want to create button and if pressed it - creates a new but and so on (infinity cycle basically). But it works just one time - create only one additional button.
from tkinter import *
class Application(Frame):
rw = 0
cl = 0
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.btn1 = Button(self, text = 'Create a new button')
self.btn1.grid(row = 0, column = 0, sticky = W)
self.btn1['command'] = self.new_button
def new_button(self):
rw = 1
cl = 1
self.btn1 = Button(self, text = 'Create a new button')
self.btn1.grid(row = rw, column = cl, sticky = W)
rw+=1
cl+=1
root = Tk()
root.title('King of the Kings!')
root.geometry('400x205')
app = Application(root)
root.mainloop()
Cheers!
Here are a few changes in your code. I've made
rw
andcl
instance variables and also removed the linesrw = 1
andcl = 1
because every time the function is called, it creates a new button but places it directly above the first button at position (1, 1).