构建和更新GUI状态/进度条

时间:2017-08-17 07:17:22

标签: python user-interface tkinter

我找不到这个问题的有效答案。我想构建一个python GUI,它将显示健康,伤害/射击,射击率,杀死时间,射击杀死,几乎所有这个代码中的变量,但我不知道如何构建我需要的东西。它需要是一个简单的窗口,中间有一个彩色条(如下载百分比条),下面有一个按钮,用于激活模拟(使用这种特殊武器实际发生死亡的速度)和上面的标签列表。当条形图清空时(从完全健康到实时零健康状态)它需要保持不变,所以我还需要一个按钮来刷新具有所有相同变量值的模拟。这是我的代码(这正是我需要它做的保存GUI部分):

#import module for update frequency
import threading

#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    
ttk = ((stk - 1) * delay_in_seconds)

#test on how to test for frequency of updating GUI once I figure out how in the heck to build it
def DPS_Timer():
    threading.Timer(float((ttk/stk)), DPS_Timer).start()

#calls my god forsaken function
DPS_Timer()

任何GUI模块都足够了,我正在尝试使用Tkinter,它从来没有为我做过。我还阅读了Python 2.7.13的Tkinter文档,我无法弄清楚我需要做什么。另外,作为一个例子,如果它需要4次射击才能杀死敌方玩家,并且这4次射击发生在一分钟内,这个进度/状态栏需要每(ttk / stk)秒减少(生命值/ stk)%。所以100健康,每15秒25%。

1 个答案:

答案 0 :(得分:0)

这是解决我的GUI问题的非常混乱的测试代码。仍然不是100%我想要的ttk.Progressbar(),但是当它达到100%时它会保存它,然后在停止之前重置它。

#import module for update frequency and gui. ttk for progressbar
import threading,ttk
from Tkinter import *

#establishes 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)
    # delay_in_milliseconds = float(delay_in_seconds*1000)

    #establishes the time to kill in seconds, take one from stk to account for delay of gunfire
    ttki = ((stk - 1) * delay_in_seconds)

    print int((ttki * 1000)) / stk, int(ttki), stk,health, health/stk, range(int(stk))

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