Python Forum
tkinter button commands
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter button commands
#1
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.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 978 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 846 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,766 May-25-2023, 07:37 PM
Last Post: deanhystad
  Can't get tkinter button to change color based on changes in data dford 4 3,419 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 5,531 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,011 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,624 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 4,042 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,558 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  TKinter restarting the mainloop when button pressed zazas321 7 16,345 Jan-26-2021, 06:38 AM
Last Post: zazas321

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020