Python Forum

Full Version: tkinter button commands
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am writing code in tkinter (on a mac, in Python 3.6)
Right now, I am writing some code that basically waits until a button is pressed, then opens a new window. I know a button can be hooked up to a function, but I don't want to write all the code for this new window inside of a new function. Is there any other way to do this?


More Info:
Right now, my code looks like this:
select = Tk()

var = IntVar()

for index in range(0, len(worlds)):
    Radiobutton(select, text=worlds[index], variable=var, value=index, font=('Courier New', 20)).pack(anchor=W)

def selWorld(num):
    select.quit()
    ## this will open a new tkinter window, but I don't want to write all my code for that window in this function.

okButton = Button(select, text=' Begin Editing ', command=selWorld(var), font=("Courier New", 20))
okButton.pack(anchor=W)

select.mainloop()
Sorry if anything was unclear.
you need to bind the button to the function you want to call.
def clear_entry():
    ...

self.btn = Button(self.CallSubFrame, text='Clear Item', bg='LightBlue')
self.btn.grid(row=x, column=y)
self.btn.bind('<Button-1>', clearEntry)
see: http://infohost.nmt.edu/tcc/help/pubs/tk...utton.html

You won't need the self. if your code is not part of a class