Question
object oriented programming solve 2 quetions
9 Lab 8 - Graphical User Interface (GUI) The assignments for this week are to understand the use of tkinter modules to create
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer for your question in Python Programming Language.

Question 1

CODE :

import tkinter as tk

"""Creates Window and sets its dimensions"""
window = tk.Tk()
window.geometry("350x200")

"""Variables to store values from entry boxes"""
firstName = tk.StringVar()
lastName = tk.StringVar()

"""Method to quit window"""
def quitWindow():
window.destroy()

"""Method to show name"""
def show():
"""Gets first and last names and stores them in 'name'"""
name = firstName.get() + " " + lastName.get()
"""Displays name"""
resultLabel = tk.Label(window, text="Name : " + name)
resultLabel.grid(row = 3, column = 0)
"""Resets entry boxes"""
firstName.set("")
lastName.set("")

"""Required GUI components"""
firstNameLabel = tk.Label(window, text="First Name")
firstNameEntry = tk.Entry(window, textvariable = firstName, width=25, bd=2)
lastNameLabel = tk.Label(window, text="Last Name")
lastNameEntry = tk.Entry(window, textvariable = lastName, width=25, bd=2)
quitBtn = tk.Button(window, text = "Quit", command = quitWindow)
showBtn = tk.Button(window, text = "Show", command = show)

"""Sets positions for GUI Components"""
firstNameLabel.grid(row = 0, column = 0)
firstNameEntry.grid(row = 0, column = 1)
lastNameLabel.grid(row = 1, column = 0)
lastNameEntry.grid(row = 1, column = 1)
quitBtn.grid(row = 2, column = 0)
showBtn.grid(row = 2, column = 1)

window.mainloop()

SCREENSHOTS :

Please see the screenshots of the code below for indentations of the code.

I 1 UUU 1 import tkinter as tk 2 3 Creates Window and sets its dimensions 4 window = tk.Tk() 5 window.geometry(350x20030 lastName Entry = tk.Entry(window, textvariable = lastName, width=25, bd=2) 31 quitBtn = tk.Button(window, text = Quit, c

OUTPUT :

tk Х First Name Last Name Quit Show

After entering names,

tk ] X First Name John Last Name Dog Quit Show

After pressing "show"

tk х First Name Last Name Quit Name: John Doe Show

##############################

Question 2

CODE :

import tkinter as tk
from tkinter import messagebox
"""Creates Window and sets its dimensions"""
window = tk.Tk()
window.geometry("350x200")

"""Variables to store values from entry boxes"""
firstNum = tk.StringVar()
secondNum = tk.StringVar()
added = tk.StringVar()
"""Method to quit window"""
def quitWindow():
window.destroy()

"""Method to show name"""
def show():
try:
"""Gets first and second numbers and stores their sum in 'add'"""
add = int(firstNum.get()) + int(secondNum.get())
"""Displays sum"""
added.set(str(add))
except ValueError:
messagebox.showinfo("Error", "Only numbers are allowed")

"""Required GUI components"""
firstNumLabel = tk.Label(window, text="Enter Num 1: ")
firstNumEntry = tk.Entry(window, textvariable = firstNum, width=25, bd=2)
secondNumLabel = tk.Label(window, text="Enter Num 2: ")
secondNumEntry = tk.Entry(window, textvariable = secondNum, width=25, bd=2)
resultLabel = tk.Label(window, text="The sum is : ")
resultEntry = tk.Entry(window, textvariable = added, width = 25, bd = 2)
quitBtn = tk.Button(window, text = "Quit", command = quitWindow)
showBtn = tk.Button(window, text = "Show", command = show)

"""Sets positions for GUI Components"""
firstNumLabel.grid(row = 0, column = 0)
firstNumEntry.grid(row = 0, column = 1)
secondNumLabel.grid(row = 1, column = 0)
secondNumEntry.grid(row = 1, column = 1)
resultLabel.grid(row = 2, column = 0)
resultEntry.grid(row = 2, column = 1)
quitBtn.grid(row = 3, column = 0)
showBtn.grid(row = 3, column = 1)

window.mainloop()

SCREENSHOTS :

Please see the screenshots of the code below for indentations of the code. As python is a Language of indentations kindly check the indentations before execution.

H11 1 import tkinter as tk 2 from tkinter import messagebox 3 Creates Window and sets its dimensions 4 window = tk.Tk()= 2) = 30 resultLabel tk.Label(window, text=The sum is : ) 31 resultEntry tk. Entry(window, textvariable = added, width = 2

OUTPUT :

tk Enter Num 1: Enter Num 2: The sum is: Quit Show

After entering numbers and pressing "Show" button,

tk ) X Enter Num 1: 9 Enter Num 2:1 The sum is: 14 Quit Show

tk Х Enter Num 1: la Enter Num 2: The sum is : Quit Show s C except ValueError: Error x ror, On Red first Only numbers are

Any doubts regarding this can be explained with pleasure :)

Add a comment
Know the answer?
Add Answer to:
object oriented programming solve 2 quetions 9 Lab 8 - Graphical User Interface (GUI) The assignments...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I. User Interface Create a JavaFX application with a graphical user interface (GUI) based on the...

    I. User Interface Create a JavaFX application with a graphical user interface (GUI) based on the attached “GUI Mock-Up”. Write code to display each of the following screens in the GUI: A. A main screen, showing the following controls: • buttons for “Add”, “Modify”, “Delete”, “Search” for parts and products, and “Exit” • lists for parts and products • text boxes for searching for parts and products • title labels for parts, products, and the application title B. An add...

  • The Gui has all the right buttons, but from there i get lost. I need to...

    The Gui has all the right buttons, but from there i get lost. I need to know whats wrong with my assignment can someone please help. The code I have so far is listed below, could you please show me the errors in my code. PYTHON Create the GUI(Graphical User Interface). Use tkinter to produce a form that looks much like the following. It should have these widgets. Temperature Converter GUI Enter a temperature (Entry box)                 Convert to Fahrenheit...

  • JAVA Developing a graphical user interface in programming is paramount to being successful in the business...

    JAVA Developing a graphical user interface in programming is paramount to being successful in the business industry. This project incorporates GUI techniques with other tools that you have learned about in this class. Here is your assignment: You work for a flooring company. They have asked you to be a part of their team because they need a computer programmer, analyst, and designer to aid them in tracking customer orders. Your skills will be needed in creating a GUI program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT