1

I'm attempting to make a GUI for my text game (similar to a pokemom game)

In summary the code works like:

import random
hp = 100

for i in range (0,4):
        
    element =  input("Enter attack: ")
        
    list.append(element)
            
attack1 = list[0]
attack2 = list[1]
attack3 = list[2]
attack4 = list[3]

while(True):

    cmd = input(">")

    If cmd.lower() == attack1 ... :
        if cmd.lower() == bite:
             hp -= 10

Now I tried doing a gui on Tkinter through buttons but it seems I can't get a grasp of it

from tkinter import *

#4 buttons 
bite = 0
root = Tk()

def click(hp):  
    hp -= 10 
    myLabel = Label(text=f"{hp} hp remaining left.")
    myLabel.grid(row = 5, column = 5)
    
        
def click2():
    bite = 0
    hp = 50
    hp -= 20
    myLabel = Label(text=f"{hp} hp remaining left.")
    myLabel.grid(row = 5, column = 5)
    bite += 1
            
            
myButton = Button(root, text=f"hello {bite}/5", command = click(hp))
    
myButton2 = Button(root, text=f"Presssss {bite}/5", command = click2)
    
myButton3 = Button(root, text="Presssss")
myButton4 = Button(root, text="Presssss")
            
myButton.grid(row = 0, column = 1)
myButton2.grid(row = 1, column = 1)
myButton3.grid(row = 1, column = 2)
myButton4.grid(row = 0, column = 2)
root.mainloop()

And this just presents a constant value "90, 30" due to the variables being included on the function, but whenever I'm trying to put it into an argument like

hp = 100
def click(hp):
    hp -= 10


button1 = Button(root, text = "try", command = click(100))

It just returns the value 90, 30 way before the button is clicked. When hp is used as an arg, it is saying undefined.

Winmari Manzano
  • 456
  • 4
  • 8
  • You need to use [lambda](https://stackoverflow.com/questions/16501/what-is-a-lambda-function/62742314#62742314) otherwise your function is executed as you define it. Take a look at the link and let me know if questions left. – Thingamabobs May 13 '21 at 11:14
  • just so You know, that `hp` inside `click()` won't do anything because it will subtract and store the value in local scope, to counter this You could use `global hp` at the start of that function definition. that will then actually change the `hp`. A bit of correction: it will do something it will put 90 on label because 100 - 10 is 90, however calling it again will put 90 on the label again. The same can be said about the rest of the functions – Matiiss May 13 '21 at 12:21
  • Hello, thanks for the replies. I was also wondering how the label copes up with the -= operator, do we have a way of updating the values that the label shows everytime the button is clicked. – Winmari Manzano May 13 '21 at 12:42
  • @MonkeyD.Luffy you could use a `tk.StringVar` and the optinal parameter `textvariable` or you could just confiugre the `tk.Label` with the optional parameter *text*. – Thingamabobs May 13 '21 at 13:11

1 Answers1

0

It does so because click() function is called as soon as window is created.

To prevent it you can use lambda :

myButton = Button(root, text=f"hello {bite}/5", command = lambda: click(hp))
Utpal Kumar
  • 300
  • 2
  • 13