Skip to content

Commit 06b62a8

Browse files
Update Day-27
1 parent 77b6b3a commit 06b62a8

File tree

6 files changed

+435
-0
lines changed

6 files changed

+435
-0
lines changed

Day-27-TkinterGUI/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# BMI Calculator GUI
2+
3+
### An implementation of BMI-Calculator.
4+
### [BMICalculatorGUI](https://repl.it/@abhijeetpandit/BMICalculatorGUI#main.py)
5+
6+
<img src= 'https://user-images.githubusercontent.com/65078610/106606495-355a4c80-6588-11eb-9df8-651ca5029d91.gif' width="500">

Day-27-TkinterGUI/Tkinter GUI.ipynb

+237
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "bacterial-notice",
6+
"metadata": {},
7+
"source": [
8+
"# Day 27"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "human-import",
14+
"metadata": {},
15+
"source": [
16+
"###### Tkinter, \\*args, **kwargs, Creating GUI Programs"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 15,
22+
"id": "excess-injection",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"import tkinter"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 14,
32+
"id": "vocal-arkansas",
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"window = tkinter.Tk()\n",
37+
"window.title('GUI title')\n",
38+
"window.minsize(width=500, height=300)\n",
39+
"window.config(padx=20, pady=20)\n",
40+
"\n",
41+
"# Label\n",
42+
"FONT = ('arial',24,'bold')\n",
43+
"my_label = tkinter.Label(text='I am label', font=FONT)\n",
44+
"# pack() used to lay the components built\n",
45+
"# my_label.pack(side='bottom',expand=1)\n",
46+
"my_label.pack()\n",
47+
"\n",
48+
"\n",
49+
"# Alternate ways of updating properties of built component\n",
50+
"my_label['text'] = 'New label'\n",
51+
"my_label.config(text='Another new label')\n",
52+
"\n",
53+
"def btnClick():\n",
54+
" user_input = input.get()\n",
55+
" my_label['text'] = user_input\n",
56+
" \n",
57+
"# Button\n",
58+
"my_button = tkinter.Button(text='Click me', command=btnClick)\n",
59+
"my_button.pack()\n",
60+
"\n",
61+
"# Entry\n",
62+
"input = tkinter.Entry(width=20)\n",
63+
"input.pack()\n",
64+
"\n",
65+
"window.mainloop()"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"id": "hungry-local",
71+
"metadata": {},
72+
"source": [
73+
"**Layout Manager**\n",
74+
"\n",
75+
"- **pack()**\n",
76+
" - Adds components one below other, aligned at center\n",
77+
" ---\n",
78+
"- **place()**\n",
79+
" ```python\n",
80+
" input = tkinter.Entry(width=20)\n",
81+
" input.pack()\n",
82+
" input.place(x=0,y=0)\n",
83+
" ```\n",
84+
" ---\n",
85+
"- **grid()** => Divides screen into num of rows & columns\n",
86+
" ```python\n",
87+
" input = tkinter.Entry(width=20)\n",
88+
" input.pack()\n",
89+
" input.column(row=0,column=0)\n",
90+
" ```"
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"id": "substantial-summer",
96+
"metadata": {},
97+
"source": [
98+
"###### Advanced Python Arguments\n",
99+
"###### *args: Many Positional Arguments => args - Tuple"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 24,
105+
"id": "falling-opposition",
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"name": "stdout",
110+
"output_type": "stream",
111+
"text": [
112+
"7\n",
113+
"15\n"
114+
]
115+
}
116+
],
117+
"source": [
118+
"# Here 'add' function takes 2 arguments only\n",
119+
"def add(n1,n2):\n",
120+
" print(n1+n2)\n",
121+
"add(5,2)\n",
122+
"\n",
123+
"# For unlimited args:\n",
124+
"# Note: 'args' can be changed to any string, but '*' is required\n",
125+
"def add(*args):\n",
126+
" numbers = [n for n in args]\n",
127+
" print(sum(numbers))\n",
128+
"add(1,2,3,4,5)"
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"id": "attractive-particle",
134+
"metadata": {},
135+
"source": [
136+
"###### **kwargs: Many Keyword Arguments => kwargs - Dictionary"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 35,
142+
"id": "cleared-necessity",
143+
"metadata": {},
144+
"outputs": [
145+
{
146+
"name": "stdout",
147+
"output_type": "stream",
148+
"text": [
149+
"Addition: 4\n",
150+
"Multiplication: 9\n"
151+
]
152+
}
153+
],
154+
"source": [
155+
"def calculate(n,**kwargs):\n",
156+
" add = n + kwargs['add']\n",
157+
" multiply = n * kwargs['multiply']\n",
158+
" print(f'Addition: {add}')\n",
159+
" print(f'Multiplication: {multiply}')\n",
160+
"calculate(3, add=1, multiply=3)"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": 37,
166+
"id": "ongoing-mentor",
167+
"metadata": {},
168+
"outputs": [
169+
{
170+
"name": "stdout",
171+
"output_type": "stream",
172+
"text": [
173+
"BMW\n",
174+
"GT\n"
175+
]
176+
}
177+
],
178+
"source": [
179+
"class Car:\n",
180+
" def __init__(self,**kw):\n",
181+
" self.brand = kw['brand']\n",
182+
" self.model = kw['model']\n",
183+
"my_car = Car(brand='BMW',model='GT')\n",
184+
"print(my_car.brand)\n",
185+
"print(my_car.model)"
186+
]
187+
},
188+
{
189+
"cell_type": "markdown",
190+
"id": "psychological-terminal",
191+
"metadata": {},
192+
"source": [
193+
"**Important Note:**\n",
194+
"- For accessing values from dictionary, we use say \n",
195+
"```car['brand']``` \n",
196+
"- Reduced to ```car.get('brand')```"
197+
]
198+
},
199+
{
200+
"cell_type": "markdown",
201+
"id": "color-reputation",
202+
"metadata": {},
203+
"source": [
204+
"---"
205+
]
206+
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": null,
210+
"id": "collectible-naples",
211+
"metadata": {},
212+
"outputs": [],
213+
"source": []
214+
}
215+
],
216+
"metadata": {
217+
"kernelspec": {
218+
"display_name": "Python 3",
219+
"language": "python",
220+
"name": "python3"
221+
},
222+
"language_info": {
223+
"codemirror_mode": {
224+
"name": "ipython",
225+
"version": 3
226+
},
227+
"file_extension": ".py",
228+
"mimetype": "text/x-python",
229+
"name": "python",
230+
"nbconvert_exporter": "python",
231+
"pygments_lexer": "ipython3",
232+
"version": "3.9.1"
233+
}
234+
},
235+
"nbformat": 4,
236+
"nbformat_minor": 5
237+
}

Day-27-TkinterGUI/bmi_calculator.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from tkinter import *
2+
3+
window = Tk()
4+
window.title('BMI Calculator')
5+
window.minsize(width=300, height=300)
6+
window.config(padx=100, pady=100)
7+
FONT = ("Comic Sans MS",14,'bold')
8+
9+
# Label
10+
label1 = Label(text='Enter height:', font=FONT)
11+
label1.grid(row=1,column=1)
12+
label2 = Label(text='Enter weight:', font=FONT)
13+
label2.grid(row=2,column=1)
14+
label3 = Label(text='cm', font=FONT)
15+
label3.grid(row=1,column=3)
16+
label4 = Label(text='kg', font=FONT)
17+
label4.grid(row=2,column=3)
18+
label5 = Label(text= ' ', font=("Comic Sans MS",14,'normal'))
19+
label5.grid(row=4,column=1)
20+
label6 = Label(text=' ', font=("Comic Sans MS",14,'normal'))
21+
label6.grid(row=5,column=1)
22+
23+
# Button
24+
def onClick():
25+
bmi = float(weight.get()) / (float(height.get())/100)**2
26+
if(bmi <= 18.5):
27+
comment = "Underweight"
28+
elif(bmi <= 25):
29+
comment = "Normal weight"
30+
elif(bmi <= 30):
31+
comment = "Overweight"
32+
elif(bmi <= 35):
33+
comment = "Obese"
34+
else:
35+
comment = "Clinically obese"
36+
label5.config(text= f'BMI = {bmi:.2f}')
37+
label6.config(text=f'{comment}')
38+
39+
button = Button(text='Calculate', command=onClick, font=FONT)
40+
button.grid(row=3,column=2)
41+
42+
# Entry
43+
height = Entry(width=3, font=FONT)
44+
height.grid(row=1,column=2)
45+
height.focus()
46+
weight = Entry(width=3, font=FONT)
47+
weight.grid(row=2,column=2)
48+
49+
window.mainloop()

Day-27-TkinterGUI/mile_to_km.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from tkinter import *
2+
3+
window = Tk()
4+
window.title('Unit Conversion')
5+
window.minsize(width=300, height=300)
6+
window.config(padx=100, pady=100)
7+
FONT = ("Comic Sans MS",14,'bold')
8+
9+
# Label
10+
label1 = Label(text='0', font=FONT)
11+
label1.grid(row=2,column=2)
12+
label2 = Label(text='Miles', font=FONT)
13+
label2.grid(row=1,column=3)
14+
label3 = Label(text='Kilometres', font=FONT)
15+
label3.grid(row=2,column=3)
16+
label4 = Label(text='is equivalent to:', font=FONT)
17+
label4.grid(row=2,column=1)
18+
19+
# Button
20+
def onClick():
21+
converted_value = float(user_input.get()) * 1.609
22+
label1.config(text=f'{converted_value:.2f}')
23+
# Calls onClick whenever clicked
24+
button = Button(text='Calculate', command=onClick, font=FONT)
25+
button.grid(row=3,column=2)
26+
27+
# Entry
28+
user_input = Entry(width=5, font=FONT)
29+
user_input.grid(row=1,column=2)
30+
user_input.focus()
31+
32+
window.mainloop()

Day-27-TkinterGUI/practice.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from tkinter import *
2+
3+
window = Tk()
4+
window.title('First GUI')
5+
window.minsize(width=500, height=500)
6+
window.config(padx=100, pady=200)
7+
8+
# Label
9+
label = Label(text='Old Text', font=('arial',18,'bold'))
10+
label.config(text='New Text')
11+
label.grid(row=1,column=1)
12+
13+
# Button
14+
def onClick():
15+
print("Do something")
16+
label.config(text=user_input.get())
17+
# Calls onClick whenever clicked
18+
button1 = Button(text='Click Me', command=onClick)
19+
button1.grid(row=2,column=2)
20+
button2 = Button(text='Click Me', command=onClick)
21+
button2.grid(row=1,column=3)
22+
23+
user_input = Entry(width=30)
24+
user_input.grid(row=3,column=4)
25+
26+
window.mainloop()

0 commit comments

Comments
 (0)