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