Question

In: Computer Science

Exercise: Advanced counting Complete the program below in the answer box by entering functions so the...

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()

Solutions

Expert Solution

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:


Related Solutions

Rewrite the following program so    that the counting of the a's and    total count...
Rewrite the following program so    that the counting of the a's and    total count are done be    separate functions       The code you turn in should have 2 functions */ #include<stdio.h> int main(void){       char input[81];               // Input string    int a=0, total=0;           // counter variables                     // Ask for an input string    puts(" Input a string of up to 80 characters and\n"    "...
Complete the below code so that your program generates a random walk on the given graph....
Complete the below code so that your program generates a random walk on the given graph. The length of your walk should also be random. /******************************************************************/ #include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct NODE_s *NODE; typedef struct NODE_s{ char name; NODE link[10]; }NODE_t[1]; #define nodeA 0 #define nodeB 1 #define nodeC 2 #define nodeD 3 #define nodeE 4 #define nodeF 5 int main() { srandom(time(NULL)); //printf("%d\n", random()); NODE_t node[6]; node[nodeA]->name = 'A'; node[nodeB]->name = 'B'; node[nodeC]->name = 'C'; node[nodeD]->name...
Complete this question by entering your answers in the tabs below. Required 1 Required 2 Complete...
Complete this question by entering your answers in the tabs below. Required 1 Required 2 Complete the above schedule of the company’s total costs and costs per unit. (Round the per unit variable cost and fixed cost to 2 decimal places.) Units Produced and Sold 69,000 89,000 109,000 Total costs: Variable costs $213,900 Fixed costs 390,000 Total costs $603,900 $0 $0 Cost per unit: Variable cost Fixed cost Total cost per unit $0.00 $0.00 $0.00
To complete this comprehensive exercise, use the Excel spreadsheet program. In this exercise you will be...
To complete this comprehensive exercise, use the Excel spreadsheet program. In this exercise you will be using linear programming and Solver in Excel to solve the problem described in the following scenario. At Goodman Shipping, the load master wants to determine the mix of cargo to be carried on the next trip. The ship's volume limit for cargo is 100,000 cubic meters, and its weight capacity is 2,310 tons. The master has five different types of cargo from which to...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called in the code at the very bottom. So you should be able simply to run the script to test that your functions work as expected. ''' ''' * function name: say_hi * parameters: none * returns: N/A * operation: Uhh, well, just say "hi" when called. And by "say" I mean "print". * expected output: >>> say_hi() hi ''' ''' * function name: personal_hi...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called in the code at the very bottom. So you should be able simply to run the script to test that your functions work as expected. ''' ''' * function name: say_hi * parameters: none * returns: N/A * operation: Uhh, well, just say "hi" when called. And by "say" I mean "print". * expected output: >>> say_hi() hi ''' ''' * function name: personal_hi...
''' Problem 2: Functions that give answers Define and complete the functions described below. The functions...
''' Problem 2: Functions that give answers Define and complete the functions described below. The functions are called in the code at the very bottom. So you should be able simply to run the script to test that your functions work as expected. ''' ''' * function name: get_name * parameters: none * returns: string * operation: Here, I just want you to return YOUR name. The expected output below assumes that your name is Paul. Of course, replace this...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
For the below questions, write a thorough and complete response in the text box below. Go...
For the below questions, write a thorough and complete response in the text box below. Go to a popular online e-commerce site like Amazon.com. Place several items in your shopping cart, and then go to check out. When you reach the screen that asks for your credit card number, right-click on the Web browser and select “Properties.” What can you find out about the cryptosystems and protocols in use to protect this transaction? Repeat question 1 on a different Web...
Complete this question by entering your answers in the tabs below. Fill in the missing amounts...
Complete this question by entering your answers in the tabs below. Fill in the missing amounts in the following schedules. April May June Sales* $276,000 $207,000 Cash receipts: From cash sales $156,600 From sales on account† 117,300 Total cash receipts ​ Accounts payable, 12/31/x0 €654,000 Purchase of goods and services on account during 20x1 276,000 Payments of accounts payable during 20x1 Accounts payable, 12/31/x1 €872,000 Accounts receivable, 12/31/x0 ¥1,880,000 ‡ Sales on account during 20x1 4,716,000 Collections of accounts receivable...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT