Skip to content

Commit 6a90137

Browse files
Create GUI app with Python, Tkinter
1 parent f6418e3 commit 6a90137

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

p22.py

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
# Create GUI app with Python, Tkinter
2+
3+
# python tkinter tutorial
4+
5+
# python's standard library
6+
# we can make GUI application using this library
7+
# pronunciation: tee-kinter, tk-inter, kinter
8+
9+
10+
# starter code
11+
# import tkinter
12+
# window=tkinter.Tk() # Tk is constructor(it will create the window)
13+
# window.mainloop() # mainloop is method
14+
# these three line will return window when we will run
15+
# we must write these above three line when we want to make GUI application using Tkinter.
16+
17+
18+
19+
20+
# import tkinter
21+
# window=tkinter.Tk()
22+
# window.title("GUI") # this will change the name of window
23+
# window.mainloop()
24+
25+
26+
27+
28+
29+
# from tkinter import * # importing tkinter libray in other way
30+
# window=Tk()
31+
# window.title("GUI")
32+
# window.mainloop()
33+
34+
35+
36+
37+
38+
# starter code
39+
import tkinter as tk
40+
from tkinter import ttk
41+
window=tk.Tk()
42+
window.title("GUI")
43+
44+
45+
# create lables ---->lables can be a text, or lable of any thing(stuff)
46+
47+
# widgets(label, button, radio buttons) ---> available in tk and ttk library
48+
# we can use tk or ttk for widgets but ttk widgets are more attractive in terms of design
49+
50+
# name_label=tk.Label(window,text="Enter Your name : ")
51+
name_lable=ttk.Label(window,text="Enter Your name : ")
52+
name_lable.grid(row=0,column=0,sticky=tk.W) # # there are two methos for set location of widget 1)pack (name_label.pack()), 2)grid(name_lable.grid(row=0,column=0))
53+
54+
email_lable=ttk.Label(window,text="Enter your E-mail : ")
55+
email_lable.grid(row=1,column=0,sticky=tk.W)
56+
57+
age_label=ttk.Label(window,text="Enter your age : ")
58+
age_label.grid(row=2,column=0,sticky=tk.W) # stiky=tk.W is used for start line from lest side window(stick leble on WEST side)
59+
60+
gender_label=ttk.Label(window,text="Gender : ")
61+
gender_label.grid(row=3,column=0,sticky=tk.W)
62+
63+
radio_lable=ttk.Label(window,text='Your occuptionption : ')
64+
radio_lable.grid(row=4,column=0,sticky=tk.W)
65+
66+
67+
68+
69+
# Create Entry box
70+
# name_entry=ttk.Entry(window,width=18)
71+
# name_entry.grid(row=0,column=1)
72+
name_var=tk.StringVar()
73+
name_entry=ttk.Entry(window,width=18,textvariable=name_var) # what ever we will write in entry box, it will be stored in name_var
74+
name_entry.grid(row=0,column=1)
75+
name_entry.focus()
76+
77+
email_var=tk.StringVar()
78+
email_entry=ttk.Entry(window,width=18,textvariable=email_var)
79+
email_entry.grid(row=1,column=1)
80+
81+
age_var=tk.IntVar()
82+
age_entry=ttk.Entry(window,width=18,textvariable=age_var)
83+
age_entry.grid(row=2,column=1)
84+
85+
86+
87+
88+
# Create Combo box
89+
gender_var=tk.StringVar()
90+
gender_combo=ttk.Combobox(window,width=15, textvariable=gender_var,state='readonly') # state is for make comobo box read only, it means we can not write inside combo box
91+
gender_combo['values']=('Male','Female','Other')
92+
gender_combo.current(0) # it is for set the value in comobo box
93+
gender_combo.grid(row=3,column=1)
94+
95+
96+
97+
98+
# create radio button
99+
occuption_var=tk.StringVar()
100+
radio_student=ttk.Radiobutton(window,text='Student', value='Student', variable=occuption_var)
101+
radio_student.grid(row=4,column=1)
102+
103+
radio_teacher=ttk.Radiobutton(window,text='Teacher', value='Teacher', variable=occuption_var)
104+
radio_teacher.grid(row=4,column=2)
105+
106+
107+
108+
109+
110+
# Create Checck Button
111+
check_var=tk.IntVar()
112+
check_button=ttk.Checkbutton(window,text="check if you want to subscribe to our newsletter",variable=check_var)
113+
check_button.grid(row=5,columnspan=3) # columnspan is used for taking column without affecting other one. If we write only column we got affected GUI in terms of space.
114+
115+
116+
117+
118+
119+
# create button
120+
# def action():
121+
# user_name=name_var.get() # here, get() method is used for getting value of variable
122+
# user_mail=email_var.get()
123+
# user_age=age_var.get()
124+
# user_gender=gender_var.get()
125+
# user_occuption=occuption_var.get()
126+
# # for check box, beccause check box will return 0 or 1.
127+
# if check_var.get()==0:
128+
# user_checked='NO'
129+
# else:
130+
# user_checked="YES"
131+
# print(f'name:{user_name}, mail-id:{user_mail}, age:{user_age}, gender:{user_gender}, occuption:{user_occuption},Subscribed:{user_checked}')
132+
# with open("data.txt","a") as data: # this code for stotre value in file
133+
# data.write(f'{user_name},{user_mail},{user_age},{user_gender},{user_occuption},{user_checked} \n' )
134+
135+
# # these three lines for deleteing values from edit box after clicking on submit button
136+
# name_entry.delete(0,tk.END)
137+
# email_entry.delete(0,tk.END)
138+
# age_entry.delete(0,tk.END)
139+
140+
# # when we want to change color of name lable after clicking on submit button
141+
# name_lable.configure(foreground="Blue") # here we can use color code instead of color name
142+
# # name_lable.configure(background="Blue")
143+
# # window.configure(background="Pink") # change background color of window
144+
# # submit_button.configure(foreground="Blue") # in this we will get error, for doing this we have to use stylish, otherwise use tk insted of ttk in submit_button=tk.Button(.....)
145+
146+
147+
148+
# write into CSV
149+
from csv import DictWriter
150+
import os
151+
def action():
152+
user_name=name_var.get() # here, get() method is used for getting value of variable
153+
user_mail=email_var.get()
154+
user_age=age_var.get()
155+
user_gender=gender_var.get()
156+
user_occuption=occuption_var.get()
157+
# for check box, beccause check box will return 0 or 1.
158+
if check_var.get()==0:
159+
user_checked='NO'
160+
else:
161+
user_checked="YES"
162+
print(f'name:{user_name}, mail-id:{user_mail}, age:{user_age}, gender:{user_gender}, occuption:{user_occuption},Subscribed:{user_checked}')
163+
164+
165+
# write to csv
166+
with open("data.csv","a",newline='') as data: # this code for stotre value in file
167+
dict_write=DictWriter(data, fieldnames=['User_name','E_mail',"Age",'Gender','Occuption','Subscribed'])
168+
if os.stat('data.csv').st_size==0:
169+
dict_write.writeheader()
170+
dict_write.writerow({
171+
"User_name" : user_name,
172+
"E_mail" : user_mail,
173+
"Age" : user_age,
174+
"Gender" : user_gender,
175+
"Occuption" : user_occuption,
176+
"Subscribed": user_checked
177+
})
178+
179+
# these three lines for deleteing values from edit box after clicking on submit button
180+
name_entry.delete(0,tk.END)
181+
email_entry.delete(0,tk.END)
182+
age_entry.delete(0,tk.END)
183+
184+
# when we want to change color of name lable after clicking on submit button
185+
name_lable.configure(foreground="Blue")
186+
187+
188+
189+
190+
submit_button=ttk.Button(window,text="Submit",command=action) # command is for calling method
191+
submit_button.grid(row=6,column=0)
192+
193+
194+
195+
window.mainloop()

0 commit comments

Comments
 (0)