Calculator application using the grid layout manager

Assignment Help Other Subject
Reference no: EM133091574

User Interfaces and Event Driven Programming

Exercises

Question 1. Create a new, empty .pyfile and write a short program that contains a single Button called btnthat has the following text on it: Click to start counting!The button doesn't have to do anything at the moment - just get it up and running like this:

Question 2. In the same file, create a global variable (i.e. a variable in the main body of the program - just below your import statement would be fine) called counter and assign this variable an initial value of 0.

Now add the following function to your code:

def incrementCounter():
global counter
counter += 1
btn.configure( text = str(counter) )

Now add a command section to your btnButton so that it runs the incrementCounterfunction every time the button is clicked - i.e. btn = Button(win, text='Click!', command=incrementCounter)

Run the program and click the button a few times - the text on the button should change to the new value of the counter variable after each click.

Question 3. Create a new, empty python file and use the grid geometry manager to create an application with two Labels and Entries for the user to enter a username and password. Below these labels and entries place a button that users can click to 'Log in' - for example:

The log in button should span two columns using the columnspanattribute. Also, if you make the log in button sticky (i.e. stick the sides of the widget to the north/south/east/west [aka top/bottom/left/right] edges of the grid location, then it'll fill the entire row - for example:

login_button.grid(row=2, column=0, columnspan=2, sticky="nsew")

Make the password Entry show asterisks instead of the entered text by specifying show="*"

One final thing we can do is to add a callback to the log in button so that it displays an alert based on whether the provided username and password match some values (pick whatever you want for a valid username and password) - that is, whether the log in was successful or not.

To do this, you can simply use some code like this (yes, we must explicitly import messagebox):

from tkinter import messagebox
if (username == VALID_USERNAME and password == VALID_PASSWORD):
tk.messagebox.showinfo('Success', 'Log in successful!')
else:
tk.messagebox.showerror('Login failed', 'Bad username or password.')

Question 4. Try to create a Calculator application using the grid layout manager. Place a text Entry widget at the top, and a few buttons (1,2,3), (4,5,6), (7,8,9) and a row with + - * /.

Place an "Equals" button somewhere. When you click a button it should add that text to the top Entry, and when you click "Equals" it should replace the top entry with the result of evaluating that string.

Here's a simple example of something similar that adds some entries and changes the text on a button (but the values are in Entry widgets and not taken from the text of the button - you would need to 'add to' the string in your tip Entry widget, and then later when the user pushes the = button you would have to evaluate it):

import tkinter as tk
win = tk.Tk()

e1 = tk.Entry(win)
e1.grid(row=0, column=0, sticky='nsew')

e2 = tk.Entry(win)
e2.grid(row=1, column=0, sticky='nsew')

def calc_sum():
the_sum = int(e1.get()) + int(e2.get())
equals_button['text'] = str(the_sum)

equals_button = tk.Button(win, text="???", command=calc_sum)
equals_button.grid(row=2, column=0, sticky='nsew')

win.mainloop()

Here's an example of evaluating a string:

user_entry = "5 + 15 / 2 - 1"
result = eval(user_entry)
print('The result of that calculation is:', result)

If you put the 'text-from-a-button' idea together with the evaluation then you'll have a working calculator, which is somewhat similar to what you'll need to do for Assignment 2.

Attachment:- User Interfaces and Event Driven Programming.rar

Reference no: EM133091574

Questions Cloud

Prepare journal entries to record the preceding transactions : Mayfair Co. completed the following transactions and uses a perpetual inventory system. Prepare journal entries to record the preceding transactions
Developing and managing brands : Identify the factors that need to be considered when developing and managing brands.
Prepare journal entries under each of the cases : The share options will vest as follows: End of 2020, if earnings in 2020 increased by 19%. Prepare journal entries under each of the cases
Acquired knowledge in marketing and marketing research : The focus of this TMA is on application of acquired knowledge in marketing and marketing research.
Calculator application using the grid layout manager : Create a new, empty .pyfile and write a short program that contains a single Button called btnthat has the following text on it: Click to start counting
Performing an ife matrix : According to James Van Horne, what are the three decisions that comprise the functions of finance? Describe each function.
Phase of consumer buying process : Now describe five brand touchpoints using one touchpoint for each phase of consumer buying process.
Describing the key learning from the it for managers : Write a short reflection, describing the key learning from the IT for managers course and future trends in IT.
Pricing strategies and simulated business : The marketing mix is a key set of tools within the microenvironment, which helps an organization produce the response it wants from their target market.

Reviews

Write a Review

Other Subject Questions & Answers

  Describe the behaviors that are used or demonstrated

The Texas Higher Education Coordinating Board has defined the core objectives of "Personal Responsibility" as the ability to connect choices, actions.

  Explain two challenges an administrator should consider

Discuss at least two challenges an administrator should consider when preparing a trend analysis over a five year period. Justify your response.

  Google slide deck presentation

You will design a google slide deck presentation about the piece, biographical information about the artist, and why the piece speaks to you.

  Examine the risk factors and the various protective factors

Examine the risk factors and the various protective factors that may mitigate the level of risk.

  How are mental health programs delivered in prison

Assume the role of a correctional support staff member dedicated to assisting prisoners with reaching personal goals for probation and inclusion into society.

  What is an example of how the leader champions quality

What is an example of how this leader champions quality and safety in the organization or public health system?

  List eight most abundant elements in the continental crust

List the eight most abundant elements in the continental crust. Explain some of the tips on assessing job candidates

  Directly involved in the litigation

A brief filed by a third party who is not directly involved in the litigation but who has an interest in the outcome of a case is called

  Discuss the safety and effectiveness of alternative

Discuss the safety and effectiveness of alternative and complementary medicine for the treatment of specific illnesses such as cancer, diabetes.

  How gendered violence movment and policy develop in china

An anthropological article related to your final project, which you will find on the Anthrosource database. Approximately ¾ page (or so, longer is fine), single-spaced summary of that article and its relationship with your project and the material..

  Critically evaluate and synthesise theories and models

Evaluate and synthesise theories and models in practice to improve performance through effective leadership, operations management and financial analysis

  Show the relationship between the OT and NT practices

Three words should be taken from the OT and three from the NT. Show the relationship between the OT and NT practices in worship

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd