Reputation: 2332
I have code similar to the following
import tkinter as tk
from tkinter import Tk
from tkinter import ttk
root = Tk()
ttk.Label(root, text = 'label text', justify = 'center').grid(row=0, column=0)
string = tk.StringVar()
string.set('entry text')
ttk.Entry(root, textvariable = string).grid(row=0, column=1)
mainButton = ttk.Button(root, text = 'BUTTON').grid(row = 1, columnspan = 2)
root.pack()
Effectively this makes the first row have a label and a text box next to it, on the same row, and then a button below both of those. Now, when that button is pressed, i want to add a new label/entry in the row containing the button and move the button down a row. Is there a feasible way to do this?
Upvotes: 0
Views: 6724
Reputation: 101
I have fixed W1ll1amvl's code so it would function better!
from tkinter import ttk
from tkinter import Tk, Label, Button, StringVar, Entry
root = Tk()
Label(root, text = 'label text', justify = 'center').grid(row=0, column=0)
string = StringVar()
string.set('entry text')
Entry(root, textvariable = string).grid(row=0, column=1)
def clicked():
Label(root, text = 'label text 2', justify = 'center').grid(row=1, column=0)
string2 = StringVar()
string.set('entry text')
Entry(root, textvariable = string).grid(row=1, column=1)
mainButton = Button(root, text = 'BUTTON', command = clicked)
mainButton.grid(row = 2, columnspan = 2)
# mainbutton row=2 will automatically sit at row 1 until a new row 'pushes' it down
Upvotes: 0
Reputation: 10532
Using grid_forget
, you can remove the button and later place it again using grid
on a new position. I've demonstrated this in the example below.
I have made your program into a class
, so you can easily pass around variables such as string through self
. As you can see, I have appended every new StringVar
to self.string
. This way you can retrieve the entry text for each widget using self.string[index].get()
import tkinter as tk
from tkinter import ttk
class App():
def __init__(self):
self.root = tk.Tk()
ttk.Label(self.root, text = 'label text', justify = 'center').grid(row=0, column=0)
self.string = [tk.StringVar()]
self.string[0].set('entry text')
ttk.Entry(self.root, textvariable = self.string[0]).grid(row=0, column=1)
self.mainButton = ttk.Button(self.root, text = 'BUTTON', command=self.callback)
self.mainButton.grid(row = 1, columnspan = 2)
self.root.mainloop()
def callback(self):
self.mainButton.grid_forget()
self.string.append(tk.StringVar())
l = len(self.string)
self.string[l-1].set('entry text')
ttk.Label(self.root, text = 'label text', justify = 'center').grid(row=l-1, column=0)
ttk.Entry(self.root, textvariable = self.string[l-1]).grid(row=l-1, column=1)
self.mainButton.grid(row = l, columnspan = 2)
App()
Upvotes: 6
Reputation: 1269
This should work. All you do is set a command for the button, and write a function for it. If you say that the original code works, then this should as well.
import tkinter as tk
from tkinter import Tk
from tkinter import ttk
root = Tk()
ttk.Label(tab, text = 'label text', justify = 'center').grid(row=0, column=0)
string = tk.StringVar()
string.set('entry text')
ttk.Entry(tab, textvariable = string).grid(row=0, column=1)
def clicked():
ttk.Label(tab, text = 'label text 2', justify = 'center').grid(row=1, column=0)
string2 = tk.StringVar()
string.set('entry text')
ttk.Entry(tab, textvariable = string).grid(row=1, column=1)
mainButton = ttk.Button(mainTab, text = 'BUTTON', command = clicked)
mainbutton.grid(row = 2, columnspan = 2)
# mainbutton row=2 will automatically sit at row 1 until a new row 'pushes' it down
root.pack()
Upvotes: 0