TheBudderBomb TheBudderBomb -4 years ago 140
Python Question

Bouncy Ball Distance GUI

I am currently trying to finish a coding assignment where I have to build a gui that will take three entries: height, bounciness index, and number of bounces, to calculate how far the bouncy ball has traveled. I feel like my problem is simple to solve but I cant for the life of me figure it out. My problem is that I need to have the final calculation show up in another box underneath the button, and I cant figure out how. Here's the code I have so far:

from tkinter import *

master = Tk()
master.title("Bouncy")

def BouncyCalc():
z = float(b_index.get()) ** (float(num_bounce.get()) + 1)
return float(height_e.get()) * (1 + (2 * (z / (float(b_index.get()) - 1))))

Label(master, text = "Initial Height").grid(row = 0)
Label(master, text = "Bounciness Index").grid(row = 1)
Label(master, text = "Number of Bounces").grid(row = 2)

height_e = Entry(master)
b_index = Entry(master)
num_bounce = Entry(master)

height_e.insert(10,"0.0")
b_index.insert(10,"0.0")
num_bounce.insert(10, "0")

height_e.grid(row = 0, column = 1)
b_index.grid(row = 1, column = 1)
num_bounce.grid(row = 2, column = 1)

calc_button = Button(master, text = "Calculate", command = BouncyCalc())
calc_button.grid(row = 3, column = 1)

mainloop()


Could somebody provide me some guidance as to what I should do next? I'm fairly new to python and GUIs are good at confusing me

Answer Source

For starters, you don't want to call the function, just reference it.

calc_button = Button(master, text = "Calculate", command=BouncyCalc)

See, I removed () off of BouncyCalc.

Secondly, if you want to set the value of that method into some textbox, then you should do that instead of return-ing the value and also add a Label for that value first.

Please refer: Setting Text to Entry from event

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download