GUI Project Application.

avatar

I am a newbie on programming world and I am trying to learn Python as it is the easiest and very strong programming language. I've learnt all basic theories and the programming syntax and then I have solved some problems from urionlinejudge. Now I'am trying to learn GUI Appliations and I am trying to learn Tkinter module of Python.

I watched some tutorials on youtube and I just made my first GUI application. I named it "Simple GUI". This is just a form and all the information you put on this form will store in "simpleGUI.txt" file after clicking on submit button. Let's check out the codes.


Importing tkinter and naming the application


import tkinter as tk
from tkinter import ttk

win = tk.Tk()
win.title('Simple GUI')

nb = ttk.Notebook(win)

page1 = ttk.Frame(nb)
page2 = ttk.Frame(nb)

nb.add(page1, text='ONE')
nb.add(page2, text='TWO')
nb.grid(row=0, column=0)

label_frame1 = ttk.Labelframe(page1, text='Enter your info')
label_frame1.grid(row=0, column=0)


Creating Labels for label_frame1

name_label = ttk.Label(label_frame1, text='Enter your name :')
name_label.grid(row=0, column=0, sticky=tk.W)

email_label = ttk.Label(label_frame1, text='Enter your e-mail :')
email_label.grid(row=1, column=0, sticky=tk.W)

age_label = ttk.Label(label_frame1, text='Enter your age :')
age_label.grid(row=2, column=0, sticky=tk.W)

gender_label = ttk.Label(label_frame1, text='Enter your gender :')
gender_label.grid(row=3, column=0, sticky=tk.W)


Creating Entryboxes for label_frame1

name_var = tk.StringVar()
name_entrybox = ttk.Entry(label_frame1, width=16, textvariable=name_var)
name_entrybox.grid(row=0, column=1)
name_entrybox.focus()

email_var = tk.StringVar()
email_entrybox = ttk.Entry(label_frame1, width=16, textvariable=email_var)
email_entrybox.grid(row=1, column=1)

age_var = tk.StringVar()
age_entrybox = ttk.Entry(label_frame1, width=16, textvariable=age_var)
age_entrybox.grid(row=2, column=1)

gender_var = tk.StringVar()
gender_combobox = ttk.Combobox(label_frame1, width=13, textvariable=gender_var, state='readonly')
gender_combobox['values'] = ('Male', 'Female', 'Other')
gender_combobox.grid(row=3, column=1)
gender_combobox.current(0)

label_frame2 = ttk.Labelframe(page2, text='Enter your info')
label_frame2.grid(row=0, column=0)


Creating label for label_frame2

Type_label = ttk.Label(label_frame2, text='Type ? :')
Type_label.grid(row=4, column=0, sticky=tk.W)


creating radiobtn for label_frame2

usertype = tk.StringVar()
radiobtn1 = ttk.Radiobutton(label_frame2, text='Student', value='Student', variable=usertype)
radiobtn1.grid(row=4, column=1)

radiobtn2 = ttk.Radiobutton(label_frame2, text='Teacher', value='Teacher', variable=usertype)
radiobtn2.grid(row=4, column=2)


creating checkbutton for label_frame2


checkbtn_var = tk.IntVar()
checkbtn = ttk.Checkbutton(label_frame2, text='Check if you want to subscribe our newsletter.', variable=checkbtn_var)
checkbtn.grid(row=5, columnspan=3)


creating Submit button


def action():
    username = name_var.get()
    userage = age_var.get()
    user_email = email_var.get()
    user_gender = gender_var.get()
    user_type = usertype.get()
    check_btn = checkbtn_var.get()
    if user_gender == 'Male':
        he = 'He'
    elif user_gender == 'Other':
        he = 'He'
    else:
        he = 'She'

    if check_btn == 1:
        Subscribed = 'subscribed'
    else:
        Subscribed = 'not subscribed'
    print(
        f'{username}\'s age is {userage}, gender is {user_gender}, email is {user_email}. {he} is a {user_type} and {Subscribed} to our newsletter.')

    with open('simpleGUI.txt', 'a') as f:
        f.write(
            f'{username}\'s age is {userage}, gender is {user_gender}, email is {user_email}. {he} is a {user_type} and {Subscribed} to our newsletter.\n')

    name_entrybox.delete(0, tk.END)
    email_entrybox.delete(0, tk.END)
    age_entrybox.delete(0, tk.END)


submit_button = ttk.Button(win, text='Submit', command=action)
submit_button.grid(row=6, columnspan= 2)

win.mainloop()

So the full code is here

import tkinter as tk
from tkinter import ttk

win = tk.Tk()
win.title('Simple GUI')

nb = ttk.Notebook(win)

page1 = ttk.Frame(nb)
page2 = ttk.Frame(nb)

nb.add(page1, text='ONE')
nb.add(page2, text='TWO')
nb.grid(row=0, column=0)

label_frame1 = ttk.Labelframe(page1, text='Enter your info')
label_frame1.grid(row=0, column=0)

# ****************Creating Labels for label_frame1 *************

name_label = ttk.Label(label_frame1, text='Enter your name :')
name_label.grid(row=0, column=0, sticky=tk.W)

email_label = ttk.Label(label_frame1, text='Enter your e-mail :')
email_label.grid(row=1, column=0, sticky=tk.W)

age_label = ttk.Label(label_frame1, text='Enter your age :')
age_label.grid(row=2, column=0, sticky=tk.W)

gender_label = ttk.Label(label_frame1, text='Enter your gender :')
gender_label.grid(row=3, column=0, sticky=tk.W)

# *************** Creating Entryboxes for label_frame1 ***************

name_var = tk.StringVar()
name_entrybox = ttk.Entry(label_frame1, width=16, textvariable=name_var)
name_entrybox.grid(row=0, column=1)
name_entrybox.focus()

email_var = tk.StringVar()
email_entrybox = ttk.Entry(label_frame1, width=16, textvariable=email_var)
email_entrybox.grid(row=1, column=1)

age_var = tk.StringVar()
age_entrybox = ttk.Entry(label_frame1, width=16, textvariable=age_var)
age_entrybox.grid(row=2, column=1)

gender_var = tk.StringVar()
gender_combobox = ttk.Combobox(label_frame1, width=13, textvariable=gender_var, state='readonly')
gender_combobox['values'] = ('Male', 'Female', 'Other')
gender_combobox.grid(row=3, column=1)
gender_combobox.current(0)

label_frame2 = ttk.Labelframe(page2, text='Enter your info')
label_frame2.grid(row=0, column=0)

# ****************** Creating label for label_frame2 *************

Type_label = ttk.Label(label_frame2, text='Type ? :')
Type_label.grid(row=4, column=0, sticky=tk.W)

# *************** creating radiobtn for label_frame2 *************

usertype = tk.StringVar()
radiobtn1 = ttk.Radiobutton(label_frame2, text='Student', value='Student', variable=usertype)
radiobtn1.grid(row=4, column=1)

radiobtn2 = ttk.Radiobutton(label_frame2, text='Teacher', value='Teacher', variable=usertype)
radiobtn2.grid(row=4, column=2)

# *****************creating checkbutton for label_frame2 ********************

checkbtn_var = tk.IntVar()
checkbtn = ttk.Checkbutton(label_frame2, text='Check if you want to subscribe our newsletter.', variable=checkbtn_var)
checkbtn.grid(row=5, columnspan=3)


# ************* creating Submit button *******************

def action():
    username = name_var.get()
    userage = age_var.get()
    user_email = email_var.get()
    user_gender = gender_var.get()
    user_type = usertype.get()
    check_btn = checkbtn_var.get()
    if user_gender == 'Male':
        he = 'He'
    elif user_gender == 'Other':
        he = 'He'
    else:
        he = 'She'

    if check_btn == 1:
        Subscribed = 'subscribed'
    else:
        Subscribed = 'not subscribed'
    print(
        f'{username}\'s age is {userage}, gender is {user_gender}, email is {user_email}. {he} is a {user_type} and {Subscribed} to our newsletter.')

    with open('simpleGUI.txt', 'a') as f:
        f.write(
            f'{username}\'s age is {userage}, gender is {user_gender}, email is {user_email}. {he} is a {user_type} and {Subscribed} to our newsletter.\n')

    name_entrybox.delete(0, tk.END)
    email_entrybox.delete(0, tk.END)
    age_entrybox.delete(0, tk.END)


submit_button = ttk.Button(win, text='Submit', command=action)
submit_button.grid(row=6, columnspan= 2)

win.mainloop()

Thank You



0
0
0.000
8 comments
avatar

5 years this has been happening to me, it started here, around people that are still here. Homeland security has done nothing at all, they are not here to protect us. Dont we pay them to stop shit like this? The NSA, CIA, FBI, Police and our Government has done nothing. Just like they did with the Havana Syndrome, nothing. Patriot Act my ass. The American government is completely incompetent. The NSA should be taken over by the military and contained Immediately for investigation. I bet we can get to the sources of V2K and RNM then. https://peakd.com/gangstalking/@acousticpulses/electronic-terrorism-and-gaslighting--if-you-downvote-this-post-you-are-part-of-the-problem

0
0
0.000
avatar
Don-1UP-Cheers-Cartel-250px.png

You have received a 1UP from @fsc69!

The following @oneup-cartel family members will soon upvote your post:
@oneup-curator, @vyb-curator, @pal-curator
And they will bring !PIZZA 🍕

Learn more about our delegation service to earn daily rewards. Join the family on Discord.

0
0
0.000
avatar

Congratulations @sksksk! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 50 upvotes.
Your next target is to reach 100 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Support the HiveBuzz project. Vote for our proposal!
0
0
0.000