Tkinter Widgets

 

Tkinter Widgets

Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI application.

These controls are commonly called widgets.

  • Button

See buttons.py

The Button widget is used to display buttons in your application.

my_button = Button(root, text = “Click”)

  • Canvas

See canvas.py

The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in 

your application.

my_canvas = Canvas(root, width=300, height=can_height, bg='white')


The Canvas widget can support the following standard items −


arc − Creates an arc item, which can be a CHORD, a PIESLICE or a ARC. Default is PIESLICE.

my_canvas.create_arc(x1y1x2, y2, style=CHORD, start=0, extent=150, fill="blue")

x1y1x2, y2 is the 2 points that would create a rectangle to hold the oval that the arc is part of.


image − Creates an image item, which can be an instance of either the BitmapImage or 

the PhotoImage classes.

filename = PhotoImage(file = "sunshine.gif")

my_canvas.create_image(50, 50, anchor=NE, image=filename)


line − Creates a line item.

my_canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)


oval − Creates a circle or an ellipse at the given coordinates. It takes two pairs of coordinates; 

the top left and bottom right corners of the bounding rectangle for the oval.

my_canvas.create_oval(x0, y0, x1, y1, options)


polygon − Creates a polygon item that must have at least three vertices.

my_canvas.create_polygon(x0, y0, x1, y1,...xn, yn, options)

  • Checkbutton

See check.py

The Checkbutton widget is used to display a number of options as checkboxes. The user can

select multiple options at a time.

var = StringVar()

c = Checkbutton(root, text="Here", variable=var, onvalue = "On", offvalue = "Off")

c.deselect()

c.pack()

  • Entry

See entry.py

The Entry widget is used to display a single-line text field for accepting values from

a user.

e = Entry(root, width=50, bg='black', fg='white')

e.insert(0,"Tommy")


Methods:

delete ( first, last=None )

Deletes characters from the widget, starting with the one at index first, up to but not

including the character at position last. If the second argument is omitted, only the 

single character at position first is deleted.

e.delete(0,END)


get()

Returns the entry's current text as a string.


insert ( index, s )

Inserts string s before the character at the given index.

  • Frame

See frame.py

The Frame widget is used as a container widget to organize other widgets.

Frame has no title, so has no text item. LabelFrame has a title.

frame = LabelFrame(root, text='My Frame', padx=50, pady=50)

frame.pack(padx=10,pady=10)


frame2 = Frame(root, padx=50, pady=50,bg="red")

frame2.pack(padx=10,pady=10)

  • Label

The Label widget is used to provide a single-line caption for other widgets. It can also

contain images.

my_label = Label(root, text="Here")

  • Listbox

See listbox.py

The Listbox widget is used to provide a list of options to a user.

my_listbox = Listbox(my_frame, selectmode=MULTIPLE)


The Listbox widget can support the following parameters


selectmode – Determines how many items can be selected, and how mouse drags

     affect the selection

    BROWSE − Normally, you can only select one line out of a listbox. If you click on

     an item and then drag to a different line, the selection will follow the mouse.

     This is the default.

    SINGLE − You can only select one line, and you can't drag the mouse.wherever you 

     click button 1, that line is selected.

    MULTIPLE − You can select any number of lines at once. Clicking on any line toggles 

    whether or not it is selected.

    EXTENDED − You can select any adjacent group of lines at once by clicking on the

    first line and dragging to the last line.


xscrollcommand If you want to allow the user to scroll the listbox horizontally, you

can link your listbox widget to a horizontal scrollbar.


yscrollcommand – If you want to allow the user to scroll the listbox vertically, you can

link your listbox widget to a vertical scrollbar.


Methods:

delete ( first, last=None )

Deletes characters from the widget, starting with the one at index first, up to but not

including the character at position last. If the second argument is omitted, only the 

single character at position first is deleted.

e.delete(0,END)


get ( first, last=None )

Returns a tuple containing the text of the lines with indices from first to last, inclusive.

If the second argument is omitted, returns the text of the line closest to first.

my_label.config(text=my_listbox.get(ANCHOR))


index ( i )

If possible, positions the visible part of the listbox so that the line containing index i is

at the top of the widget.

insert ( index, *elements )

Insert one or more new lines into the listbox before the line specified by index. Use

END as the first argument if you want to add new lines to the end of the listbox.

my_listbox.insert(END,"Item#1")

  • Menu

See menu_frame2.py

The Menu widget is used to provide various commands to a user.

  • Message

The Message widget is used to display multiline text fields for accepting values from a user.

  • Radiobutton

The Radiobutton widget is used to display a number of options as radio buttons.

The user can select only one option at a time.

  • Scale

The Scale widget is used to provide a slider widget.

  • Scrollbar

The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes.

  • Text

The Text widget is used to display text in multiple lines.

  • Toplevel

The Toplevel widget is used to provide a separate window container.

  • Spinbox

The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used

to select from a fixed number of values.

  • PanedWindow

A PanedWindow is a container widget that may contain any number of panes, arranged

horizontally or vertically.

  • LabelFrame

A labelframe is a simple container widget. Its primary purpose is to act as a spacer or 

container for complex window layouts.

  • tkMessageBox

This module is used to display message boxes in your applications.


No comments:

Post a Comment