w3resource

Python tkinter widgets Exercise: Create a Listbox bar widgets using tkinter module


Write a Python GUI program to create a Listbox bar widgets using tkinter module.

Sample Solution:

Python Code:

import tkinter as tk
parent = tk.Tk()
parent.geometry("250x200")
label1 = tk.Label(parent,text = "A list of favourite languages...")
listbox = tk.Listbox(parent)
listbox.insert(1,"PHP")
listbox.insert(2, "Python")
listbox.insert(3, "Java")
listbox.insert(4, "C#")
label1.pack()
listbox.pack()
parent.mainloop()

Explanation:

In the exercise above -

  • import tkinter as tk - Import the Tkinter library (tkinter).
  • parent.geometry("250x200") - Create the main Tkinter window (parent) and set its initial size to 250x200 pixels.
  • label1 = tk.Label(parent,text = "A list of favourite languages...") - Create a Label widget (label1) with the text "A list of favorite languages...".
  • listbox = tk.Listbox(parent) - Create a Listbox widget (listbox) to display a list of items.
  • Insert four items ("PHP," "Python," "Java," "C#") into the Listbox.
  • Use the pack() method to display the Label and Listbox widgets within the window.
  • Start the Tkinter main loop to run the GUI application, displaying the labeled list of favorite languages.

Sample Output:

Tkinter: Create a Listbox bar widgets using tkinter module

Flowchart:

Flowchart: Create a Listbox bar widgets using tkinter module

Python Code Editor:


Previous: Write a Python GUI program to create a Progress bar widgets using tkinter module.
Next: Python GUI Program: Creating a tabbed interface with Tkinter.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.