Forum Archive

Button won’t show up, and can’t get result to show up on result Label. Please help

Ahab3030

I am trying to make a simple GUI in UI that will give me the hypotenuse from 2 numbers A and B. the button was displayed, but went away(not sure why). I can write numbers in the upper 2 text field’s and I’d like the button to perform a hypotenuse calc on the numbers and display it in 3rd field, which for some reason I believe needs to be a label and not TextField. Any help is appreciated. Thanks!

————————————————————————————————————————————————————————

import ui
import math
import string


view = ui.View()
view.name = 'hypotenuse'
view.background_color = 'white'

# variables for lengths A and B

A = float()
B = float()

# label for length

LenLbl = ui.Label()
LenLbl.text = 'Length'
LenLbl.x = 370
LenLbl.y = 80

# text feild input for length (A)

Length = ui.TextField(A)
Length.x = 300
Length.y = 150
Length.width = 200
Length.border_width = 1

# label for width

widLbl = ui.Label()
widLbl.text = 'Width'
widLbl.x = 370
widLbl.y = 280

# text feild input for width (B)

Width = ui.TextField(B)
Width.x = 300
Width.y = 350
Width.width = 200
Width.border_width = 1

# label for result

resLbl = ui.Label()
resLbl.text = 'Result'
resLbl.x = 370
resLbl.y = 480

# label that should show result of hypotenuse of A and B when button is pushed

Result = ui.Label()
Result.text = str()
Result.x = 300
Result.y = 550
Result.width = 200
Result.border_width = 1

# math for hypot calc

C = math.hypot(A, B)

# attempt at getting button to perform calc and put result in result Label

def button_tapped(C):
    Result.text = float(eval(C))

button = ui.Button()
button.text = ' Calculate '
button.x = 300
button.y = 650
button.action = button_tapped
button.border_width = 1

# putting items on screen in GUI(only button wont show up)

view.add_subview(Width)
view.add_subview(widLbl)
view.add_subview(Length)
view.add_subview(LenLbl)
view.add_subview(button)
view.add_subview(resLbl)
view.add_subview(Result)
view.present('fullscreen')
JonB

When you post a message in the forum, you will see a little icon that looks like this

Use that button before pasting your code, so the formatting will be correct.

JonB

There are a number of issues, mostly related to the fact that you are not instantiating your views correctly.

Length = ui.TextField(A)

If you want the text to be A, you must set the text attribute. When you instantiate it, your arguments should be the frame, and maybe bg_color.

JonB

@Ahab3030

def button_tapped(C):
    Result.text = float(eval(C))
#should be
def button_tapped(sender):
   A=float(Length.text)
   B=float(Width.text)
   Result.text = str(math.hypot( A, B ))

mikael

@Ahab3030, button needs to have tint color set to be visible, recommend setting the background color as well.

JonB

@Ahab3030
Buttons have title, not text. Set the text in the constructor so it autosizes.

button = ui.Button(frame=[300 650 100 40], title='Calculate')
#button.title = ' Calculate '
#button.x = 300
#button.y = 650
Ahab3030

Thanks y’all I will give that a try!

Ahab3030

That worked! Thanks everyone I can’t believe how fast you all provided help.