Sum Two Numbers With GUI Using Python Source Code

PHP Free Projects

In this tutorial we will create a Sum Two Numbers With GUI using Python. This code will dynamically sum the two given numbers when user click the calculate button. The code use tkinter module to create a layout and widgets that can call a specific python functions. When the function is called it will launch a script that will dynamically dynamically sum the two number by the use of a special symbol +

We will be using Python programming language because it is widely used as an advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem.

Getting started:

First you will have to download & install the Python IDLE’s, here’s the link for the Integrated Development And Learning Environment for Python

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.

Then copy code that I provided below and paste it inside the IDLE text editor.

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.

  1. root = Tk()

  2. root.title("Sum Two Numbers With GUI Using Python Source Code")

  3. width = 400

  4. height = 280

  5. screen_width = root.winfo_screenwidth()

  6. screen_height = root.winfo_screenheight()

  7. x = (screen_width/2) - (width/2)

  8. y = (screen_height/2) - (height/2)

  9. root.geometry("%dx%d+%d+%d" % (width, height, x, y))

  10. root.resizable(0, 0)

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.

  1. #==============================FRAMES=========================================

  2. Top = Frame(root, bd=2, relief=RIDGE)

  3. Top.pack(side=TOP, fill=X)

  4. Form = Frame(root, height=200)

  5. Form.pack(side=TOP, pady=20)


  7. #==============================LABELS=========================================

  8. lbl_title = Label(Top, text = "Sum Two Numbers With GUI Using Python Source Code", font=('arial', 12))

  9. lbl_title.pack(fill=X)

  10. lbl_num1 = Label(Form, text = "Number 1:", font=('arial', 14), bd=15)

  11. lbl_num1.grid(row=0, sticky="e")

  12. lbl_num2 = Label(Form, text = "Number 2:", font=('arial', 14), bd=15)

  13. lbl_num2.grid(row=1, sticky="e")

  14. lbl_text = Label(Form)

  15. lbl_text.grid(row=2, columnspan=2)


  17. #==============================ENTRY WIDGETS==================================

  18. num1 = Entry(Form, font=(14))

  19. num1.grid(row=0, column=1)

  20. num2 = Entry(Form, font=(14))

  21. num2.grid(row=1, column=1)


  23. #==============================BUTTON WIDGETS=================================

  24. btn_calculate = Button(Form, text="Calculate", width=45, command=calculate)

  25. btn_calculate.grid(pady=25, row=3, columnspan=2)

Creating the Main Function

This is where the code that contains the main function of the application. This code will sum both give numbers when the button is clicked. To do that just copy and write these blocks of code.

  1. #==============================METHODS========================================

  2. def calculate():

  3. if not num1.get() and not num2.get():

  4. print("Please enter a number")

  5. else:

  6. res=int(num1.get())+int(num2.get())

  7. lbl_text.config(text = "The answer is %d" % (res))

  8. num1.set=""

  9. num2.set=""

There you have it we just created a Sum Two Numbers With GUI using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *