How to handle a button click event

I am just learning Python, and I have a basic concept, and already a few command line programs. Now I'm learning how to create graphical interfaces using Tkinter.

I created a simple graphical interface to accept some user information from the Entry widget, and then when the user clicks the submit button, he should open a dialog.

The dialog should contain the first and last name.

The problem is that I don’t know how to handle the event when the user clicks the submit button.

Here is my code:

 from Tkinter import * class GUI(Frame): def __init__(self,master=None): Frame.__init__(self, master) self.grid() self.fnameLabel = Label(master, text="First Name") self.fnameLabel.grid() self.fnameEntry = Entry(master) self.fnameEntry.grid() self.lnameLabel = Label(master, text="Last Name") self.lnameLabel.grid() self.lnameEntry = Entry(master) self.lnameEntry.grid() self.submitButton = Button(self.buttonClick, text="Submit") self.submitButton.grid() def buttonClick(self, event): """ handle button click event and output text from entry area""" pass if __name__ == "__main__": guiFrame = GUI() guiFrame.mainloop() 
+4
source share
3 answers

You already had an event function. Just return the code:

  """Create Submit Button""" self.submitButton = Button(master, command=self.buttonClick, text="Submit") self.submitButton.grid() def buttonClick(self): """ handle button click event and output text from entry area""" print('hello') # do here whatever you want 

This is the same as @Freak's answer, with the exception of the buttonClick() method, now outside the __init__ class method. The advantage is that in this way you can trigger the action programmatically. This is the usual way in the GUI with OOP.

+5
source

You must specify a handler or function to call when the button is clicked. You can do this by assigning the name (non-calling function) of the function to the command property of your button.

For instance:

 self.submitButton = Button(self.buttonClick, text="Submit", command=buttonClick) 

Note the lack of () when assigning buttonClick as the command property for self.submitButton .

Note that you do not need the second event parameter in your buttonClick() handler / function.

+1
source

I found a pretty good link called Thinking in Tkinter , and I hammered it a bit. I tried to make him fit what you wanted.

 from tkinter import * class GUI(Frame): def __init__(self,master=None): Frame.__init__(self, master) self.grid() self.fnameLabel = Label(master, text="First Name") self.fnameLabel.grid() self.fnameEntry = StringVar() self.fnameEntry = Entry(textvariable=self.fnameEntry) self.fnameEntry.grid() self.lnameLabel = Label(master, text="Last Name") self.lnameLabel.grid() self.lnameEntry = StringVar() self.lnameEntry = Entry(textvariable=self.lnameEntry) self.lnameEntry.grid() def buttonClick(): print("You pressed Submit!") print(self.fnameEntry.get() + " " + self.lnameEntry.get() +", you clicked the button!") self.submitButton = Button(master, text="Submit", command=buttonClick) self.submitButton.grid() if __name__ == "__main__": guiFrame = GUI() guiFrame.mainloop() 
+1
source

All Articles