0% found this document useful (0 votes)
95 views3 pages

Gui

The document contains examples of using different Tkinter widgets and functions in Python. It includes examples of using messageboxes, radiobuttons, canvas drawing, formatting text labels, creating a counter, and building menus. Each example demonstrates basic usage of a Tkinter widget or function.

Uploaded by

Yogesh Tripathi
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)
95 views3 pages

Gui

The document contains examples of using different Tkinter widgets and functions in Python. It includes examples of using messageboxes, radiobuttons, canvas drawing, formatting text labels, creating a counter, and building menus. Each example demonstrates basic usage of a Tkinter widget or function.

Uploaded by

Yogesh Tripathi
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/ 3

1)Textbox

import tkinter
from tkinter import messagebox
top=tkinter.Tk()

def simple():
m=messagebox.showinfo("Welcome","this is the welcome message")

def simple2():
m=messagebox.showwarning("Warning","this is the warning message")

def simple3():
m=messagebox.showerror("Error","this is the error message")

b=tkinter.Button(top,text="Welcome",command=simple)

a=tkinter.Button(top,text='warning',command=simple2)

c=tkinter.Button(top,text="error",command=simple3)

b.pack()
a.pack()
c.pack()

top.mainloop()

2)RadioButton

from tkinter import *


root = Tk()
v = IntVar()

def sel():
selection = "You selected option - " + str(v.get())
label.config(text = selection)

Label(root, text="""Choose a programming language:""", justify = RIGHT, padx =


20).pack()

Radiobutton(root, text="Python", padx = 20, variable=v,


value=10,command=sel).pack(anchor=W)

Radiobutton(root, text="bigdata", padx = 20, variable=v,


value=20,command=sel).pack(anchor=W)

label = Label(root)
label.pack()
root.mainloop()
3)Canvas

import tkinter
top=tkinter.Tk()

c=tkinter.Canvas(top,bg="green",height=250,width=300)
coord = 100,200,300,400
arc=c.create_arc(coord,start=0,extent=160,fill="red")
c.pack()
top.mainloop()

4)Formating Text

from tkinter import *


root = Tk()

l=Label(root,text="WELCOME",fg = "red", bg = "dark blue",font = "Times")


l.pack(side=RIGHT)
l.place(x=10,y=10)

l1=Label(root,
text="Green Text in Helvetica Font",
fg = "light green",
bg = "dark green",
font = "Helvetica 16 bold italic",)
l1.pack(side=RIGHT)
l1.place(x=120,y=120)

l2=Label(root,
text="Blue Text in Verdana bold",
fg = "blue",
bg = "yellow",
font = "Verdana 10 bold")
l2.pack(side=RIGHT)
l2.place(x=150,y=150)

root.mainloop()

4)Counter

import tkinter as tk
counter = 0
def counter_label(label):
def count():
global counter
counter += 1
label.config(text=str(counter))
label.after(1000, count)
count()

root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
counter_label(label)
button = tk.Button(root, text='Stop', width=25, command=root.destroy)
button.pack()
root.mainloop()

5)Menu

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()

You might also like