Skip to content

Commit e5493fa

Browse files
committed
add detecting gender by name gui app tutorial
1 parent 04c3a2c commit e5493fa

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
230230
- [How to Make a Planet Simulator with PyGame in Python](https://www.thepythoncode.com/article/make-a-planet-simulator-using-pygame-in-python). ([code](gui-programming/planet-simulator))
231231
- [How to Make a Markdown Editor using Tkinter in Python](https://www.thepythoncode.com/article/markdown-editor-with-tkinter-in-python). ([code](gui-programming/markdown-editor))
232232
- [How to Build a GUI Currency Converter using Tkinter in Python](https://www.thepythoncode.com/article/currency-converter-gui-using-tkinter-python). ([code](gui-programming/currency-converter-gui/))
233+
- [How to Detect Gender by Name using Python](https://www.thepythoncode.com/article/gender-predictor-gui-app-tkinter-genderize-api-python). ([code](gui-programming/genderize-app))
233234

234235
For any feedback, please consider pulling requests.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Detect Gender by Name using Python](https://www.thepythoncode.com/article/gender-predictor-gui-app-tkinter-genderize-api-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# importing everything from tkinter
2+
from tkinter import *
3+
4+
# the requests will be used for making requests to the API
5+
import requests
6+
7+
# tkinter message box to display errors
8+
from tkinter.messagebox import showerror
9+
10+
11+
def predict_gender():
12+
# executes when code has no errors
13+
try:
14+
# getting the input from entry
15+
entered_name = name_entry.get()
16+
# making a request to the API, the user's entered name is injected in the url
17+
response = requests.get(f'https://api.genderize.io/?name={entered_name}').json()
18+
# getting name from the response
19+
name = response['name']
20+
# getting gender from the response
21+
gender = response['gender']
22+
# getting probability from the response
23+
probability = 100 * response['probability']
24+
# adding name to the label that was empty, the name is being uppercased
25+
name_label.config(text='The name is ' + name.upper())
26+
# adding gender to the label that was empty, the gender is being uppercased
27+
gender_label.config(text='The gender is ' + gender.upper())
28+
# adding probability to the label that was empty
29+
probability_label.config(text='Am ' + str(probability) + '%' + ' accurate')
30+
# executes when errors are caught
31+
# KeyError, ConnectionTimeoutError
32+
except:
33+
showerror(title='error', message='An error occurred!! Make sure you have internet connection or you have entered the correct data')
34+
35+
36+
# colors for the application
37+
gold = '#dca714'
38+
brown = '#31251d'
39+
40+
# creating the main window
41+
window = Tk()
42+
# defining the demensions of the window, width(325), height(300), 500+200 center the window
43+
window.geometry('325x300+500+200')
44+
# this is the title of the application
45+
window.title('Gender Predictor')
46+
# this makes the window unresizable
47+
window.resizable(height=FALSE, width=FALSE)
48+
49+
"""The two frames"""
50+
# this is the top frame inside the main window
51+
top_frame = Frame(window, bg=brown, width=325, height=80)
52+
top_frame.grid(row=0, column=0)
53+
54+
# this is the bottom frame inside the main window
55+
bottom_frame = Frame(window, width=300, height=250)
56+
bottom_frame.grid(row=1, column=0)
57+
58+
# the label for the big title inside the top_frame
59+
first_label = Label(top_frame, text='GENDER PREDICTOR', bg=brown, fg=gold, pady=10, padx=20, justify=CENTER, font=('Poppins 20 bold'))
60+
first_label.grid(row=0, column=0)
61+
62+
# the label for the small text inside the top_frame
63+
second_label = Label(top_frame, text='Give me any name and i will predict its gender', bg=brown, fg=gold, font=('Poppins 10'))
64+
second_label.grid(row=1, column=0)
65+
66+
"""below are widgets inside the top_frame"""
67+
# the name label
68+
label = Label(bottom_frame, text='NAME:', font=('Poppins 10 bold'), justify=LEFT)
69+
label.place(x=4, y=10)
70+
71+
# the entry for entering the user's name
72+
name_entry = Entry(bottom_frame, width=25, font=('Poppins 15 bold'))
73+
name_entry.place(x=5, y=35)
74+
75+
# the empty name label, it will be used to display the name
76+
name_label = Label(bottom_frame, text='', font=('Poppins 10 bold'))
77+
name_label.place(x=5, y=70)
78+
79+
# the empty gender label, it will be used to display the gender
80+
gender_label = Label(bottom_frame, text='', font=('Poppins 10 bold'))
81+
gender_label.place(x=5, y=90)
82+
83+
# the empty probability label, it will be used to display the gender probalility
84+
probability_label = Label(bottom_frame, text='', font=('Poppins 10 bold'))
85+
probability_label.place(x=5, y=110)
86+
87+
# the predict button
88+
predict_button = Button(bottom_frame, text="PREDICT", bg=gold, fg=brown, font=('Poppins 10 bold'), command=predict_gender)
89+
predict_button.place(x=5, y=140)
90+
91+
window.mainloop()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

0 commit comments

Comments
 (0)