【问题标题】:Reverse ttk.Progressbar()反向 ttk.Progressbar()
【发布时间】:2018-01-29 03:14:22
【问题描述】:

很好奇如何将 ttk.Progressbar() 填充到最大值(用户指定的任何数字),并以指定的增量逐步减少进度条。因此,取一个 100% 填充的条(例如,用户指定 100 作为最大值)并每 15 秒减少 25%。这是我所拥有的:

#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()

所以基本上我想要这部分代码

# 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)

说“嘿 ttk.Progressbar(),将条形图描绘为 100%/ 生命值的最大值,我需要你将其降低到 步值以速率。”这也可能提供一些它没有提供给我的见解:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Progressbar.htmlhttps://docs.python.org/2/library/ttk.html#index-0

原始代码已被修改为基于 GUI,所以这是我的新代码,它有一个问题,即 ttk.Progressbar() 在选择开始按钮之前已经丢失了一个块。只是指定,选择开始按钮时,它需要在延迟时段删除延迟时段,甚至删除第一个块。

#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()

【问题讨论】:

  • 那么你的问题是什么?你的代码怎么没有达到你想要的效果?

标签: python python-2.7 user-interface tkinter ttk


【解决方案1】:

为了将生命值设置为 100%,我们可以使用

progre.step(health - 1)

就在我们 DPS_Timer() 函数的正上方。从那里开始,除了步长值之外,您的函数大部分都是正确的。我们希望步长为负,因为我们正在降低生命值条的值。此外,除非我们在某个时候使用 .start() ,否则 .stop() 是无用的,因此我们可以完全删除该代码块。一旦 counter 等于 stk,root.after() 无论如何都不会被调用,所以我们不需要 else 语句。这是给我们的:

def DPS_Timer():
    global counter
    print(counter)

    if counter < stk:
        counter += 1
        progre.step(-1*health/stk)
        root.after(int(ttki*1000/stk), DPS_Timer)

另外,你的 progre.step() 实际上并没有消除 dps 给予的伤害量。我们可以用

解决这个问题
progre.step(health - 1)

def DPS_Timer():
    global counter
    print(counter)

    counter += 1
    progre.step(-1*dps)
    if counter < stk:
        root.after(int(ttki*100/stk), DPS_Timer)

(我移动了 if 语句,因此 DPS_Timer 不会被称为额外时间)

【讨论】:

  • 您的代码非常有用,除了一个小细节。我已经将我的代码修改为基于 GUI,并将所有内容都设置为一个函数,以便让所有内容看起来更清晰。您建议的此代码的唯一问题是,当初始化 ttk.Progressbar() 时,在按下开始按钮之前它已经丢失了一个块,我们将如何解决这个问题?我已经编辑了原始帖子,因此您可以查看改版
  • 没关系,我意识到我只是忘记删除 DPS_Timer 函数调用。感谢您的帮助,它完全符合我的需要,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 2013-09-13
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 2013-05-04
相关资源
最近更新 更多