0% found this document useful (0 votes)
46 views6 pages

From Tkinter Import From Tkinter Import TTK

The document discusses various widgets that can be used in tkinter GUI applications. It provides details on 14 main widgets: Label, PhotoImage, Button, Checkbutton, Radiobutton, Entry, Combobox, Progressbar, Scale, Frame, LabelFrame, Panedwindow, Notebook, and Treeview. It describes the key attributes and functions of each widget, how they are configured and used to build GUI elements. It also briefly mentions 3 additional widgets: Spinbox, Toplevel, and Text.

Uploaded by

Joy Wagner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views6 pages

From Tkinter Import From Tkinter Import TTK

The document discusses various widgets that can be used in tkinter GUI applications. It provides details on 14 main widgets: Label, PhotoImage, Button, Checkbutton, Radiobutton, Entry, Combobox, Progressbar, Scale, Frame, LabelFrame, Panedwindow, Notebook, and Treeview. It describes the key attributes and functions of each widget, how they are configured and used to build GUI elements. It also briefly mentions 3 additional widgets: Spinbox, Toplevel, and Text.

Uploaded by

Joy Wagner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Importing the tkinter:

from tkinter import *


from tkinter import ttk

first the root window should be created using Tk() command and then add the required widgets using
ttk module. for instance:
ttk.Button(),
ttk.Label()

You may modify each widget attributes by using the square brackets or using the config() function of
each widget.

There are 3 geometry managers in tkinter:


1. Pack: Limited sets of instructions to manage widgets.
2. Grid: divides the frame or windows into rows and columns.
3. Place: Using absolute or relatives (x,y) coordinates to place widgets.

+ You can use multiple geometry manager within an application but not with a single master.

The widgets that are interactive, the command attribute will be used to add the event handler and
meanwhile other widgets like labels which are less interactive you will use bind.

Widgets using ttk:


1. Label
1.1. Attributes:
+ text: The text within the label
+ wraplength: wrap the label’s text according to the number of specified pixels.
+ justify: Adjusting the text of the label
- LEFT, CENTER, RIGHT
+ foreground: the color of the text (indicating the color by name or hex values)
+ background: the color of the label itself
+ font: format is  font = (“Font type”, size, “bold/italic”)
+ image: adding an image to the label
+ compound: Controlling the display of text and image of the label.
- text, image, center, left
2. PhotoImage
2.1. Format  PhotoImage(file=”image full path”)
2.2. subsample(x,y): shrinks the image according to the given x and y

** For having a safe image import u need to store that image as reference to label or other widgets. You
may do in such matter:
Logo = PhotoImage(file=”image full path”)
Label.img = logo
Label.config(image = label.img)
** Tkinter variable classes are: 1. BooleanVar
2. DoubleVar
3. IntVar
4. StringVar
4.1. Functions:
+ set()
+ get()

3. Button
3.1. Attributes:
+ text
+ command: takes the function which does a certain task
+ font: format is  font = (“Font type”, size, “bold/italic”)
+ image: adding an image to the label
+ compound: Controlling the display of text and image of the label.
- text, image, center, left
3.2. Functions:
+ invoke(): Simulates the pressing of the button
+ instate(): checking if the button is in which state and it returns True or False.
Format  button.instate([“!disabled”/”disabled”])

+ state(): used to change the state of the button and used as follow:
Format  button.state([“disabled”/”!disabled”])
Where, [“disabled”] will disable the button and [“!disabled”] will enable it.

4. Checkbutton
4.1. Attributes:
+ text
+ command
+ image
+ font
+ compound
+ variable: setting it to any tkinter variable classes
+ onvalue: setting the value if the box is checked/ticked
+ offvalue: setting the value if the box is not checked/ticked
+ textvariable: a variable for the text of check button

4.2. Functions:
+ invoke()
+ instate()
+ state()

5. Radiobutton
5.1. Attributes:
+ text
+ variable
+ value
6. Entry
6.1. Attributes:
+ width
+ show: replaces every character with anything character. Useful for password.
6.2. Functions:
+ get()
+ delete(idx_begin, id_end/END): to delete elements on entry based on indexes.
+ insert(idx_begin, “text”)
+ state(): used for changing the state of the entry and it has 2 states which are:
- “!disabled”/”disabled
- “!readonly”/”readonly”
+ instate()

7. Combobox
7.1. Attributes:
+ textvariable
+ values: which adds the lists of values to add as list.

8. Progressbar
8.1. Attributes:
+ orient: How it is going to be shown
Modes: HORIZONTAL, VERTICAL
+ length
+ mode: base on the load of the task
Modes: indeterminate, determinate
+ maximum: floating point for determinate mode
+ value: the value of progress which is a floating-point value
+ variable

8.2. Functions:
+ start()
+ stop()
+ step()

** start and stop functions are for indeterminate mode and meanwhile the step is for determinate
mode to increase the percentage of the progress bar.

9. Scale
9.1. Attributes:
+ orient
+ length
+ variable
+ from_
+ to
9.2. Functions:
+ set()
+ get()
10. Frame
10.1. Attributes:
+ height
+ width
+ padding: it takes a list of two value. (x, y)
+ relief: the look of the frame
Modes: 1. FLAT (default)
2. RAISED
3. SUNKEN
4. SOLID
5. RIDGE
6. GROOVE

11. LabelFrame
11.1. Attributes:
+ height
+ width
+ text

12. Panedwindow
12.1. Attributes:
+ orient: HORIZONTAL, VERTICAL
12.2. Function:
+ add(element, weight=”int value”)
+ insert(idx, element)
+ forget(idx): removes the element according to the index (the element is
hidden)
13. Notebook: creates Tabs
13.1. Function:
+ add(element, text=”str value”)
+ insert(idx, element, text=”str value”)
+ forget(idx)
+ select(): returns the ID of the selected tab by user by default
and it can select the tab by passing the index
+ index(element_ID): converts the ID number to the index of the tab
+ tab(idx, state=”str value”): changes the state of the tab.

14. Treeview(master, height)


14.1. function:
+ insert(“parent”, “idx”, “id”, text=”str value”, image)
+ move(“item id”, “parent”, “idx”)
+ item(“item id”, open=True): similar to config method but for elements of
tree view
+ detach(“item id”): removes the item but its hidden
+ delete(“item id”)
Widgets without ttk:
1. Spinbox
1. Attributes:
+ from_
+ to
+ textvariable

2. Toplevel: created a new window


2.1. Attributes:
+ master
2.2. Functions:
+ title()
+ lower()
+ left(): to bring the desired window on top of others
+ state(): changing the state of the zoom and as well returning the current state.
Modes: 1. zoomed: full screen
2. withdrawn: minimize the Win (not showing in
taskbar)
3. iconic: minimize and visible on taskbar
4. normal: normal state and small
+ iconify()
+ deiconify()
+ geometry(): changing the geometry of the window.
Format  window.geometry(‘WIDTHXHEIGHT+X+Y’)
+ resizable(Boolean_x, boolean_y): allowing resizing in x or y direction
+ maxsize(x,y)
+ minsize(x,y)
+ destroy(): closes the window and its contain widgets and child windows.

3. Text
3.1. Attributes
+ width
+ height
+ wrap: wraps the text within the element and its takes string.
Modes: 1. Char (default)
2. none (will not wrap)
3. word (wraps around the last word)
+ state: disable/normal
3.2. function
+ get(“base-format”,”base-format”): it will get the content from specified place.
Base modifiers are usually: “line.char” and “end”. For instance“1.0” means line 1
and starting character with index 0.
+ insert(“line.char + # lines”, text): insert the a text
+ delete(“line.char”)
+ replace(“line.char”, text)

You might also like