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

  Cross-cultural opportunities and conflicts in canada

Short Paper on Cross-cultural Opportunities and Conflicts in Canada.

  Sociology theory questions

Sociology are very fundamental in nature. Role strain and role constraint speak about the duties and responsibilities of the roles of people in society or in a group. A short theory about Darwin and Moths is also answered.

  A book review on unfaithful angels

This review will help the reader understand the social work profession through different concepts giving the glimpse of why the social work profession might have drifted away from its original purpose of serving the poor.

  Disorder paper: schizophrenia

Schizophrenia does not really have just one single cause. It is a possibility that this disorder could be inherited but not all doctors are sure.

  Individual assignment: two models handout and rubric

Individual Assignment : Two Models Handout and Rubric,    This paper will allow you to understand and evaluate two vastly different organizational models and to effectively communicate their differences.

  Developing strategic intent for toyota

The following report includes the description about the organization, its strategies, industry analysis in which it operates and its position in the industry.

  Gasoline powered passenger vehicles

In this study, we examine how gasoline price volatility and income of the consumers impacts consumer's demand for gasoline.

  An aspect of poverty in canada

Economics thesis undergrad 4th year paper to write. it should be about 22 pages in length, literature review, economic analysis and then data or cost benefit analysis.

  Ngn customer satisfaction qos indicator for 3g services

The paper aims to highlight the global trends in countries and regions where 3G has already been introduced and propose an implementation plan to the telecom operators of developing countries.

  Prepare a power point presentation

Prepare the power point presentation for the case: Santa Fe Independent School District

  Information literacy is important in this environment

Information literacy is critically important in this contemporary environment

  Associative property of multiplication

Write a definition for associative property of multiplication.

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