In: Computer Science
For this assignment, you will develop working examples
of a graphical user interface (GUI) and event handling and that
demonstrate the following:
Working code with screenshots of a Python GUI
application that includes 5 design widgets of your
choosing
Working code with screenshots of event handling in
Python based on 3 events of your choosing
Be sure to include a brief narrative of your code
where you explain what the code is doing.
Documentation Guidelines:
Use good programming style (e.g., indentation for readability) and document each of your program parts with the following items (the items shown between the '<' and '>' angle brackets are only placeholders. You should replace the placeholders and the comments between them with your specific information). Your cover sheet should have some of the same information, but what follows should be at the top of each program's sheet of source code. Some lines of code should have an explanation of what is to be accomplished, this will allow someone supporting your code years later to comprehend your purpose. Be brief and to the point. Start your design by writing comment lines of pseudocode. Once that is complete, begin adding executable lines. Finally run and test your program.
written in python code.
The source code is given below:
"""
File: gui.py
Used widgets: Label,Button,Radiobutton,Entry,ScrolledText
Used events: click on buttons,radio button selection, Keyevent on
entry widget
"""
# import of tkinter library
import tkinter as tk
import tkinter.scrolledtext as st
# global variable
s=0
# keyevent is fired when ENTER key is pressed after typing in entry
widget
def keyEvent(event):
showText()
# to display the item selected and its price within
ScrolledText
def showText():
# using global variable
global s
# gets value from entry widget
s1=e1.get()
# checks if something is typed inside entry widget
if(s1!=''):
# if typed then converts to int and calculates total
s=s+int(s1)
# inserts into ScrolledText
txt.insert('insert',"You choose:"+v.get()+" "+e1.get()+"\n")
# clears the entry widget
e1.delete(0,'end')
# to display total inside ScrolledText
def showTotal():
global s
txt.insert('insert',"--------------------------------\n");
txt.insert('insert',"Total spent:"+str(s)+"\n")
# root window from tkinter
root=tk.Tk()
# v declared as command variable to store a string while being used
with Radiobutton
# the value of Radiobutton will be available in variable v
v=tk.StringVar()
# radiobutton with value "Cheese" selected initially
v.set("Cheese")
# initialization of Radiobutton 1
rb1=tk.Radiobutton(root,text="Milk",padx =
20,variable=v,value="Milk")
# Radiobutton 1 placed in grid(0,0)
rb1.grid(row=0,column=0)
# initialization of Radiobutton 2
rb2=tk.Radiobutton(root,text="Cheese",padx =
20,variable=v,value="Cheese")
# Radiobutton 2 placed in grid(0,1)
rb2.grid(row=0,column=1)
# initialization of Radiobutton 3
rb3=tk.Radiobutton(root,text="Fruits",padx =
20,variable=v,value="Fruits")
# Radiobutton 3 placed in grid(0,3)
rb3.grid(row=0,column=2)
# initialization of label showing text and placed in
grid(1,0)
tk.Label(root,text="Amount Spent").grid(row=1,column=0)
# initialization of entry widget to accept number entry
e1=tk.Entry(root)
# placed in grid(1,1)
e1.grid(row=1,column=1)
# keyEvent bound to entry e1 so that on pressing ENTER key on it
will be fired
e1.bind("<Return>",keyEvent)
#initialization of Button1 widget
b1=tk.Button(root,text="ADD MONEY
SPENT",command=showText,width=40)
# placed in grid(1,2)
b1.grid(row=1,column=2)
#initialization of Button2 widget
b2=tk.Button(root,text="TOTAL MONEY
SPENT",command=showTotal,width=40)
# placed in grid(2,2)
b2.grid(row=2,column=2)
#initialization of ScrolledText
txt=st.ScrolledText(root,height=6)
# placed in grid(3,0) with spanning over 3 columns
txt.grid(row=3,column=0,columnspan=3)
# window size of 600x200 with initial position at (300,300)
root.geometry("600x200+300+300")
#window mainloop
root.mainloop()
##########################
The source code screen shot is given below for indentation reference:
The output screen shot is given below: