Unit - 4 Lab Programs Ex 4.1 To 4.4
Unit - 4 Lab Programs Ex 4.1 To 4.4
Aim:
Design a gui form with vertical box layout that includes labels and entry fields for
user registration in python.
Algorithm:
Step 1 : The first step is to import the tkinter module
(using either tkinter import * or just import tkinter).
Step 2 : The primary window of the GUI programme was created.
Step 3 : Include one or more widgets in the GUI programme (controls such as
buttons, labels, and text boxes, etc.).
Step 4 : Enter the primary events to react to each event that the user has triggered.
Coding
from tkinter import *
base = Tk()
base.geometry("500x500")
base.title("registration form")
output:
Result :
To design a GUI form with vertical box layout that includes labels and entry fields
for user registration in python was competed successfully.
Aim:
To create a GUI window with a grid layout that performs Tic-tac-toe game(3x3 board
game).
Algorithm:
• Create a GUI window using tk() as the root window with title ‘Tic-Tac-Toe’
• Bind the button command to the function ‘clicked’ with its position as
arguments
• Attach button to root window using ‘grid’ layout with row and column values
• Check if any row has the same symbol, display win status and stop_game =
True
• Check if any column has the same symbol, if yes display win status and
stop_game = True
• Check if any diagonal has the same symbol, display win status and
stop_game = True
• Check if all the 9 positions are filled: display tie and stop_game = True
Program:
stop_game = False
def clicked(r,c):
global Player
#X's turn
b[r][c].configure(text = "X")
Player='O'
#o's turn
b[r][c].configure(text = 'O')
Player = "X"
check_if_win()
if stop_game == True:
root.destroy()
def check_if_win():
global stop_game
for i in range(3):
#Horizontal match
stop_game = True
break
#Vertical Match
break
#Left diagonal
stop_game = True
break
#Right diagonal
stop_game = True
break
#Tie case
stop_game = True
break
# Design window
root = Tk()
root.resizable(0,0)
b = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(3):
for j in range(3):
b[i][j].grid(row = i, column = j)
mainloop()
Output:
Result:
The program to create GUI board 3x3 board game with buttons was successfully
executed for Tic-tac-toe game
AIM :
To write a python program to create canvas in GUI program and draw simple shapes such
as rectangles, circles, and lines.
ALGORITHM:
In this program we do some drawing. Drawing in Tkinter is done on the Canvas widget.
Canvas is a high-level facility for doing graphics in Tkinter.
It can be used to create charts, custom widgets, or create games.
Import the module tkinter and it’s canva class to create the required shapes
Canvas Methods for shapes:
Canvas.create_oval(x1, y1, x2, y2, options = …): It is used to create a oval, pieslice
and chord.
Canvas.create_rectangle(x1, y1, x2, y2, options = …): It is used to create rectangle
and square.
Canvas.create_line(x1, y1, x2, y2, options = …) This is used to create an line.
Canvas.create_polygon(coordinates, options = …) THis is used to create any valid
shapes.
Class is used to show the working of functions that helps to creates different shapes.
Class parameters –
Source Code
# Imports each and every method and class of module tkinter
from tkinter import *
class Shape:
def __init__(self, master = None):
self.master = master
# Calls create method of class Shape
self.create()
def create(self):
# Creates a object of class canvas
# with the help of this we can create different shapes
self.canvas = Canvas(self.master)
# Creates a circle of diameter 80
self.canvas.create_oval(10, 10, 80, 80, outline = "black", fill = "white",
width = 2)
# Creates an ellipse with horizontal diameter
# of 210 and vertical diameter of 80
self.canvas.create_oval(110, 10, 210, 80, outline = "red", fill = "green",
width = 2)
# Creates a rectangle of 50x60 (heightxwidth)
self.canvas.create_rectangle(230, 10,320, 60,outline = "black", fill =
"blue",width = 2)
# Creates an line
self.canvas.create_line(30, 200, 90, 100, fill = "red", width = 2)
# Pack the canvas to the main window and make it expandable
self.canvas.pack(fill = BOTH, expand = 1)
if __name__ == "__main__":
# object of class Tk, responsible for creating
# a tkinter toplevel window
master = Tk()
shape = Shape(master)
# Sets the title to Shapes
master.title("Shapes")
# Sets the geometry and position
# of window on the screen
master.geometry("330x220 + 300 + 300")
# Infinite loop breaks only by interrupt
mainloop()
OUTPUT:
Algorithm:
Step 1: Start by importing the Tk class from the tkinter module. This class is used to create
the main application window.
Step 2: A function named clicked is defined. This function will be called when the "Submit"
button is clicked. It retrieves the text entered in the Entry widgets for name, department,
and year, concatenates them with appropriate labels, and sets the resulting text to the
display Label widget. Additionally, it changes the font of the display based on the option
selected from the dropdown menu.
Step 3: Create Main Window: An instance of the Tk class is created, representing the main
application window. The title of the window is set to "GUI Form" and its size is defined as
200x200 pixels.
Step 4: Define Variables: Two variables Name and choosen are defined. These variables
will be associated with StringVar and StringVar objects respectively.