Home > other >  Can i do a calculation in two text boxes and get the result in an other one
Can i do a calculation in two text boxes and get the result in an other one

Time:05-20

i am new to programming and i am programming my first project and i want to know can i do a calculation in two text boxes and get the result to another one And thank you guys

Here is my code:

from Tkinter import *
import Tkinter as tk


def add(x, y):
    return x y


root = tk.Tk()
root.geometry('340x275 50 50')
root.title('Calculator')  
root.resizable(True, True)
root.iconbitmap("C://123//
icon.ico")

num_1 = Label(root, 
text="First number :")
num_1.grid(row=1, column=1)
num_2 = Label(root, 
text="Second number :")
num_2.grid(row=1, column=3)

textBox1 = Text(root, 
height=2, width=10)
textBox1.grid(row=1, column=2)
input1 = textBox1.get("1.0", 
END)

textBox2 = Text(root, 
height=2, width=10)
textBox2.grid(row=1, column=4)
input2 = textBox2.get("1.0", 
END)

textBox_result = Text(root, 
height=2, width=40)
textBox_result.grid(row=3, 
column=2)

btn_add = Button(root, 
text=" ", command=add(input1, 
input2))
btn_add.grid(row=2, column=1)


root.mainloop()

CodePudding user response:

As newbie tutorial. Try this;

from tkinter import *

root = Tk()
root.title("Sum Two Numbers With GUI Using Python Source Code")
width = 400
height = 280
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d %d %d" % (width, height, x, y))
root.resizable(0, 0)

#==============================METHODS========================================
def calculate():
    if not num1.get() and not num2.get():
        print("Please enter a number")
    else:
        res=int(num1.get()) int(num2.get())
        lbl_text.config(text = "The answer is %d" % (res))
        num1.set=""
        num2.set=""

#==============================FRAMES=========================================
Top = Frame(root, bd=2,  relief=RIDGE)
Top.pack(side=TOP, fill=X)
Form = Frame(root, height=200)
Form.pack(side=TOP, pady=20)

#==============================LABELS=========================================
lbl_title = Label(Top, text = "Sum Two Numbers With GUI Using Python Source Code", font=('arial', 12))
lbl_title.pack(fill=X)
lbl_num1 = Label(Form, text = "Number 1:", font=('arial', 14), bd=15)
lbl_num1.grid(row=0, sticky="e")
lbl_num2 = Label(Form, text = "Number 2:", font=('arial', 14), bd=15)
lbl_num2.grid(row=1, sticky="e")
lbl_text = Label(Form)
lbl_text.grid(row=2, columnspan=2)

#==============================ENTRY WIDGETS==================================
num1 = Entry(Form, font=(14))
num1.grid(row=0, column=1)
num2 = Entry(Form, font=(14))
num2.grid(row=1, column=1)

#==============================BUTTON WIDGETS=================================
btn_calculate = Button(Form, text="Calculate", width=45, command=calculate)
btn_calculate.grid(pady=25, row=3, columnspan=2)

Another one easy for you.

import tkinter as tk
from functools import partial

def findsum(l3, num1, num2):
    n1 = int(num1.get())
    n2 = int(num2.get())
    n3 = n1   n2
    l3.config(text="Result = %d" % n3)
    return


root = tk.Tk()
root.title("Xiith.com")
root.geometry("700x500")

number1 = tk.StringVar()
number2 = tk.StringVar()

l1 = tk.Label(root, text="Enter 1st number").place(x=20, y=60)
l2 = tk.Label(root, text="Enter 2nd number").place(x=20, y=120)
t1 = tk.Entry(root, textvariable=number1).place(x=200, y=60)
t2 = tk.Entry(root, textvariable=number2).place(x=200, y=120)

labelResult = tk.Label(root)
labelResult.place(x=250, y=200)

findsum = partial(findsum, labelResult, number1, number2)

b1 = tk.Button(root, text="ADD", command=findsum).place(x=200, y=300)
b2 = tk.Button(root, text="Cancel", command=root.destroy).place(x=250, y=300)

root.mainloop() 
  • Related