0% found this document useful (0 votes)
76 views

Tkinter

The document contains code snippets demonstrating different Tkinter widgets in Python. It includes examples of buttons, canvas, checkbuttons, entries, frames, labels, menus, messageboxes, paned windows, scales, scrollbars, spinboxes, toplevel windows and radiobuttons. The snippets show how to create and configure each of the widgets as well as handling events.

Uploaded by

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

Tkinter

The document contains code snippets demonstrating different Tkinter widgets in Python. It includes examples of buttons, canvas, checkbuttons, entries, frames, labels, menus, messageboxes, paned windows, scales, scrollbars, spinboxes, toplevel windows and radiobuttons. The snippets show how to create and configure each of the widgets as well as handling events.

Uploaded by

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

1.

button frame
from tkinter import *

top = Tk()

top.geometry("200x100")

def fun():
messagebox.showinfo("Hello", "Red Button clicked")

b1 = Button(top,text = "Red",command = fun,activeforeground =


"red",activebackground = "pink",pady=10)

b2 = Button(top, text = "Blue",activeforeground = "blue",activebackground =


"pink",pady=10)

b3 = Button(top, text = "Green",activeforeground = "green",activebackground =


"pink",pady = 10)

b4 = Button(top, text = "Yellow",activeforeground = "yellow",activebackground =


"pink",pady = 10)

b1.pack(side = LEFT)

b2.pack(side = RIGHT)

b3.pack(side = TOP)

b4.pack(side = BOTTOM)

top.mainloop()
---------------------------------
2.Python Tkinter Canvas

from tkinter import *

top = Tk()

top.geometry("200x200")

#creating a simple canvas


c = Canvas(top,bg = "pink",height = "200")

c.pack()

top.mainloop()
--------------------------------------
3.Python Tkinter Checkbutton

from tkinter import *

top = Tk()

top.geometry("200x200")

checkvar1 = IntVar()
checkvar2 = IntVar()

checkvar3 = IntVar()

chkbtn1 = Checkbutton(top, text = "C", variable = checkvar1, onvalue = 1, offvalue


= 0, height = 2, width = 10)

chkbtn2 = Checkbutton(top, text = "C++", variable = checkvar2, onvalue = 1,


offvalue = 0, height = 2, width = 10)

chkbtn3 = Checkbutton(top, text = "Java", variable = checkvar3, onvalue = 1,


offvalue = 0, height = 2, width = 10)

chkbtn1.pack()

chkbtn2.pack()

chkbtn3.pack()

top.mainloop()
----------------------------------------
4.Python Tkinter Entry

from tkinter import *

top = Tk()

top.geometry("400x250")

name = Label(top, text = "Name").place(x = 30,y = 50)

email = Label(top, text = "Email").place(x = 30, y = 90)

password = Label(top, text = "Password").place(x = 30, y = 130)

sbmitbtn = Button(top, text = "Submit",activebackground = "pink", activeforeground


= "blue").place(x = 30, y = 170)

e1 = Entry(top).place(x = 80, y = 50)

e2 = Entry(top).place(x = 80, y = 90)

e3 = Entry(top).place(x = 95, y = 130)

top.mainloop()
--------------------------------------
5.A simple calculator

import tkinter as tk
from functools import partial

def call_result(label_result, n1, n2):


num1 = (n1.get())
num2 = (n2.get())
result = int(num1)+int(num2)
label_result.config(text="Result = %d" % result)
return

root = tk.Tk()
root.geometry('400x200+100+200')

root.title('Calculator')

number1 = tk.StringVar()
number2 = tk.StringVar()

labelNum1 = tk.Label(root, text="A").grid(row=1, column=0)

labelNum2 = tk.Label(root, text="B").grid(row=2, column=0)

labelResult = tk.Label(root)

labelResult.grid(row=7, column=2)

entryNum1 = tk.Entry(root, textvariable=number1).grid(row=1, column=2)

entryNum2 = tk.Entry(root, textvariable=number2).grid(row=2, column=2)

call_result = partial(call_result, labelResult, number1, number2)

buttonCal = tk.Button(root, text="Calculate", command=call_result).grid(row=3,


column=0)

root.mainloop()
--------------------------------------------------
6.Python Tkinter Frame

from tkinter import *

top = Tk()
top.geometry("140x100")
frame = Frame(top)
frame.pack()

leftframe = Frame(top)
leftframe.pack(side = LEFT)

rightframe = Frame(top)
rightframe.pack(side = RIGHT)

btn1 = Button(frame, text="Submit", fg="red",activebackground = "red")


btn1.pack(side = LEFT)

btn2 = Button(frame, text="Remove", fg="brown", activebackground = "brown")


btn2.pack(side = RIGHT)

btn3 = Button(rightframe, text="Add", fg="blue", activebackground = "blue")


btn3.pack(side = LEFT)

btn4 = Button(leftframe, text="Modify", fg="black", activebackground = "white")


btn4.pack(side = RIGHT)

top.mainloop()
-------------------------------------------------
7.Python Tkinter Menubutton
from tkinter import *

top = Tk()

top.geometry("200x250")

menubutton = Menubutton(top, text = "Language", relief = FLAT)

menubutton.grid()

menubutton.menu = Menu(menubutton)

menubutton["menu"]=menubutton.menu

menubutton.menu.add_checkbutton(label = "Hindi", variable=IntVar())

menubutton.menu.add_checkbutton(label = "English", variable = IntVar())

menubutton.pack()

top.mainloop()
----------------------------------------------------
8.Python Tkinter Menu

from tkinter import Toplevel, Button, Tk, Menu

top = Tk()
menubar = Menu(top)
file = Menu(menubar, tearoff=0)
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as...")
file.add_command(label="Close")

file.add_separator()

file.add_command(label="Exit", command=top.quit)

menubar.add_cascade(label="File", menu=file)
edit = Menu(menubar, tearoff=0)
edit.add_command(label="Undo")

edit.add_separator()

edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
edit.add_command(label="Delete")
edit.add_command(label="Select All")

menubar.add_cascade(label="Edit", menu=edit)
help = Menu(menubar, tearoff=0)
help.add_command(label="About")
menubar.add_cascade(label="Help", menu=help)

top.config(menu=menubar)
top.mainloop()
--------------------------------------------------
9.Tkinter Toplevel

from tkinter import *

root = Tk()

root.geometry("200x200")

def open():
top = Toplevel(root)
top.mainloop()

btn = Button(root, text = "open", command = open)

btn.place(x=75,y=50)

root.mainloop()
----------------------------------------------------
10.Tkinter PanedWindow

from tkinter import *

def add():
a = int(e1.get())
b = int(e2.get())
leftdata = str(a+b)
left.insert(1,leftdata)

w1 = PanedWindow()
w1.pack(fill = BOTH, expand = 1)

left = Entry(w1, bd = 5)
w1.add(left)

w2 = PanedWindow(w1, orient = VERTICAL)


w1.add(w2)

e1 = Entry(w2)
e2 = Entry(w2)

w2.add(e1)
w2.add(e2)

bottom = Button(w2, text = "Add", command = add)


w2.add(bottom)

mainloop()
---------------------------------------------------------
11.Tkinter LabelFrame

from tkinter import *

top = Tk()
top.geometry("300x200")

labelframe1 = LabelFrame(top, text="Positive Comments")


labelframe1.pack(fill="both", expand="yes")
toplabel = Label(labelframe1, text="Place to put the positive comments")
toplabel.pack()

labelframe2 = LabelFrame(top, text = "Negative Comments")


labelframe2.pack(fill="both", expand = "yes")

bottomlabel = Label(labelframe2,text = "Place to put the negative comments")


bottomlabel.pack()

top.mainloop()
---------------------------------------------------
12.Tkinter messagebox

A.)- INFORMATION msg

from tkinter import *

from tkinter import messagebox

top = Tk()

top.geometry("100x100")

messagebox.showinfo("information","Information")

top.mainloop()

B.)-ASK QuEsTION

from tkinter import *


from tkinter import messagebox

top = Tk()
top.geometry("100x100")
messagebox.askquestion("Confirm","Are you sure?")
top.mainloop()

c.)-TRY AGAIN MSG

from tkinter import *


from tkinter import messagebox

top = Tk()
top.geometry("100x100")
messagebox.askretrycancel("Application","try again?")

top.mainloop()
------------------------------------------------
13.Python Tkinter Spinbox

from tkinter import *

top = Tk()

top.geometry("200x200")

spin = Spinbox(top, from_= 0, to = 25)


spin.pack()

top.mainloop()

---------------------------------------
14.Python Tkinter Scrollbar

from tkinter import *

top = Tk()
sb = Scrollbar(top)
sb.pack(side = RIGHT, fill = Y)

mylist = Listbox(top, yscrollcommand = sb.set )

for line in range(30):


mylist.insert(END, "Number " + str(line))

mylist.pack( side = LEFT )


sb.config( command = mylist.yview )

mainloop()
-----------------------------------------
15.Python Tkinter Scale

from tkinter import *

def select():
sel = "Value = " + str(v.get())
label.config(text = sel)

top = Tk()
top.geometry("200x100")
v = DoubleVar()
scale = Scale( top, variable = v, from_ = 1, to = 50, orient = HORIZONTAL)
scale.pack(anchor=CENTER)

btn = Button(top, text="Value", command=select)


btn.pack(anchor=CENTER)

label = Label(top)
label.pack()

top.mainloop()
-----------------------------------
16.Python Tkinter Radiobutton

from tkinter import *

def selection():
selection = "You selected the option " + str(radio.get())
label.config(text = selection)

top = Tk()
top.geometry("300x150")
radio = IntVar()
lbl = Label(text = "Favourite programming language:")
lbl.pack()
R1 = Radiobutton(top, text="C", variable=radio, value=1,
command=selection)
R1.pack( anchor = W )

R2 = Radiobutton(top, text="C++", variable=radio, value=2,


command=selection)
R2.pack( anchor = W )

R3 = Radiobutton(top, text="Java", variable=radio, value=3,


command=selection)
R3.pack( anchor = W)

label = Label(top)
label.pack()
top.mainloop()
------------------------------------------------------------------

You might also like