Home > Software engineering >  ValueError: invalid literal for int() with base 10: 'm:'
ValueError: invalid literal for int() with base 10: 'm:'

Time:12-08

I'm getting an error for the else statement but not for the if statement, and they are supposed to be the same. I'm not able to point out why there is an error.

Here is the error:

line 27, in <module>
Y.append(int(i))
ValueError: invalid literal for int() with base 10: 'm:'

And here is the code, I still didn't finish it, I'm still checking out the errors:

from tkinter import *

root = Tk()
root.title("Y=mX c Graph")

def startfunc():
    string =''
    for x in Y:
        string = string  str(x)  '\n'
        my_label.config(text=string)

X=[]
Y=[]

for x in range(2):
    e = Entry(root)
    e.grid(row=0, column=x, padx=5, pady=10)
    if(x==1):
        e.insert(0,"X:")
        i = e.get()
        i.lstrip(":")
        X.append(int(i))
    else:
        e.insert(0,"m:")
        i = e.get()
        i.lstrip(":")
        Y.append(int(i))

button = Button(root, text="Start", command=startfunc)
button.grid(row=0,column =3,padx=5,pady=10)
my_label = Label(root,text='')
root.mainloop()

CodePudding user response:

lstrip(':') removes : only if it at the beginning of string but you have : as second char. Maybe you need split(":") to create two strings - before : and after :. OR maybe you should slice it i = i[2:] to remove two chars from the beginning.

But there is other problem. Entry doesn't work like input() - it doesn't wait for your data. It only informs tkinter what it has to display in window. If you use get() directly after Entry then you get empty string or only "m:". You have to use .get() in startfunc


Minimial working example.

I put X:, m: as Labels above Entry so I don't have to remove it from value which I get from Entry.

For two Entries it is simpler to create them without for-loop. And code is more readable.

But if you would have more Entries then I would use for-loop with list or tuple instead of range()

for number, text in enumerate(["X:", "m:"]):

Full code:

import tkinter as tk  # PEP8: `import *` is not preferred

# -- functions ---

def startfunc():
    new_x = entry_x.get()
    new_m = entry_m.get()
    
    X.append(int(new_x))
    Y.append(int(new_m))

    string = "\n".join([str(i) for i in X])
    label_x.config(text=string)
    
    string = "\n".join([str(i) for i in Y])
    label_m.config(text=string)

# --- main ---

X = []  # PEP8: spaces around `=`
Y = []

root = tk.Tk()
root.title("Y=mX c Graph")

# ---

l = tk.Label(root, text="X:")
l.grid(row=0, column=0, padx=5, pady=10)

entry_x = tk.Entry(root)
entry_x.grid(row=1, column=0, padx=5, pady=10)

l = tk.Label(root, text="m:")
l.grid(row=0, column=1, padx=5, pady=10)

entry_m = tk.Entry(root)
entry_m.grid(row=1, column=1, padx=5, pady=10)

# ---

button = tk.Button(root, text="Start", command=startfunc)
button.grid(row=1, column=3, padx=5, pady=10)

# ---

label_x = tk.Label(root)
label_x.grid(row=2, column=0, padx=5, pady=10)

label_m = tk.Label(root)
label_m.grid(row=2, column=1, padx=5, pady=10)

# ---

root.mainloop()

enter image description here

  • Related