The Mob
The Mob

Reputation: 431

tk.Button - Assign variable to an instance spawned by tk.Button(command)

Can someone please explain how I can assign a variable name to a tk.Button command that spawns a class instance? Currently stuck at..

   times_button=tk.Button(master,text="NEW",command=newToplevelWindow)

In my head I am looking for something like (pardon the syntax)..

   times_button=tk.Button(master,text="NEW",command=newWindowInstance = newToplevelWindow())

My goal is to have a series of buttons on a root window that once clicked will create a new instance of the tk.Toplevel() class which I can then play around with and tailor.

  1. Press one of the new window buttons
  2. Create newWindowInstance
  3. Call methods on that newly created instance i.e newWindowInstance.geometry("AxB")

Upvotes: 0

Views: 43

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385830

Button commands don't return anything (or more accurately, the code that runs the button command ignores whatever the command returns).

Unless there is a really compelling reason to do otherwise, it's best to have your buttons call a regular function or method. It makes writing, reading, debugging, and maintaining the code easier.

def createNewToplevelWindow():
    global newWindowInstance
    newWindowInstance = newToplevelWindow()

times_button=tk.Button(master,text="NEW",command=createNewToplevelWindow)

Upvotes: 1

Related Questions