This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

How can I add two numbers with LineEdit and show the answer in label?

rodmeloufparodmeloufpa Posts: 2Member

I am trying to make an app to add two numbers. I used LineEdits to put numbers and a label to show the answer. Unfortunately, something is wrong. Could anyone here help me?


Tags :

Best Answers

  • MegalomaniakMegalomaniak Posts: 2,580
    Accepted Answer

    So heres my example:

    extends HBoxContainer # used HBoxContainer as parent for the other nodes
    
    var num1 = 0
    var num2 = 0
    var result = 0
    onready var res = get_node("OutputLabel")
    
    # note that I've opted to use the _text_changed signal
    func _on_InputLine1_text_changed(new_text):
        num1 = int(new_text)
    
    
    func _on_InputLine2_text_changed(new_text):
        num2 = int(new_text)
    
    
    func _on_CalcButton_pressed():
        result = num1 + num2
        print(res.text)
        print(result)
        res.set_text(String("Result: ") + String(result))
    
  • rodmeloufparodmeloufpa Posts: 2
    Accepted Answer

    Thanks a lot Megalomaniak. It's working!!!

Answers

  • MegalomaniakMegalomaniak Posts: 2,580Admin
    edited July 2019

    Your _ready() is empty and none of your custom functions are triggered by anything based on what I can see. Also that extra indent on the highlighted line 16 could be removed.

    edit: I suppose your signals might be triggering the functions if set up properly, but I can't be sure of that I mean.

    Edit 2: Ok, so instead of using Str() you should use String() on line 19, and since you used the text entered signal from the LineEdits, after inputting/changing the text you need to hit enter too.

  • MegalomaniakMegalomaniak Posts: 2,580Admin
    Accepted Answer

    So heres my example:

    extends HBoxContainer # used HBoxContainer as parent for the other nodes
    
    var num1 = 0
    var num2 = 0
    var result = 0
    onready var res = get_node("OutputLabel")
    
    # note that I've opted to use the _text_changed signal
    func _on_InputLine1_text_changed(new_text):
        num1 = int(new_text)
    
    
    func _on_InputLine2_text_changed(new_text):
        num2 = int(new_text)
    
    
    func _on_CalcButton_pressed():
        result = num1 + num2
        print(res.text)
        print(result)
        res.set_text(String("Result: ") + String(result))
    
  • rodmeloufparodmeloufpa Posts: 2Member
    Accepted Answer

    Thanks a lot Megalomaniak. It's working!!!

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.