1

I have a program that when a button is clicked, I need another button to be generated. It does not have to be packed specifically.

I've tried setting the command= section equal to a method where there's a newbutton = tk.Button(root, text=fields[0]) but that didn't work.

from tkinter import *
import tkinter as tk

class MainWindow(tk.Frame):
    counter = 0
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.button = tk.Button(self, text="Create new hotlink",
                            command=self.create_window)
        self.button.pack(side="top")

    def create_window(self):
        self.counter += 1
        t = tk.Toplevel(self)
        t.wm_title("Create New Hotlink")
        fields = 'Hotlink Name', 'URL'

        def fetch(entries):
            for entry in entries:
                field = entry[0]
                text = entry[1].get()
                print('%s: "%s"' % (field, text))

        def makeform(root, fields):
            entries = []
            for field in fields:
                row = Frame(root)
                lab = Label(row, width=15, text=field, anchor='w')
                ent = Entry(row)
                row.pack(side=TOP, fill=X, padx=5, pady=5)
                lab.pack(side=LEFT)
                ent.pack(side=RIGHT, expand=YES, fill=X)
                entries.append((field, ent))
            return entries

        def button2():
            newButton = tk.Button(root, text=fields[0])

        ents = makeform(t, fields)
        t.bind('<Return>', (lambda event, e=ents: fetch(e)))
        b2 = Button(t, text='Save', command=button2())
        b2.pack(side=LEFT, padx=5, pady=5)

if __name__ == "__main__":
    root = tk.Tk()
    main = MainWindow(root)
    main.pack(side="top", fill="both", expand=True)
    root.mainloop()
  • Change `command=button2()` to `command=button2`. Possible duplicate of [why-is-button-parameter-command-executed-when-declared](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – Miraj50 Jan 21 '19 at 16:44

2 Answers2

2

Its because you're assigning not function itself but value that its returning in command=button() instead you should do:

b2 = Button(t, text='Save', command=button2)

Also in your button2() function you forgot to pack your new button:

def button2():
    newButton = tk.Button(root, text=fields[0])
    newButton.pack()

After pressing Save button new Buttons are being created

enter image description here

Filip Młynarski
  • 3,534
  • 1
  • 10
  • 22
1

The reason why this wasn't working was because the command you were calling was command=button2() when it should be command=button2. The () should not included when calling a command.

Also, as no placement for newButton was defined it was not placed anywhere. When defined using .pack it appeared.

See snipped from your code here:

    def button2():
    newButton = tk.Button(root, text=fields[0])
    newButton.pack(side=RIGHT)

ents = makeform(t, fields)
t.bind('<Return>', (lambda event, e=ents: fetch(e)))
b2 = Button(t, text='Save', command=button2)
b2.pack(side=LEFT, padx=5, pady=5)

The rest of your code is good, hence why I have only included this snippet.

JackU
  • 1,406
  • 3
  • 15
  • 43