vendredi 12 juin 2020

One of my Tkinter checkbuttons is not updating its value within a nested function when I call it

from tkinter import *

root = Tk()
root.title("SW Damage Calculator")

frame1 = LabelFrame(root, text="Single Stat Scaling", padx=10, pady=10)
frame1.grid(row=1, column=0)

def scaling1():
    def totalstat():
        b_stat = int(b_stat_entry.get())
        r_stat = int(r_stat_entry.get())
        cd_stat = int(cd_stat_entry.get())
        atower1_stat = int(atower1_stat_entry.get())
        atower2_stat = int(atower2_stat_entry.get())
        atowercd_stat = int(atowercd_stat_entry.get())
        leadskill_stat = int(leadskill_stat_entry.get())
        en_def_stat = int(en_def_stat_entry.get())
        skill_multiplier = int(multiplier_entry.get())
        skill_ups = int(skill_ups_entry.get())
        def_break_toggle = int(def_break_val.get())
        atk_buff_toggle = int(atk_buff_val.get())

        tot_dmg.set((
                            ((b_stat + r_stat +
                              ((atower1_stat / 100) * b_stat) +
                              ((atower2_stat / 100) * b_stat) +
                              ((leadskill_stat / 100) * b_stat)) * atk_buff_toggle) *
                            (1 + ((atowercd_stat + cd_stat) / 100))) *
                    ((skill_multiplier + skill_ups) / 100) *
                    (1000 / (1140 + 3.5 * (en_def_stat * def_break_toggle))))

    basestat = Label(frame1, text="Base Attack")
    b_stat_entry = Entry(frame1)

    runestat = Label(frame1, text="Bonus Attack from Runes")
    r_stat_entry = Entry(frame1)

    cdstat = Label(frame1, text="Critical Damage %")
    cd_stat_entry = Entry(frame1)

    leadskill = Label(frame1, text="Leader Skill %")
    leadskill_stat_entry = Entry(frame1)

    endefstat = Label(frame1, text="Enemy Defence")
    en_def_stat_entry = Entry(frame1)

    multiplier = Label(frame1, text="Skill Multiplier")
    multiplier_entry = Entry(frame1)

    skillups = Label(frame1, text="Damage from Skill Ups")
    skill_ups_entry = Entry(frame1)

    atower1 = Label(frame1, text="Arena Tower Stat 1 %")
    atower1_stat_entry = StringVar(frame1)
    atower1_stat_entry.set('0')
    atower1lvl = OptionMenu(frame1, atower1_stat_entry, '0', '3', '5', '7', '9', '11', '13', '15', '17', '19', '21')

    atower2 = Label(frame1, text="Arena Tower Stat 2 %")
    atower2_stat_entry = StringVar(frame1)
    atower2_stat_entry.set('0')
    atower2lvl = OptionMenu(frame1, atower2_stat_entry, '0', '2', '4', '6', '8', '10', '12', '14', '16', '18', '20')

    atowercd = Label(frame1, text="Arena CD Tower %")
    atowercd_stat_entry = StringVar(frame1)
    atowercd_stat_entry.set('0')
    atowercdlvl = OptionMenu(frame1, atowercd_stat_entry, '0', '2', '5', '7', '10', '12', '15', '17', '20', '22', '25')

    defbreak = Label(frame1, text="Defence Break")
    def_break_val = IntVar(frame1)
    def_break = Checkbutton(frame1, offvalue=1, onvalue=0.3, variable=def_break_val, textvariable=def_break_val)
    def_break_val.set(1)

    atkbuff = Label(frame1, text="Attack Buff")
    atk_buff_val = IntVar(frame1)
    atk_buff = Checkbutton(frame1, offvalue=1, onvalue=1.5, variable=atk_buff_val, textvariable=atk_buff_val)
    atk_buff_val.set(1)

    tot_dmg = IntVar()
    tot_damage = Label(frame1, textvariable=tot_dmg)

    add_stats = Button(frame1, text="Total Stat", command=totalstat)

    basestat.grid(row=1, column=0)
    b_stat_entry.grid(row=1, column=1)

    runestat.grid(row=2, column=0)
    r_stat_entry.grid(row=2, column=1)

    cdstat.grid(row=3, column=0)
    cd_stat_entry.grid(row=3, column=1)

    tot_damage.grid(row=8, column=1)
    add_stats.grid(row=9, column=1)

    atower1lvl.grid(row=1, column=3)
    atower1.grid(row=1, column=2)

    atower2lvl.grid(row=2, column=3)
    atower2.grid(row=2, column=2)

    atowercdlvl.grid(row=3, column=3)
    atowercd.grid(row=3, column=2)

    endefstat.grid(row=4, column=0)
    en_def_stat_entry.grid(row=4, column=1)

    leadskill.grid(row=7, column=0)
    leadskill_stat_entry.grid(row=7, column=1)

    multiplier.grid(row=5, column=0)
    multiplier_entry.grid(row=5, column=1)

    skillups.grid(row=6, column=0)
    skill_ups_entry.grid(row=6, column=1)

    defbreak.grid(row=5, column=2)
    def_break.grid(row=5, column=3)

    atkbuff.grid(row=4, column=2)
    atk_buff.grid(row=4, column=3)

ScalingButton1 = Button(frame1, text="Single Stat Scaling", command=scaling1)
ScalingButton1.grid(row=0, column=0)

root.mainloop()

I'm creating a damage calculator for a game I play. All of the code works except that the checkbutton variable "atk_buff_val" does not update within the "totalstat" function. The off value is initialized but it never changes to 1.5 in "totalstat" when I check it. However my other checkbutton "def_break_val" works perfectly fine even though the code is pretty much the same for the both of them so I'm confused as to what I'm doing wrong.




Aucun commentaire:

Enregistrer un commentaire