The document provides an overview of three Python GUI widgets: ListBox, CheckBox, and ComboBox. It explains their functionalities, optional parameters, common methods, and includes example code for usage. Additionally, it differentiates between these widgets and their typical applications in user interfaces.
The document provides an overview of three Python GUI widgets: ListBox, CheckBox, and ComboBox. It explains their functionalities, optional parameters, common methods, and includes example code for usage. Additionally, it differentiates between these widgets and their typical applications in user interfaces.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17
PYTHON
lesson OTHER CONTROLS:
10: LISTBOX, CHECKBOX, COMBOBOX List box The ListBox widget is used to display different types of items. These items must be of the same type of font and having the same font color. The items must also be of Text type. The user can select one or more items from the given list according to the requirement. optional parameters Optional parameters root – root window. bg – background colour fg – foreground colour bd – border height – height of the widget. width – width of the widget. font – Font type of the text. highlightcolor – The colour of the list items when focused. yscrollcommand – for scrolling vertically. xscrollcommand – for scrolling horizontally. cursor – The cursor on the widget which can be an arrow, a dot etc. Common methods yview – allows the widget to be vertically scrollable. xview – allows the widget to be horizontally scrollable. get() – to get the list items in a given range. activate(index) – to select the lines with a specified index. size() – return the number of lines present. delete(start, last) – delete lines in the specified range. nearest(y) – returns the index of the nearest line. curseselection() – returns a tuple for all the line numbers that are being selected. example code: checkbox Heckboxes, also known as tickboxes or tick boxes or check boxes, are widgets that permit the user to make multiple selections from a number of different options. This is different to a radio button, where the user can make only one choice. Usually, checkboxes are shown on the screen as square boxes that can contain white spaces (for false, i.e not checked) or a tick mark or X (for true, i.e. checked). simple example The following example presents two checkboxes "male" and "female". Each checkbox needs a different variable name (IntVar()). We can improve this example a little bit. First we add a Label to it. Furthermore we add two Buttons, one to leave the application and the other one to view the values var1 and var2. We can improve this example a little bit. First we add a Label to it. Furthermore we add two Buttons, one to leave the application and the other one to view the values var1 and var2. combobox Combobox allows users to select from a predefined list of options and also provides the flexibility to enter values that are not in the list.
Combobox widget is part of ttk, so we have to import ttk
otherwise we will get this error. combobox Tkinter Combobox to select Option or add data by user & reading adding and setting default options
By using cb1.set('Apr') we are setting a default selected
option for the Combobox. We can use the index of the option cb1.current(2) to set it as default selection. getting value and index of selected option Here cb1 is our Combobox object To get the selected value ( option ) we can use cb1.get() To get the index of the selected option we can use cb1.current()
Here on click of the button b1 the value of the Combobox cb1
is set to Apr then this value and index is collected and displayed in the Label l1 The output of cb1.current() is an integer so we used str() to convert the same to string before adding. getting value and index of selected option Here cb1 is our Combobox object To get the selected value ( option ) we can use cb1.get() To get the index of the selected option we can use cb1.current()
Here on click of the button b1 the value of the Combobox cb1
is set to Apr then this value and index is collected and displayed in the Label l1 The output of cb1.current() is an integer so we used str() to convert the same to string before adding. getting value and index of selected option differentiate In Python GUI programming, a ListBox is a widget that displays a list of items, often with scrollbars for navigation. It allows users to select one or more items from the list. This makes it useful for presenting a set of options where users can make multiple selections, such as selecting multiple files from a list. On the other hand, a CheckBox is a widget that represents a binary choice, typically displayed as a small box that can be checked or unchecked. differentiate It is commonly used for enabling or disabling options, or for selecting multiple options independently of each other. Lastly, a ComboBox combines a button or editable field with a drop-down list of items. It shows a single item initially and expands into a list when clicked, allowing users to select one item from the list. This makes it suitable for cases where the list of options is long or when space is limited, providing a compact and user-friendly way to present choices. THANK YOU!