简体   繁体   中英

What is the reason of this error I'm getting when using tkinter for a math app

Im making a program that will do most of my homework. Im trying to add some ui and it gives errors in my code. Please tell what's wrong. Make it easy enough for a 13 year old to understand because I'm new to python. This gives an error only when i use canvas. If i use window, then it doesn't but i want to use canvas because I can change their position more accurately

from tkinter import *

root=Tk()
canvas1 = Canvas(root, width = 400, height = 300)
canvas1.pack()
entry1 = Entry (root) 
canvas1.create_window(200, 140, window=entry1)
entry2 = Entry (root) 
canvas1.create_window(200, 180, window=entry2)
entry3 = Entry (root) 
canvas1.create_window(200, 220, window=entry3)

def getvalue():
      p=entry1.get()
      r=entry2.get()
      t=entry3.get()      
      labelans = Label(root, text = float(p*r*t)/100)
      canvas1.create_window(200, 230, window=labelans)

label1 = Label(root, text="Time")
canvas1.create_window(437, 220, window=label1)
label2 = Label(root, text="Rate")
canvas1.create_window(437,180, window=label2)
label3 = Label(root, text="Principal")
canvas1.create_window(465, 140, window=label3)

button1 = Button(text='Solve!', bg="red", command=getvalue)
canvas1.create_window(200, 300, window=button1)

mainloop()

*And it gives this error

Exception in Tkinter callback
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue
    labelans = Label(root, text = float(p*r*t)/100)
TypeError: can't multiply sequence by non-int of type 'str'
Exception in Tkinter callback
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue
    labelans = Label(root, text = float(p*r*t)/100)
TypeError: can't multiply sequence by non-int of type 'str'
Exception in Tkinter callback
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue
    labelans = Label(root, text = float(p*r*t)/100)
TypeError: can't multiply sequence by non-int of type 'str'
Exception in Tkinter callback
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue
    labelans = Label(root, text = float(p*r*t)/100)
TypeError: can't multiply sequence by non-int of type 'str'
Exception in Tkinter callback
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue
    labelans = Label(root, text = float(p*r*t)/100)
TypeError: can't multiply sequence by non-int of type 'str'*

On line 19-21, I added float . Line 22, I removed float . Also for LABEL widget II changed x, y location. in line 23, I also place LABEL below ENTRY

Here is code:

from tkinter import *

root=Tk()
root.title('Math')

canvas1 = Canvas(root, width = 400, height = 320)
canvas1.pack()

entry1 = Entry (root) 
canvas1.create_window(200, 140, window=entry1)

entry2 = Entry (root) 
canvas1.create_window(200, 180, window=entry2)

entry3 = Entry (root) 
canvas1.create_window(200, 220, window=entry3)

def getvalue():
      p = float(entry1.get())
      r = float(entry2.get())
      t = float(entry3.get()      )
      labelans = Label(root, text=(p*r*t)/100)
      canvas1.create_window(200, 250, window=labelans)

label1 = Label(root, text="Time") 
canvas1.create_window(120, 140, window=label1)

label2 = Label(root, text="Rate")
canvas1.create_window(120,180, window=label2)

label3 = Label(root, text="Principal")
canvas1.create_window(110, 220, window=label3)

button1 = Button(text='Solve!', bg="red", command=getvalue)
canvas1.create_window(200, 300, window=button1)

mainloop()

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM