0

I am trying to make a simple calculator for a game I play as a random project. I found myself very confused on how to go about doing this; I want the user to enter a number in the textbox and it will multiply that number by 64000 and display it in a label above. But, I cannot find out anywhere how to do this. This is just a small practice thing and im amazed I cannot figure it out. Please help meh! Here is my code:

from Tkinter import *

####The GUI#####
root = Tk()

#Title#
root.wm_title("TMF Coin Calculator")

####The Widgets####
a = StringVar()
b = StringVar()

def multiply_a():
    d = (int(a) * 64000)
    e = str(d)
    b.set(e)

b.trace("w", multiply_a)

#Top Label#

top_label = Label(root,  fg="red", text="Type the ammount of Coin Stacks you have.")
top_label.grid(row=0, column=0)

#The Output Label#
output_label = Label(root, textvariable=b, width = 20)
output_label.grid(row = 1, column=0)

#User Entry Box#
user_input_box = Entry(root, textvariable=a, width=30)
user_input_box.grid(row=2, column=0)

root.mainloop()

1 Answer 1

3

You need to trace the variable that changes: a. Then you need to define multiply_a() to handle an arbitrary number of arguments with *args (happens to be 3 arguments in this case, but *args is fine here).

Go from:

def multiply_a():
    d = (int(a) * 64000)
    e = str(d)
    b.set(e)

b.trace("w", multiply_a)

to:

def multiply_a(*args):
    try:
        myvar = int(a.get())
    except ValueError:
        myvar = 0
    b.set(myvar * 64000)

a.trace("w", multiply_a)

This will additionally use 0 if the entered value can't be turned into an int.

Sign up to request clarification or add additional context in comments.

Comments

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.