-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
53 lines (38 loc) · 1.76 KB
/
functions.py
File metadata and controls
53 lines (38 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import tkinter as tk
# ~~~~~~~~~~~~~~~~~~ Functions ~~~~~~~~~~~~~~~~~~~~
""" Let's talk about functions, why do we need them at all ?
Well, we can use functions to make our code more readable,
more efficient and more reusable.
with functions we can use the same commands as many times as we want,
by calling the function instead of writing the same code over and over again.
Let's start with a simple examples :
"""
#Examples:
# add() function gets 2 integar arguments and returns the sum of them
def add(x, y):
return x + y # return statement is used to return a value from a function
# hello() function gets a string argument and print hello message
def hello(name):
print("Hello, {}!\nLet's code together ;)".format(name)) # format() method is used to format a string
# in this function we dont use a return statement, so the function will return None but print() function
# will print a message to the console
# create_window() function gets a string argument and create a window with the given title
def create_window(title, message="Hello World!"):
window = tk.Tk()
window.title(title)
window.geometry('500x100')
window.resizable(0,0)
window.configure(bg = 'purple')
tk.Label(window, text = "{}".format(message), bg = 'black', fg = 'purple', font=("Grinched", 40) ).pack()
tk.mainloop()
return window
# main() function is the entry point of our program
def main():
# call add() function and store the result in a variable
sum = add(2, 3)
print("Sum of 2 and 3 is : {}".format(sum))
# call hello() function
hello( "Joue")
# call create_window() function
window = create_window(title="Hello Window", message="Hey There !")
main()