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

Gui Codes

The document provides code examples for common Tkinter widgets and functions including: 1. Buttons, radio buttons, checkbuttons, entries, spinboxes, frames, label frames, listboxes, adding images, canvas, message boxes, binding functions, scrollbars, and menu bars. 2. The examples demonstrate how to create each widget type, configure properties like colors and text, and place the widgets in the window. 3. Functions like pack, grid, place are used to arrange the widgets, and bindings handle user interactions like key presses.

Uploaded by

AKANKSHA GARG
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Gui Codes

The document provides code examples for common Tkinter widgets and functions including: 1. Buttons, radio buttons, checkbuttons, entries, spinboxes, frames, label frames, listboxes, adding images, canvas, message boxes, binding functions, scrollbars, and menu bars. 2. The examples demonstrate how to create each widget type, configure properties like colors and text, and place the widgets in the window. 3. Functions like pack, grid, place are used to arrange the widgets, and bindings handle user interactions like key presses.

Uploaded by

AKANKSHA GARG
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Button

from tkinter import *

root= Tk()

root.geometry("200x100")

b = Button(root,text = "Simple")

b.pack()

root.mainloop()

2. 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()
3. 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. Entry

# import tkinter as tk
from tkinter import *
root=Tk()

# setting the windows size


root.geometry("600x400")

# declaring string variable


# for storing name and password
name_var=StringVar()
passw_var=StringVar()

# defining a function that will


# get the name and password and
# print them on the screen
def submit():

name=name_var.get()
password=passw_var.get()

print("The name is : " + name)


print("The password is : " + password)

name_var.set("")
passw_var.set("")

# creating a label for


# name using widget Label
name_label = Label(root, text = 'Username', font=('calibre',10, 'bold'))

# creating a entry for input


# name using widget Entry
name_entry = Entry(root,textvariable = name_var, font=('calibre',10,'normal'))

# creating a label for password


passw_label = Label(root, text = 'Password', font = ('calibre',10,'bold'))

# creating a entry for password


passw_entry=Entry(root, textvariable = passw_var, font = ('calibre',10,'normal'), show = '*')

# creating a button using the widget


# Button that will call the submit function
sub_btn=Button(root,text = 'Submit', command = submit)

# placing the label and entry in


# the required position using grid
# method
name_label.grid(row=0,column=0)
name_entry.grid(row=0,column=1)
passw_label.grid(row=1,column=0)
passw_entry.grid(row=1,column=1)
sub_btn.grid(row=2,column=1)

# performing an infinite loop


# for the window to display
root.mainloop()

5. SpinBox

from tkinter import *


master = Tk()
w = Spinbox(master, from_ = -10, to = 10)
w.place(x=0,y=10)
mainloop()

6. Frames

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

8. ListBox

from tkinter import *

# create a root window.


top = Tk()

# create listbox object


listbox = Listbox(top, height = 10,
width = 15,
bg = "grey",
activestyle = 'dotbox',
font = "Helvetica",
fg = "yellow")
# Define the size of the window.
top.geometry("300x250")

# Define a label for the list.


label = Label(top, text = " FOOD ITEMS")

# insert elements by their


# index and names.
listbox.insert(1, "Nachos")
listbox.insert(2, "Sandwich")
listbox.insert(3, "Burger")
listbox.insert(4, "Pizza")
listbox.insert(5, "Burrito")

# pack the widgets


label.pack()
listbox.pack()

# Display untill User


# exits themselves.
top.mainloop()

9. Adding Images

# Import required libraries


from tkinter import *
from PIL import ImageTk, Image

# Create an instance of tkinter window


win = Tk()

# Define the geometry of the window


win.geometry("300x205")

frame = Frame(win, width=600, height=400)


frame.pack()
frame.place()

# Create an object of tkinter ImageTk


img=Image.open("C:\\Users\\acer\\Pictures\\Video Projects\\IMG-20201107-WA0008.jpg")
resized_img= img.resize((300,205), Image.ANTIALIAS)
new_img = ImageTk.PhotoImage(resized_img)

# Create a Label Widget to display the text or Image


label = Label(frame, image = new_img)
label.pack()

win.mainloop()

10. Canvas

from tkinter import *

root = Tk()

C = Canvas(root, bg="yellow",
height=250, width=300)

line = C.create_line(108, 120,


320, 40,
width="10",
fill="brown")

arc = C.create_arc(180, 150, 80,


210, start=0,
extent=220,
fill="red")

oval = C.create_oval(80, 30, 140,


150,
fill="blue")

C.pack()
mainloop()

11. MessageBox

from tkinter import *


from tkinter import messagebox

root = Tk()
root.geometry("300x200")
w = Label(root, text ='MessageBox Tutorial', font = "50")
w.pack()

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

messagebox.showwarning("showwarning", "Warning")

messagebox.showerror("showerror", "Error")

messagebox.askquestion("askquestion", "Are you sure?")

messagebox.askokcancel("askokcancel", "Want to continue?")

messagebox.askyesno("askyesno", "Find the value?")

messagebox.askretrycancel("askretrycancel", "Try again?")

root.mainloop()

12. Binding Function

from tkinter import *

# function to be called when


# keyboard buttons are pressed
def key_press(event):
key = event.char
print(key, 'is pressed')

# creates tkinter window or root window


root = Tk()
root.geometry('200x100')

# here we are binding keyboard


# with the main window
root.bind('<Key>', key_press)

mainloop()

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

14. MenuBar

from tkinter import *

# creating tkinter window


root = Tk()
root.title('Menu Demonstration')

# Creating Menubar
menubar = Menu(root)

# Adding File Menu and commands


file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = root.destroy)

# Adding Edit Menu and commands


edit = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)

# Adding Help Menu


help_ = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)

# display Menu
root.config(menu = menubar)
root.mainloop()

You might also like