Python Forum

Full Version: Multiply function using Tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Everyone, i am stuck at the multiply function using Tkinter, any help is greatly appreciated. Addition seems to be fine.

from tkinter import *


def calc_sum(event):
    num1 = int(num1Entry.get())
    num2 = int(num2Entry.get())

    sum = num1 + num2

    sumTotalEntry.delete(0,sum)

    sumTotalEntry.insert(0,sum)

def multiply_num(event):
    num1= num1Entry.get()
    num2= num2Entry.get()

    mul= num1 * num2

    #mulTotalEntry.delete(0,mul)
    mulTotalEntry.insert(0,mul)

root=Tk()


num1Entry = Entry(root)
num1Entry.grid(row=0)

plusLabel = Label(root,text="+",font=("Verdana",8))
plusLabel.grid(row=0,column=1)

num2Entry = Entry(root)
num2Entry.grid(row=0,column=2)

equalButton=Button(root,text="=",font=("Verdana",8))
equalButton.bind('<Button-1>',calc_sum)
equalButton.grid(row=0,column=3)

sumTotalEntry=Entry(root)
sumTotalEntry.grid(row=0,column=4)



mul1Entry = Entry(root)
mul1Entry.grid(row=1)

mulLabel = Label(root,text="*",font=("Verdana",8))
mulLabel.grid(row=1,column=1)

mul2Entry = Entry(root)
mul2Entry.grid(row=1,column=2)

equalButton2=Button(root,text="=",font=("Verdana",8))
equalButton2.bind('<Button-1>',multiply_num)
equalButton2.grid(row=1,column=3)

mulTotalEntry=Entry(root)
mulTotalEntry.grid(row=1,column=4)

root.mainloop()
Below is the error i get

Output:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 1549, in __call__ return self.func(*args) File "C:/Users/auppu/Downloads/Py/2.py", line 18, in multiply_num mul= num1 * num2 TypeError: can't multiply sequence by non-int of type 'str'
Please use python and output tags when posting code and results. The icode tags are for inline code.
add print statements before line 18:
print('num1: {}, type num1: {}, num2: {}, type num2: {}'.format(num1, type(num1), num2, type(num2)))
(Dec-20-2018, 11:48 PM)Larz60+ Wrote: [ -> ]add print statements before line 18:
print('num1: {}, type num1: {}, num2: {}, type num2: {}'.format(num1, type(num1), num2, type(num2)))

no luck

still see the same error

__
    return self.func(*args)
  File "C:\Users\auppu\Downloads\Py\2.py", line 20, in multiply_num
    mul= num1 * num2
TypeError: can't multiply sequence by non-int of type 'str'
I think the point of Larz's comment was to get information on the state of the program right before the error, so that you could tell which value is causing the problem. Although, it would help if you used {!r} in place of {} in his format statement, which would distinguish '5' from 5.

Based on the error, I expect it's num2. I would also note that you are not converting to integer in mulitply_num as you are in calc_sum.
You should learn using Tkinter Variables
http://effbot.org/tkinterbook/variable.htm

In your case you need to link the entry with DoubleVar or if you want to work only with integers - IntVar