Tkinter Button and Entry in Python

Hello friends how are you, today in this blog i will teach you what is Button and Entry, syntax of Button and Entry, how to create Button and Entry, how Button and Entry work in a very simple way.

Let's start

Button

  • Button widget is used to provide different kinds of buttons in application.
  • Syntax:
    btn = Button ( master, option1,option2, ... )
    
  • Here master is parent window and option can be used as a key-value pairs.
  • Examples of options are text,bg,image,height etc.

from tkinter import *
top = Tk()
#window dimension
top.geometry("300x150")
top['bg']="#51E1DC"
# creating button
btn = Button(top, text="Click Me").pack()
top.mainloop()
 



from tkinter import *
top = Tk()
#window dimension
top.geometry("300x150")
top['bg']="#51E1DC"
#here x is distance from left
#and y is distance from top
btn1 = Button(top, text="Click Me").place(x=50,y=30)
top.mainloop()



from tkinter import messagebox
from tkinter import *
top = Tk()
#window dimension
top.geometry("300x150")
top['bg']="#51E1DC"
#creating function
def myfun():
    messagebox.showinfo("Title","You clicked on button")
btn1 = Button(top, text="Click Me",command=myfun).pack()
top.mainloop()




 Entry

  • Entry is used to create single line textbox which is used to take accept a value from the user.
  • Syntax:
    ent = Entry ( master, option1,option2, ... )
    
  • Here master is parent window and option can be used as a key-value pairs.
  • Examples of options are bg,font,width etc.

from tkinter import *
top = Tk()
#window dimension
top.geometry("300x150")
top['bg']="#51E1DC"
label = Label(top, text="First Number",).place(x=50,y=50)
#create text box
ent=Entry(top).place(x=150,y=50)
top.mainloop()
 



from tkinter import  messagebox
from tkinter import *
top = Tk()
#window dimension
top.geometry("300x180")
top['bg']="#51E1DC"
#defining function
def add():
    f=firstNum.get()
    s=secondNum.get()
    messagebox.showinfo("Sum",(f+s))
#declaring variables
firstNum=IntVar()
secondNum=IntVar()
#create labels
Label(top, text="First Number",width="13").place(x=50,y=50)
Label(top, text="Second Number",width="13").place(x=50,y=90)
#create text boxes
Entry(top,textvariable=firstNum).place(x=150,y=50)
Entry(top,textvariable=secondNum).place(x=150,y=90)
#create button
Button(top,text="Add",width="5",bg="orange",command=add).place(x=100,y=120)
top.mainloop()



Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks.😊

Post a Comment

0 Comments