Curieux comment remplir un ttk.Progressbar() à la valeur maximale (n'importe quel nombre spécifié par l'utilisateur) et réduire la barre avec l'étape dans les incréments spécifiés. Prenez donc une barre remplie à 100% (par exemple, l'utilisateur spécifie 100 comme valeur maximale) et réduisez-la de 25% toutes les 15 secondes. voici ce que j'ai:Inverser ttk.Progressbar()
#import module for update frequency and gui. ttk for progressbar
import threading,ttk
from Tkinter import *
#establishs counter for testing purposes
counter = 0
#establish window
root = Tk()
def main():
#user inputs health
health = float(input("Enter health:"))
#user inputs how much damage each bullet does
dps = float(input("Enter Damage per shot:"))
#user inputs the fire rate of the weapon
spm = float(input("Enter Fire Rate:"))
#from user inputs establish how many shots it takes to reduce health to or below zero
if ((health/dps).is_integer()) is False: #checks if the stk value will be a float
stk = int(health/dps) + 1 #since stk value is a float go up to next whole number. 33dps doesn't kill in 3 shots.
else: #if stk value is an integer, establishes stk variable
stk = health/dps
delay_in_seconds = float(60/spm)
#establishes the time to kill in seconds, take one from stk to account for delay of gunfire
ttki = ((stk - 1) * delay_in_seconds)
# establish progressbar
progre = ttk.Progressbar(root, orient='horizontal', maximum=health, mode='determinate')
progre.pack(fill=X)
# test on how to test for frequency of updating GUI once I figure out how in the heck to build it
def DPS_Timer():
global counter
print counter
if counter != (stk-1):
counter += 1
progre.step(float((health/stk)))
root.after(int(ttki*1000/stk), DPS_Timer)
else:
progre.stop()
# establish GUI Button
B1 = Button(root, text='start', command=DPS_Timer).pack(fill=X)
root.mainloop()
main()
donc en gros je veux que cette portion de code
# establish progressbar
progre = ttk.Progressbar(root, orient='horizontal', maximum=health, mode='determinate')
progre.pack(fill=X)
# test on how to test for frequency of updating GUI once I figure out how in the heck to build it
def DPS_Timer():
global counter
print counter
if counter != (stk-1):
counter += 1
progre.step(float((health/stk)))
root.after(int(ttki*1000/stk), DPS_Timer)
else:
progre.stop()
# establish GUI Button
B1 = Button(root, text='start', command=DPS_Timer).pack(fill=X)
dire « Hey ttk.Progressbar(), dépeignent la barre à 100% / la valeur maximale de la santé et je dois vous le réduire à cette valeur de pas à ce taux « . Cela pourrait également fournir un aperçu qu'elle ne me fournit: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Progressbar.html et https://docs.python.org/2/library/ttk.html#index-0
Le code original a été remanié pour être interface graphique, donc voici mon nouveau code qui a un problème de la ttk.Progressbar() étant initialisé avec un morceau déjà manquant avant la sélection du bouton de démarrage. Juste pour spécifier, quand le bouton de démarrage est sélectionné, il doit être maintenu pendant la période de délai avant même que le premier morceau soit enlevé.
#import module for update frequency and gui. ttk for progressbar
import ttk
from Tkinter import *
#establish window
root = Tk()
E1 = Entry(root)
E2 = Entry(root)
E3 = Entry(root)
global health, dps, spm, ttki, stk, delay_in_seconds, progre, counter
def captcha_health():
global health, dps, spm, ttki, stk, delay_in_seconds, progre, counter
try:
health = float(E1.get())
except ValueError:
exit()
Entry2()
def captcha_dps():
global health, dps, spm, ttki, stk, delay_in_seconds, progre, counter
try:
dps = float(E2.get())
except ValueError:
exit()
Entry3()
def captcha_spm():
global health, dps, spm, ttki, stk, delay_in_seconds, progre, counter
try:
spm = float(E3.get())
except ValueError:
exit()
estvar()
def Entry2():
global health, dps, spm, ttki, stk, delay_in_seconds, progre, counter
# user inputs how much damage each bullet does
E2.grid(sticky='we')
DB = Button(root, text='enter damage/shot', command=captcha_dps).grid()
def Entry3():
global health, dps, spm, ttki, stk, delay_in_seconds, progre, counter
# user inputs the fire rate of the weapon
E3.grid(sticky='we')
SB = Button(root, text='enter fire rate', command=captcha_spm).grid()
def estvar():
global health, dps, spm, ttki, stk, delay_in_seconds, progre, counter
# establishs counter for testing purposes
counter = 0
# from user inputs establish how many shots it takes to reduce health to or below zero
if ((health/dps).is_integer()) is False: # checks if the stk value will be a float
stk = int(
health/dps) + 1 # since stk value is a float go up to next whole number. 33dps doesn't kill in 3 shots.
else: # if stk value is an integer, establishes stk variable
stk = health/dps
delay_in_seconds = float(60/spm)
# establishes the time to kill in seconds, take one from stk to account for delay of gunfire
ttki = ((stk - 1) * delay_in_seconds)
guiest()
def DPS_Timer():
global health, dps, spm, ttki, stk, delay_in_seconds, progre, counter
counter += 1
progre.step(-1*dps)
if counter < stk:
root.after(int(ttki*1000/stk), DPS_Timer)
def guiest():
global health, dps, spm, ttki, stk, delay_in_seconds, progre, counter
# establish GUI Button
ttkLabel = Label(root, text='Time to kill: ' + str(ttki)).grid(sticky=W)
stkLabel = Label(root, text='Shots to kill: ' + str(stk)).grid(sticky=W)
hhLabel = Label(root, text='Health: ' + str(health)).grid(sticky=W)
dpsLabel = Label(root, text='Damage/shot: ' + str(dps)).grid(sticky=W)
spmLabel = Label(root, text='Fire rate: ' + str(spm)).grid(sticky=W)
delayLabel = Label(root, text='Delay between shots: ' + str(delay_in_seconds)).grid(sticky=W)
B1 = Button(root, text='start', command=DPS_Timer).grid(sticky='we')
# establish progressbar
progre = ttk.Progressbar(root, orient='horizontal', maximum=health, mode='determinate')
progre.grid(sticky='we')
progre.step(health-1)
#user inputs health
E1.grid(sticky='we')
HB = Button(root, text='enter health value', command=captcha_health).grid()
root.mainloop()
Alors, quelle est votre question? Comment votre code n'atteint-il pas ce que vous voulez? – SiHa