In this Python TKinter article i want to show you How to Create MultiChoiceBox in Python TKinter. so first of all let’s talk that What is MultiChoiceBox in Python TKinter?
What is MultiChoiceBox In Python TKinter?
Multi Choice Box refers to a dialog box that prompts the user with a question and provides multiple choices for their response. It’s implemented using the messagebox module in Tkinter, specifically the askyesnocancel function.
How To Create MultiChoiceBox In Python TKinter?
For creating a custom multi-choice selection interface in Python Tkinter, you can use a combination of widgets like Checkbuttons, Listbox, or Combobox along with appropriate event handling to manage user selections. This is a basic example demonstrating how to create a multi-choice:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
from tkinter import * from tkinter import ttk from tkinter import messagebox as msg class Root(Tk): def __init__(self): super(Root, self).__init__() # Set title of the window self.title("Codeloop.org - Tkinter MultiChoiceBox") # Set window icon self.iconphoto(True, PhotoImage(file='codeloop.png')) # Call method to create the button self.button() def button(self): # Create button widget to open the multi-choice box self.btn = ttk.Button(self, text="Open Multi Choice Box", command=self.choicBox) self.btn.grid(column=0, row=0) def choicBox(self): # Display messagebox with a yes/no/cancel choice answer = msg.askyesnocancel("Multi Choice Box", "Do you like codeloop.org website?") # If 'Yes' is selected, quit the application if answer == True: self.quit() # Create an instance of the Root class root = Root() root.mainloop() |
These are the imports that we need for this article, basically it is tkinter library, also we are going to import ttk from tkinter with messagebox from tkinter .
1 2 3 |
from tkinter import * from tkinter import ttk from tkinter import messagebox as msg |
So at the top we have our Root class that inherits from TK class, and in that class we have added some requirements for the window like title, size and also icon.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Root(Tk): def __init__(self): super(Root, self).__init__() # Set title of the window self.title("Codeloop.org - Tkinter MultiChoiceBox") # Set window icon self.iconphoto(True, PhotoImage(file='codeloop.png')) # Call method to create the button self.button() |
This is the button method, because i want when a user click this button iam going to show a MultiChoiceBox with different choices. you can see that we have connected this method with our def choiceBox() method.
1 2 3 |
def button(self): self.btn = ttk.Button(self, text = "Open Multi Choice Box", command = self.choicBox) self.btn.grid(column = 0, row = 0) |
This is the process of MultiChoiceBox creation.
1 2 3 4 5 6 7 |
def choicBox(self): # Display messagebox with a yes/no/cancel choice answer = msg.askyesnocancel("Multi Choice Box", "Do you like codeloop.org website?") # If 'Yes' is selected, quit the application if answer == True: self.quit() |
Run the code and this will be the result
FAQs:
What is Python Tkinter used for?
Python Tkinter is standard GUI (Graphical User Interface) toolkit for Python. It provides different built-in widgets and functions that allow you to create desktop applications with graphical interfaces. Tkinter is commonly used for developing applications with buttons, menus, text boxes, and other graphical elements to interact with users.
What GUI is best for Python?
Choice of the best GUI toolkit for Python depends on different factors such as project requirements, familiarity with the toolkit, platform compatibility and personal preferences. Some popular GUI libraries for Python along side with Tkinter is PyQt (PyQt5/PyQt6), wxPython, Kivy, PyGTK and PySide6 (Qt for Python). Each Python GUI Framework has its strengths and weaknesses.
What does Tk() mean in Python?
In Python Tkinter, Tk() is a constructor function used to create new Tkinter application window or a root window. It initializes an instance of the Tk class, and it is our main window of a Tkinter application. This window serves as the container for all other widgets and graphical elements in the GUI application.
Subscribe and Get Free Video Courses & Articles in your Email