0% found this document useful (0 votes)
231 views10 pages

TKINTER

The document provides code examples for creating common GUI elements in Tkinter such as buttons, checkboxes, frames, labels, listboxes and menus. It demonstrates how to import Tkinter, create a main window container, pack or place widgets, and use common widget classes like Button, Checkbutton, Label, Listbox and Menubar to build the interface. It also shows how to define callback functions for buttons and menus.

Uploaded by

pdanish
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)
231 views10 pages

TKINTER

The document provides code examples for creating common GUI elements in Tkinter such as buttons, checkboxes, frames, labels, listboxes and menus. It demonstrates how to import Tkinter, create a main window container, pack or place widgets, and use common widget classes like Button, Checkbutton, Label, Listbox and Menubar to build the interface. It also shows how to define callback functions for buttons and menus.

Uploaded by

pdanish
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/ 10

TKINTER

#!/usr/bin/python3

import tkinter # note that module name has changed from Tkinter in Python 2 to
tkinter in Python 3
top = tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()

HOW TO CREATE A GUI APPLICATIONS ?


# !/usr/bin/python3
from tkinter import *

from tkinter import messagebox

top = Tk()
top.geometry("300x300")
def helloCallBack():
msg = messagebox.showinfo( "Hello Python", "Hello World")

B = Button(top, text = "Hello", command = helloCallBack)


B.place(x = 50,y = 50)
top.mainloop()

HOW TO CREATE CHECKBOX IN TKINTER ?

# !/usr/bin/python3
from tkinter import *

import tkinter
top = Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \
onvalue = 0, offvalue = 0, height=5, \
width = 20, )
C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \
onvalue = 1, offvalue = 0, height=5, \
width = 20)
C1.pack()
C2.pack()
top.mainloop()

HOW TO CREATE A INBOX IN PYTHON ?


# !/usr/bin/python3
from tkinter import *

top = Tk()
L1 = Label(top, text = "User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd = 5)
E1.pack(side = RIGHT)

top.mainloop()

HOW TO CREATE FRAME ?

# !/usr/bin/python3
from tkinter import *

root = Tk()
frame = Frame(root)
frame.pack()

bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text = "Red", fg = "red")
redbutton.pack( side = LEFT)

greenbutton = Button(frame, text = "Brown", fg="brown")


greenbutton.pack( side = LEFT )

bluebutton = Button(frame, text = "Blue", fg = "blue")


bluebutton.pack( side = LEFT )

blackbutton = Button(bottomframe, text = "Black", fg = "black")


blackbutton.pack( side = BOTTOM)

root.mainloop()

HOW TO CREATE A LABEL IN PYTHON3 ?

# !/usr/bin/python3
from tkinter import *

root = Tk()

var = StringVar()
label = Label( root, textvariable = var, relief = RAISED )

var.set("Hey!? How are you doing?")


label.pack()
root.mainloop()

HOW TO CREATE A LIST BOX IN PYTHON ?

# !/usr/bin/python3
from tkinter import *

import tkinter

top = Tk()

Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")

Lb1.pack()
top.mainloop()

HOW TO CREATE MENU IN PYTHON ?

# !/usr/bin/python3
from tkinter import *
def donothing():
filewin = Toplevel(root)
button = Button(filewin, text="Do nothing button")
button.pack()

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="New", command = donothing)
filemenu.add_command(label = "Open", command = donothing)
filemenu.add_command(label = "Save", command = donothing)
filemenu.add_command(label = "Save as...", command = donothing)
filemenu.add_command(label = "Close", command = donothing)

filemenu.add_separator()

filemenu.add_command(label = "Exit", command = root.quit)


menubar.add_cascade(label = "File", menu = filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label = "Undo", command = donothing)

editmenu.add_separator()

editmenu.add_command(label = "Cut", command = donothing)


editmenu.add_command(label = "Copy", command = donothing)
editmenu.add_command(label = "Paste", command = donothing)
editmenu.add_command(label = "Delete", command = donothing)
editmenu.add_command(label = "Select All", command = donothing)

menubar.add_cascade(label = "Edit", menu = editmenu)


helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label = "Help Index", command = donothing)
helpmenu.add_command(label = "About...", command = donothing)
menubar.add_cascade(label = "Help", menu = helpmenu)

root.config(menu = menubar)
root.mainloop()

HOW TO CREATE MESSAGE ?

# !/usr/bin/python3
from tkinter import *

root = Tk()

var = StringVar()
label = Message( root, textvariable = var, relief = RAISED )

var.set("Hey!? How are you doing?")


label.pack()
root.mainloop()

You might also like