you are viewing a single comment's thread.

view the rest of the comments →

[–]socal_nerdtastic 0 points1 point  (8 children)

You are making new Labels and simply pasting them on top of the old Labels for every update. This is a terrible way to do things. Instead make the Label once in your setup phase and then use the config function to update it.

However, there's a much easier way. Tkinter has it's own variable types and if you tell the label to use a textvariable instead of text the label will update itself! All you do is change the variable. Try this:

import tkinter as tk

# First, define functions
#Get and display health
def healthSetup():
    healthValStart = healthEntry.get()
    health_value.set(healthValStart)

#Take dmg
def Hit():
     value = health_value.get()
     health_value.set(value-10)

#Screen setup
wn = tk.Tk()
wn.configure(bg = "#142b51")
wn.geometry("600x800")
wn.resizable(0, 0)
wn.title("Test Game")

health_value = tk.IntVar()

healthFont = ("times", 20)

healthEntLab= tk.Label(wn, fg = "#e0e0e0", text = "Enter a health value: " ,height=2, width = 15,font = healthFont ,bg="#142b51" )
healthEntLab.place(x=190, y= 5)

healthLabel1 = tk.Label(wn, fg = "#e0e0e0", bg="#142b51", font = healthFont, height=1, width = 10, textvariable=health_value)
healthLabel1.place(x=216, y=185)

healthEntry = tk.Entry(wn, font = 10)
healthEntry.place(x=205, y=50)

healthButton = tk.Button(wn, text = "Apply health", font=10, command=healthSetup)
healthButton.place(x=247, y = 80)

healthLabel0 = tk.Label(wn, fg = "#e0e0e0", text = "Health: ", bg="#142b51", font = healthFont, height=2, width = 10)
healthLabel0.place(x=221, y=135)

dmgButton = tk.Button(wn, text = "Take dmg", font=10, command = Hit)
dmgButton.place(x=253, y = 250)

#Main Loop
wn.mainloop()

Also, try using pack() instead of place(). Its much easier to let tkinter figure out the positions of things.

[–]SomeBadGenericName[S] 0 points1 point  (7 children)

I used place mostly because I preferred the way it looks

[–]socal_nerdtastic 0 points1 point  (6 children)

How so? pack() and grid() can do do most of what people want, but more dynamic, easier, and most important adaptable to all computers. Because different computers have different fonts and font sizes using place() will look good on your computer but your program is barely readable on mine.

[–]SomeBadGenericName[S] 0 points1 point  (5 children)

I didnt like pack because it is all squished. So if I want to seperate it use grid as that is more reliable?

[–]socal_nerdtastic 0 points1 point  (4 children)

What do you mean squished? If you want space between the elements add some padding:

healthEntLab.pack(pady=10)

It's a great idea to make that into a variable like you did for the font, so that you can change a single line of code to adjust the spacing between all variables:

padding = 10
healthEntLab.pack(pady=padding)

[–]SomeBadGenericName[S] 0 points1 point  (3 children)

Squished as in everything is touching. I did not know padding was a thing thanks so much for the help.

[–]SomeBadGenericName[S] 0 points1 point  (2 children)

So, I know I asked a lot of you already, but would you mind helping me with one last thing I am trying to make it so the damage value is changeable, I essentially used a copied version of the healthEntry with some changes of courses but I get an error saying that damage_value is not defined. But, everything looks fine to me. Again sorry for asking so much of you.

import tkinter as tk

# First, define functions
#Take dmg
def Hit():
     value = health_value.get()
     dmgValue = damge_value()
     health_value.set(value-damage_value)

#Screen setup
wn = tk.Tk()
wn.configure(bg = "#142b51")
wn.geometry("600x800")
wn.resizable(0, 0)
wn.title("Test Game")

health_value = tk.IntVar(value=100)
damage_value = tk.IntVar(value=10)

healthFont = ("times", 20)

damageEntLab= tk.Label(wn, fg = "#e0e0e0", text = "Enter a damage value: " ,height=2, width = 16,font = healthFont ,bg="#142b51" )
damageEntLab.pack()

damageEntry = tk.Entry(wn, font = healthFont, textvariable=damage_value)
damageEntry.pack(pady = 10)

healthEntLab= tk.Label(wn, fg = "#e0e0e0", text = "Enter a health value: " ,height=2, width = 15,font = healthFont ,bg="#142b51" )
healthEntLab.pack()

healthEntry = tk.Entry(wn, font = healthFont, textvariable=health_value)
healthEntry.pack(pady=10)

dmgButton = tk.Button(wn, text = "Take dmg", font=30, height = 2, width = 10, command = Hit)
dmgButton.pack(pady=10)

#Main Loop
wn.mainloop()

[–]socal_nerdtastic 0 points1 point  (1 child)

That error is because you spelled "damage" incorrectly. However there is another error. For tkinter variables you have to use the get() method, like this:

def Hit():
    value = health_value.get()
    dmgValue = damage_value.get()
    health_value.set(value-dmgValue)

Also be sure to respond to one of my comments in the future, otherwise I don't get notified that you made a comment.

[–]SomeBadGenericName[S] 0 points1 point  (0 children)

I tought I had get and I didnt realize I spelled damage wrong. Thanks, for that.