Reputation: 2490
I cannot update the rows in Tkinter.
If I set the rows to a regular variable it doesn't not update. this is shown in the first script. If I set the rows to the IntVar type like you would do with text it refuses the data type. This is shown in the second script.
2 things to note: If you watch the counter in script 1, its going up just fine but its not being applied. If you use self.activeRow.get() instead of self.activeRow it will effectively turn it into a normal variable with the same results shown in script 1.
Script 1
from tkinter import *
class Example(Frame):
def move(self):
self.activeRow += 1
print(self.activeRow)
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.columnconfigure(0, pad=0)
self.columnconfigure(1, pad=0)
self.columnconfigure(2, pad=0)
self.rowconfigure(0, pad=0)
self.rowconfigure(1, pad=0)
self.rowconfigure(2, pad=0)
Label(self, text= 'row 0').grid(row=0, column=0)
Label(self, text= 'row 1').grid(row=1, column=0)
Label(self, text= 'row 2').grid(row=2, column=0)
#regular variable
self.activeRow = 0
b = Button(self, text="normal variable {0}".format(self.activeRow), command=self.move)
b.grid(row=self.activeRow, column=1)
self.pack()
def main():
root = Tk()
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
Script 2
from tkinter import *
class Example(Frame):
def move(self):
self.activeRow.set(self.activeRow.get() + 1)
print(self.activeRow.get())
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.columnconfigure(0, pad=0)
self.columnconfigure(1, pad=0)
self.columnconfigure(2, pad=0)
self.rowconfigure(0, pad=0)
self.rowconfigure(1, pad=0)
self.rowconfigure(2, pad=0)
Label(self, text= 'row 0').grid(row=0, column=0)
Label(self, text= 'row 1').grid(row=1, column=0)
Label(self, text= 'row 2').grid(row=2, column=0)
#Tkinter IntVar
self.activeRow = IntVar()
self.activeRow.set(0)
b = Button(self, text="IntVar", command=self.move)
b.grid(row=self.activeRow, column=1)
self.pack()
Upvotes: 0
Views: 1952
Reputation: 4604
If you want to move an existing widget, you have to call again the grid
method to update this widget (ie widget.grid(row=other_value)
). To remove a widget, you can use the grid_forget()
method.
from Tkinter import *
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.initUI()
def move(self):
info = self.b.grid_info()
previous_row = int(info["row"]) #int() needed because datas are stored as string
self.b.grid(row=previous_row+1)
def initUI(self):
for i in range(5):
l = Label(self, text="Row {0}".format(i))
l.grid(row=i, column=0)
self.b = Button(self, text="Moving button", command=self.move)
self.b.grid(row=0, column=1)
self.pack()
root = Tk()
app = Example(root)
root.mainloop()
Upvotes: 1
Reputation: 4604
You may use either a regular python variable or a Tkinter variable. Below are two working examples.
Tkinter variables classes are variables which changes can be "traced" (ie you may be notified that values have changed). They are used with widgets with values (Scale, Entry...) for instance to retrieve values, or to sync two widgets.
def initUI(self):
#regular variable
self.activeRow = 0
for i in range(5):
b = Button(self, text="normal variable {0}".format(self.activeRow))
b.grid(row=self.activeRow, column=0)
self.activeRow += 1
#Tkinter IntVar
self.activeRow = IntVar()
for i in range (5):
b = Button(self, text="IntVar {0}".format(self.activeRow.get()))
b.grid(row=self.activeRow.get(), column=1)
self.activeRow.set(self.activeRow.get() + 1)
Upvotes: 0