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

GUI Eg f7b91d4f 3c1b 4242 Adb8 Fa2dbddde969

The document contains two Python programs: one for an Age Calculator using Tkinter for GUI, which calculates age based on user input of birth date and a given date, and another for a Clock Example using Turtle graphics to draw a clock face and display the current time. The Age Calculator includes functions for input validation and clearing fields, while the Clock Example features methods for drawing the clock hands and numbers. Both programs demonstrate basic GUI and graphics programming in Python.

Uploaded by

Naw Sut Aung
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)
8 views10 pages

GUI Eg f7b91d4f 3c1b 4242 Adb8 Fa2dbddde969

The document contains two Python programs: one for an Age Calculator using Tkinter for GUI, which calculates age based on user input of birth date and a given date, and another for a Clock Example using Turtle graphics to draw a clock face and display the current time. The Age Calculator includes functions for input validation and clearing fields, while the Clock Example features methods for drawing the clock hands and numbers. Both programs demonstrate basic GUI and graphics programming in Python.

Uploaded by

Naw Sut Aung
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

1.

Age Calculator
# import all functions from the tkinter
from tkinter import *
# import messagebox class from tkinter
from tkinter import messagebox
# Function for clearing the
# contents of all text entry boxes
def clearAll() :
# deleting the content from the entry box
dayField.delete(0, END)
monthField.delete(0, END)
yearField.delete(0, END)
givenDayField.delete(0, END)
givenMonthField.delete(0, END)
givenYearField.delete(0, END)
rsltDayField.delete(0, END)
rsltMonthField.delete(0, END)
rsltYearField.delete(0, END)
# function for checking error
def checkError() :
# if any of the entry field is empty
# then show an error message and clear
# all the entries
if (dayField.get() == "" or monthField.get() == ""
or yearField.get() == "" or givenDayField.get() == ""
or givenMonthField.get() == "" or givenYearField.get() == "") :
# show the error message
messagebox.showerror("Input Error")
# clearAll function calling
clearAll()
return -1
# function to calculate Age
def calculateAge() :
# check for error
value = checkError()
# if error is occur then return
if value == -1 :
return
else :
# take a value from the respective entry boxes
# get method returns current text as string
birth_day = int(dayField.get())
birth_month = int(monthField.get())
birth_year = int(yearField.get())
given_day = int(givenDayField.get())
given_month = int(givenMonthField.get())
given_year = int(givenYearField.get())
# if birth date is greater then given birth_month
# then donot count this month and add 30 to the date so
# as to subtract the date and get the remaining days
month =[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (birth_day > given_day):
given_month = given_month - 1
given_day = given_day + month[birth_month-1]
# if birth month exceeds given month, then
# donot count this year and add 12 to the
# month so that we can subtract and find out
# the difference
if (birth_month > given_month):
given_year = given_year - 1
given_month = given_month + 12
# calculate day, month, year
calculated_day = given_day - birth_day;
calculated_month = given_month - birth_month;
calculated_year = given_year - birth_year;
# calculated day, month, year write back
# to the respective entry boxes
# insert method inserting the
# value in the text entry box.
rsltDayField.insert(10, str(calculated_day))
rsltMonthField.insert(10, str(calculated_month))
rsltYearField.insert(10, str(calculated_year))
# Driver Code
if __name__ == "__main__" :
# Create a GUI window
gui = Tk()
# Set the background colour of GUI window
gui.configure(background = "light blue")
# set the name of tkinter GUI window
gui.title("Age Calculator")
# Set the configuration of GUI window
gui.geometry("525x260")
# Create a Date Of Birth : label
dob = Label(gui, text = "Date Of Birth", bg = "light blue")
# Create a Given Date : label
givenDate = Label(gui, text = "Given Date", bg = "light blue")
# Create a Day : label
day = Label(gui, text = "Day", bg = "light blue")

# Create a Month : label


month = Label(gui, text = "Month", bg = "light blue")

# Create a Year : label


year = Label(gui, text = "Year", bg = "light blue")

# Create a Given Day : label


givenDay = Label(gui, text = "Given Day", bg = "light blue")

# Create a Given Month : label


givenMonth = Label(gui, text = "Given Month", bg = "light blue")

# Create a Given Year : label


givenYear = Label(gui, text = "Given Year", bg = "light blue")

# Create a Years : label


rsltYear = Label(gui, text = "Years", bg = "light blue")

# Create a Months : label


rsltMonth = Label(gui, text = "Months", bg = "light blue")

# Create a Days : label


rsltDay = Label(gui, text = "Days", bg = "light blue")
# Create a Resultant Age Button and attached to calculateAge function
resultantAge = Button(gui, text = "Resultant Age", fg = "Black", bg = "light
blue", command = calculateAge)

# Create a Clear All Button and attached to clearAll function


clearAllEntry = Button(gui, text = "Clear All", fg = "Black", bg = "light blue",
command = clearAll)

# Create a text entry box for filling or typing the information.


dayField = Entry(gui)
monthField = Entry(gui)
yearField = Entry(gui)

givenDayField = Entry(gui)
givenMonthField = Entry(gui)
givenYearField = Entry(gui)

rsltYearField = Entry(gui)
rsltMonthField = Entry(gui)
rsltDayField = Entry(gui)
# grid method is used for placing
# the widgets at respective positions
# in table like structure .
dob.grid(row = 0, column = 1)
day.grid(row = 1, column = 0)
dayField.grid(row = 1, column = 1)
month.grid(row = 2, column = 0)
monthField.grid(row = 2, column = 1)
year.grid(row = 3, column = 0)
yearField.grid(row = 3, column = 1)
givenDate.grid(row = 0, column = 4)
givenDay.grid(row = 1, column = 3)
givenDayField.grid(row = 1, column = 4)
givenMonth.grid(row = 2, column = 3)
givenMonthField.grid(row = 2, column = 4)
givenYear.grid(row = 3, column = 3)
givenYearField.grid(row = 3, column = 4)
resultantAge.grid(row = 4, column = 2)
rsltYear.grid(row = 5, column = 2)
rsltYearField.grid(row = 6, column = 2)
rsltMonth.grid(row = 7, column = 2)
rsltMonthField.grid(row = 8, column = 2)
rsltDay.grid(row = 9, column = 2)
rsltDayField.grid(row = 10, column = 2)
clearAllEntry.grid(row = 12, column = 2)
# Start the GUI
gui.mainloop()
*********************************
2.ClockExample
from turtle import *
import time
import math

class Clock_face:
def __init__(self) -> None:
self.radius = 100
self.hourmaker_len = 10
self.hour_hand_len = 45
self.min_hand_len = 60
self.sec_hand_len = 65
self.dot_size = 7

def draw_hand(self, length, angle):


pu()
goto(0, 0)
pd()
seth(90) # Set heading upwards
rt(angle) # Rotate to the correct angle
fd(length)
bk(length) # Move back to center

def draw_numbers(self):
"""Draw numbers on the clock face without overlapping dots."""
for num in range(1, 13):
angle = math.radians(30 * num) # 360° divided by 12 hours = 30° each
x = (self.radius - 20) * math.sin(angle) # Move numbers slightly inward
y = (self.radius - 20) * math.cos(angle) # Adjust Y position
pu()
goto(x, y - 10 if num in [10, 11, 12] else y - 5) # Shift top numbers down slightly
pd()
write(num, align="center", font=("Arial", 14, "bold"))

def draw_hour_markers(self):
"""Draw dots for hour markers instead of lines."""
self.inner_radius = 85
for i in range(12):
angle = math.radians(30 * i)
x = self.inner_radius * math.sin(angle)
y = self.inner_radius * math.cos(angle)
pu()
goto(x, y)
pd()
dot(5)

def draw(self, hour, minute, second):


pu()
goto(0, -self.radius)
pd()
pensize(5)
circle(self.radius)

# Draw the hour markers as dots


self.draw_hour_markers()

# Draw the numbers


self.draw_numbers()

# Draw the hour hand


self.hour_hand_angle = 0.5 * ((60 * hour) + minute)
self.draw_hand(self.hour_hand_len, self.hour_hand_angle)

# Draw the minute hand


self.min_hand_angle = 6 * minute
self.draw_hand(self.min_hand_len, self.min_hand_angle)

# Draw the second hand


pencolor('red')
pensize(2)
self.sec_hand_angle = 6 * second
self.draw_hand(self.sec_hand_len, self.sec_hand_angle)

# Draw a dot at the center


pu()
goto(0, 0)
pd()
dot(self.dot_size)

hideturtle()
done()

def main():
clock = Clock_face()
while True:
hr = int(time.strftime("%I")) # 12-hour format
mn = int(time.strftime("%M"))
sec = int(time.strftime("%S"))
clock.draw(hr, mn, sec)
time.sleep(1)
clear() # Clear the screen for the next update
if __name__ == '__main__':
main()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy