Python Forum
Updating button text based upon different variable values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Updating button text based upon different variable values (/thread-33347.html)



Updating button text based upon different variable values - knoxvilles_joker - Apr-18-2021

I am working through among one of the final issues on my script. I have the temp and rate bars updating like I want. I have the ammo and msec countdown timers counting downward with keypress f is initiated.

What I am wanting is to have the crit bar update based on ammo and temp levels.
initially nothing is displayed. at 50 rounds remaining it flashes critical and then at 0 it stops for a few seconds and says out. And when the tempbar reaches 90% it flashes overheat briefly.

I can get the overheat to work more or less the way I want. I think my issue is that due to the if statements with ammo levels not being exclusive ranges, the conflicts are preventing proper updates.

The script works with no real errors.

        # crit menu bar
        crit = tk.Button(self,justify=tk.CENTER,textvariable=self.stat, bg='black', fg='yellow', highlightbackground='yellow', highlightcolor='black',activebackground='yellow')
        crit.grid(row=5,column=0,columnspan=2,sticky="NSEW")
import tkinter as tk
from tkinter import messagebox
import datetime as dt
import tkinter.ttk as ttk
import time

# sets ammo count to initial value of 500 
INITIAL_COUNTER_VALUE = 500
# sets initial time at 100%
INITIAL_TIMER_VALUE = 33000 
# sets initial gun terminal ID
#sets gun rate
RMMAX = 0
# sets initial temparature
TEMP_INITIAL = 20
# sets text string for ammo/temp/low ammo status button
STATUS = ""

COOLED = 20
 
class TkApp(tk.Tk): 
#class MainFrame(tk.Frame):
 
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # set screen options
        self.geometry("640x400")
        self.configure(bg='black')
        self.title("Sentry Terminal")
        # locally initialize ammo count down
        self.counter = tk.IntVar()
        self.counter.set(INITIAL_COUNTER_VALUE)
        # locally sets the timer count down at 100%
        self.timer = tk.IntVar()
        self.timer.set(INITIAL_TIMER_VALUE)
        # locally sets rmbar to 0%
        self.rmcount = tk.IntVar()
        self.rmcount.set(RMMAX)
        # locally sets tempbar
        self.temperature = tk.IntVar()
        self.temperature.set(TEMP_INITIAL) 
        # locally sets text string for ammo status button
        self.stat = tk.StringVar()
        self.stat.set(STATUS)
#
        self.cooldown = tk.IntVar()
        self.stat.set(COOLED)


        tk.Grid.rowconfigure(self, 0, weight=1, uniform='c')
        tk.Grid.rowconfigure(self, 1, weight=1, uniform='b')
        tk.Grid.rowconfigure(self, 2, weight=1, uniform='b')
        tk.Grid.rowconfigure(self, 3, weight=1, uniform='b')
        tk.Grid.rowconfigure(self, 4, weight=1, uniform='b')
        tk.Grid.rowconfigure(self, 5, weight=1, uniform='b')
        tk.Grid.rowconfigure(self, 6, weight=1, uniform='b')
        tk.Grid.rowconfigure(self, 7, weight=1, uniform='b')
        tk.Grid.rowconfigure(self, 8, weight=1, uniform='b')

        tk.Grid.columnconfigure(self, 0, weight=1, uniform='a')
        tk.Grid.columnconfigure(self, 1, weight=1, uniform='a')
        tk.Grid.columnconfigure(self, 2, weight=1, uniform='a')
        tk.Grid.columnconfigure(self, 3, weight=1, uniform='a')
        tk.Grid.columnconfigure(self, 4, weight=1, uniform='a')
        tk.Grid.columnconfigure(self, 5, weight=1, uniform='a')


 
# key input bindings
        self.bind("<KeyPress-f>", self.on_keypress_f)
        self.bind("<KeyRelease-f>", self.on_keyrelease_f)
        # these are the button and label declarations
        rounds = tk.Label(self, justify=tk.CENTER, textvariable=self.counter)
        rounds.grid(row=3, column=2, columnspan=1, sticky="EW")
        label1 = tk.Label(self, textvariable=self.timer)
        label1.grid(row=7, column=2, sticky="EW")
        # declarative buttons
        timestat = tk.Button(self,bg='black',state=tk.DISABLED, fg='yellow',disabledforeground='yellow',highlightbackground='yellow',borderwidth=2,justify=tk.CENTER,text="TIME AT 100% \n (msecs)")
        timestat.grid(row=7,column=0,columnspan=2,sticky="NSEW")
        roundsr = tk.Button(self,bg='black',state=tk.DISABLED, fg='yellow',disabledforeground='yellow',highlightbackground='yellow',borderwidth=2,justify=tk.CENTER,text="Rounds \n Remaining")
        roundsr.grid(row=3,column=0,columnspan=2,sticky="NS")




        temp = tk.Button(self,justify=tk.CENTER,text="Temp",bg='black', fg='yellow', highlightbackground='yellow', highlightcolor='black',activebackground='yellow')
        temp.grid(row=2,column=4,sticky="NSEW")

        rm = tk.Button(self,justify=tk.CENTER,text="R(M)",bg='black', fg='yellow', highlightbackground='yellow', highlightcolor='black',activebackground='yellow')
        rm.grid(row=2,column=5,sticky="NSEW")
        # rounds rate bar
        rmbar = ttk.Progressbar(self, orient="vertical", variable=self.rmcount)
        rmbar.grid(row=3,column=5,rowspan=6, sticky="NS")

        # temperature bar
        tempbar = ttk.Progressbar(self, orient="vertical", variable=self.temperature)
        tempbar.grid(row=3,column=4,rowspan=6,sticky="NS")

        # crit menu bar
        crit = tk.Label(self,justify=tk.CENTER, textvariable=self.stat, bg='black', fg='yellow', highlightbackground='yellow', highlightcolor='black',activebackground='yellow')
        crit.grid(row=5,column=0,columnspan=2,sticky="NSEW")

# these are ported
    def coolbar(self):
        temperature_count = self.temperature.get()
        if temperature_count >= 21:
          temperature_count = self.temperature.get()
          temperature_count = temperature_count-1 or TEMP_INITIAL      
          self.temperature.set(temperature_count)
          print("key not pressed", temperature_count)
          self.after(300, self.coolbar)

    def overheat(self):
        rmcount_count = self.rmcount.get()
        rmcount_count = 0
        self.rmcount.set(rmcount_count)
        state_stat = self.stat.get()
        state_stat = "OVERHEAT"
        self.stat.set(state_stat)
        time.sleep(0.7)
        
    def critical(self):
        counter_value = self.counter.get()
        if counter_value <= 50:
          counter_value = self.counter.get()
          state_stat = self.stat.get()
          state_stat = "CRITICAL"
          self.stat.set(state_stat)
          counter_value = self.counter.get()
          print("ammo CRIT", counter_value) 
          

    def out(self):
        counter_value = self.counter.get()
        if counter_value == 1:
          state_stat = self.stat.get()
          state_stat = "OUT"
          self.stat.set(state_stat)
          counter_value = self.counter.get()
          print("ammo CRIT", counter_value) 
          

    def reload(self):
        counter_value = self.counter.get()
        if counter_value >= 1:
          state_stat = self.stat.get()
          state_stat = ""
          self.stat.set(state_stat)
          counter_value = self.counter.get()
          print("ammo CRIT", counter_value)
          

    def on_keypress_f(self, evet):        
        counter_value = self.counter.get()
        counter_value = counter_value-1 or INITIAL_COUNTER_VALUE
        self.counter.set(counter_value)
        counter_value = self.counter.get()
        # this is logic to try and get ammo warnings
        counter_value = self.counter.get()

        self.reload()
        self.critical()
        self.out()

 
        # this sets the time at 100%
        timer_value = self.timer.get()
        timer_value = timer_value-66 or INITIAL_TIMER_VALUE
        self.timer.set(timer_value)
        # this sets the rmbar to 40%
        rmcount_count = self.rmcount.get()
        rmcount_count = 40
        self.rmcount.set(rmcount_count)
        # this sets the temperature
        temperature_count =  self.temperature.get()
        temperature_count = temperature_count+1
        self.temperature.set(temperature_count)
        if temperature_count == 90:
          self.coolbar()
          self.overheat()
        else:
          state_stat = self.stat.get()
          state_stat = ""
          self.stat.set(state_stat)
          
    def on_keyrelease_f(self, evet):
        #this adds logic to say rate is zero if not firing
        #this automatically turns off rate bar moment key is released fixing a logic issue
        rmcount_count = self.rmcount.get()
        rmcount_count = 0
        self.rmcount.set(rmcount_count)
        temperature_count =  self.temperature.get()
        if temperature_count >= 20:
          self.coolbar()


tk_app = TkApp()
tk_app.mainloop()
I think I am missing something blatently obvious in the formatting and function definitions. I think it has to do with the fact that if ammo count is lessthan or equal to 50 it flashes critical and also at ammo count == 1 it flashes out and the dual rules not playing well together.

thoughts?
Linked here:
http://forum.alienslegacy.com/viewtopic.php?f=3&t=3019

Is the the location of a flash piece I am basically trying to replace. I ran with tkinter as it seemed to have all the options easily and quickly accessible that pygame did not have. Or my searches and understanding was soo limited that I did not find the specific library modules to use with python with pygame that would do the functional equivalent of what I have now.
Any thoughts anyone?