Python Forum
Multiply function using Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiply function using Tkinter
#1
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'
Reply
#2
Please use python and output tags when posting code and results. The icode tags are for inline code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
add print statements before line 18:
print('num1: {}, type num1: {}, num2: {}, type num2: {}'.format(num1, type(num1), num2, type(num2)))
Reply
#4
(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'
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,078 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  Tkinter won't run my simple function AthertonH 6 3,891 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,893 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,563 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,032 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter get function finndude 2 2,966 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,457 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  function in new window (tkinter) Dale22 7 5,170 Nov-24-2020, 11:28 PM
Last Post: Dale22
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 4,599 Oct-23-2020, 05:46 PM
Last Post: deanhystad
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,457 Oct-12-2020, 09:16 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020