In: Computer Science
Exercise: Advanced counting
Complete the program below in the answer box by entering functions so the program can modify the value variable either by adding 1, adding 5, subtracting 1, or subtracting 5. There should only be one function updating the value. The functions subtract_one, add_one, etc. are helper functions as described above; they should simply call the value update function.
from tkinter import *
from tkinter.ttk import *
# Write your functions here
def main():
"""Set up the GUI and run it"""
global value, value_label
window = Tk()
value = 0
value_label = Label(window, text=str(value))
value_label.grid(row=0, column=0, columnspan=4)
subtract_five_button = Button(window, text="-5",
command=subtract_five)
subtract_five_button.grid(row=1, column=0)
subtract_one_button = Button(window, text="-1",
command=subtract_one)
subtract_one_button.grid(row=1, column=1)
add_one_button = Button(window, text="+1", command=add_one)
add_one_button.grid(row=1, column=2)
add_five_button = Button(window, text="+5", command=add_five)
add_five_button.grid(row=1, column=3)
window.mainloop()
main()
1. Using one function
Code:
from tkinter import *
from tkinter.ttk import *
global value, value_label
window = Tk()
counter = 0 #declare counter and intialize with 0
value = StringVar() #declare a variable for update
value.set(str(0)) #set first value with 0
value_label = Label(window, textvariable=value) #declare label for counter
value_label.grid(row=0, column=0, columnspan=4)
def update_counter(button): #this function is used to add value to the counter according to the clicked button
global counter
temp = counter
counter = counter + int(button.cget('text')) #here update counter value with text of clicked button
value.set(str(counter)) #update new value to label
def main():
#here whenever you click any button then update_counter function called with button as an argument
subtract_five_button = Button(window, text="-5", command=lambda:update_counter(subtract_five_button))
subtract_five_button.grid(row=1, column=0)
subtract_one_button = Button(window, text="-1", command=lambda:update_counter(subtract_one_button))
subtract_one_button.grid(row=1, column=1)
add_one_button = Button(window, text="+1", command=lambda:update_counter(add_one_button))
add_one_button.grid(row=1, column=2)
add_five_button = Button(window, text="+5", command=lambda:update_counter(add_five_button))
add_five_button.grid(row=1, column=3)
window.mainloop()
main() #call main function to set gui to the window
Output:
After clicking +5 Button:
2. Using 4 different functions
Code:
from tkinter import *
from tkinter.ttk import *
global value, value_label
window = Tk()
counter = 0 #declare counter and intialize with 0
value = StringVar() #declare a variable for update
value.set(str(0)) #set first value with 0
value_label = Label(window, textvariable=value) #declare label for displaying result
value_label.grid(row=0, column=0, columnspan=4)
def update_label(counter):#whenever any button is clicked then this function is called
value.set(str(counter)) #to set new value to the result
def subtract_five():
global counter
counter = counter-5 #update counter value by subtracting 5
update_label(counter) #then this function is clled to display the new result
def subtract_one():
global counter
counter = counter-1 #update counter value by subtracting 1
update_label(counter) #then this function is clled to display the new result
def add_one():
global counter
counter = counter+1 #update counter value by adding 1
update_label(counter) #then this function is clled to display the new result
def add_five():
global counter
counter = counter+5 #update counter value by subtracting 1
update_label(counter) #then this function is clled to display the new result
def main():
subtract_five_button = Button(window, text="-5", command=subtract_five) #subtract_five() called when button clicked
subtract_five_button.grid(row=1, column=0)
subtract_one_button = Button(window, text="-1", command=subtract_one) #subtract_one() called when button clicked
subtract_one_button.grid(row=1, column=1)
add_one_button = Button(window, text="+1", command=add_one) #add_one() called when button clicked
add_one_button.grid(row=1, column=2)
add_five_button = Button(window, text="+5", command=add_five) #add_five() called when button clicked
add_five_button.grid(row=1, column=3)
window.mainloop()
main() #call main function to set gui to the window
Output:
After clicking +1 Button: