Tkinter Listbox and Menubutton in Python

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

Let's start 

Listbox

  • Listbox is used to display list of items to the user.
  • Syntax:
    l = Listbox ( 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,height etc.

from tkinter import *
top = Tk()
#window dimension
top.geometry("300x150")
top['bg']="#51E1DC"
Label(top,text = "My Favourite Fruits").pack()
listbox=Listbox(top,height="20")
listbox.insert(1,"Apple")
listbox.insert(2,"Orange")
listbox.insert(3,"Cherry")
listbox.insert(4,"Mango")
listbox.pack()
top.mainloop()
 


from tkinter import *
top = Tk()
top['bg']="#51E1DC"
top.geometry("300x250")
lbl = Label(top, text="My Favourite Fruits",bg="Orange",width="200").pack()
listbox = Listbox(top)
listbox.insert(1, "Apple")
listbox.insert(2, "Orange")
listbox.insert(3, "Cherry")
listbox.insert(4, "Mango")
# delete selected item from list
#by default it deletes from top if not selected any item
btn = Button(top, text="delete",bg="orange" ,command=lambda listbox=listbox: listbox.delete(ANCHOR))
listbox.pack(pady=5)
btn.pack()
top.mainloop()




 TKinter Menubutton

  • It acts like a drop down that contains a number of list.
  • When user clicks on menubutton it displays the lists.
  • Syntax:
    m = Menubutton ( 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,height etc.

from tkinter import *
top = Tk()
top.geometry("200x150")
top['bg']="#51E1DC"
mb = Menubutton(top, text="Programming",bg="navy",fg="white", relief=GROOVE)
mb.grid()
mb.menu = Menu(mb)
mb["menu"] = mb.menu
mb.menu.add_checkbutton(label="C")
mb.menu.add_checkbutton(label="C++")
mb.menu.add_checkbutton(label="JAVA")
mb.menu.add_checkbutton(label="PYTHON")
mb.pack()
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