How to pass arguments to a Button command in Tkinter
In this tutorial, we are going to learn about how to pass arguments using the button. For this tutorial, we are going to use the Tkinter library.
There are many modules in Python that provide Graphical User Interface(GUI). Tkinter is the module used most commonly for Graphical User Interface(GUI). This module provides many features that are used most commonly like Button, Message Box, etc… Using Tkinter we can create these so fast and easily.
To do this we will be following these steps.
- Creating a Tkinter Object.
- Create a window using Geometry.
- Add a button to the window.
- Use mainloop() to call this window endless.
We will use Lamba for this.
Lambda function: pass arguments to a Button command in Tkinter
#import libraries from tkinter import * #creating a function to call by button def display(args): print(args) #Creating tkinter object tkobj = Tk() #setting dimensions tkobj.title("Codespeedy") tkobj.geometry("400x400") #Creating button bt = Button(tkobj,text="Click me",command=lambda: display("Welcome to codespeedy!!"),pady=100) bt.pack() #looping tkobj.mainloop()
Output:
Leave a Reply