1

The error code I'm getting is;

line 83, in qu2
wins = float(wins)
ValueError: could not convert string to float: 

This is the code that the error appears in

                wins = wins_box.get()
                draws = draws_box.get()
                loses = losses_box.get()
                wins = float(wins)
                draws = float(draws)
                loses = float(loses)
                total_score = 0
                total_score = total_score + wins * 3
                total_score = total_score + draws * 2
                total_score = total_score + loses

                yourvar = tkinter.StringVar()
                yourvar.set(total_score)
                totalscore1 = tkinter.Label(q3, textvariable=yourvar)
                totalscore1.pack()

Any help is appreciated, thank you.

12
  • 2
    You should print() your wins variable - from the sounds of it, it contains a string that can not be converted to float. Commented Dec 2, 2017 at 15:16
  • I've tried and I'm trying to use the score as a window in tkinter, not pritned normally. Commented Dec 2, 2017 at 15:22
  • Most likely, you're trying to convert a blank string to a float. A simple way to debug this is to print the value before trying to convert it. Commented Dec 2, 2017 at 15:25
  • seems to still be the same Commented Dec 2, 2017 at 15:31
  • We can't help you unless we know what input you're putting into wins_box. As mentioned, likely a blank string, but we cannot know from your question. Commented Dec 2, 2017 at 15:34

2 Answers 2

2

You're trying to convert the string "" into a float. "" doesn't make sense as a float, so you'll have to do one of the following.

1 Populate your fields with strings that make sense as floats. For example "0".

2 Set a default value. The following code sets wins as the float value of wins or 0.0 if not applicable.

try:
    wins = float(wins)
except:
    wins = 0.0

3 Specify "" to convert to 0.0:

if wins == "":
    wins = 0.0
else:
    wins = float(wins)
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, i will look in a moment, i am a bit busy at the minute.
0

I think you use it in wrong place - directly after you create Entry. But Entry() is not input(), it will not wait till you put text, so every command after Entry is executed before you put text in entry. They are executed even before window is opened. Entry() only inform tkinter what element you want use in window. mainloop() will do all job (it opens window, puts widgets in window, gets key/mouse events, sends them to widgets, (re)draws widgets, etc., and back to getting events, etc.).

You can assign function with your code to Button and it will be executed after you click it (after you put code in Entry)

Example:

import tkinter as tk

# --- functions ---

def callback():
    text = e.get()
    try:
        value = float(text)
        l['text'] = 'square: ' + str(value**2)
    except Exception as ex:
        l['text'] = 'wrong float: ' + text

# --- main ---

root = tk.Tk()

e = tk.Entry(root)
e.pack()

b = tk.Button(root, text='CALC SQUARE', command=callback)
b.pack()

l = tk.Label(root, text='here you will see result')
l.pack()

root.mainloop()

2 Comments

is there any way I can cap the number they can put for each one?
do you mean - filtering data allowed in Entry ? You can assing (bind()) function which will be executed after every key and it can check new text in entry and it can delete not allowed chars. See <KeyReleased> in code github.com/furas/python-examples/blob/master/tkinter/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.