Exercise 200

Using the Tkinter Python library, write a Python program that displays a dialog box asking the user to enter an integer and return its double: 

Solution


from tkinter import *

# method that performs the action
def action ():
# retrieve the value of the first input field
N = int (entryNumber1.get ())
N2 = 2 * N
# deletion of the existing value in the second field
entryNumber2.delete (0, END)
# inserting the new value N2 = 2 * N
entryNumber2.insert (0, N2)

# creation of the main window
fen = Tk ()
fen.geometry ("430x200")

# Creation of the first entry input field with the associated label
lblnumber1 = Label (fen, text = "Enter the value of N")
lblnumber1.place (x = 50, y = 20)
entryNumber1 = Entry (fen)
entryNumber1.place (x = 230, y = 20)

# Creation of the 2nd entry input field with the associated label
lblnumber2 = Label (fen, text = "Here is the double 2*N:")
lblnumber2.place (x = 50, y = 50)
entryNumber2 = Entry (fen)
entryNumber2.place (x = 230, y = 50)

# Creation of button that validates the action
Validate = Button (fen, text = "Validate", command = action)
Validate.place (x = 230, y = 80, width = 165)

fen.mainloop ()


Younes Derfoufi
my-courses.net

Leave a Reply