2017-08-17 48 views
-1

我無法找到針對此問題的正常運行答案。我想構建一個python GUI,它將顯示健康狀況,傷害/射擊,射擊速度,殺死時間,殺死鏡頭,幾乎所有代碼中的變量,但我不知道如何構建我需要的東西。它需要是一個帶有彩色條的簡單窗口(如下載百分比欄),中間有一個按鈕,用於激活模擬(使用此特定武器實際發生的死亡速度)以及上面的標籤列表。當條形圖(實時從完全健康狀態到零健康狀態)清空時,它需要保留,所以我也需要一個按鈕來刷新具有所有相同變量值的模擬。這裏是我的代碼(即不正是我需要做的保存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)秒減少(health/stk)%。所以在100點的時候,每15秒25%。

+0

你的問題有點複雜,不能在這裏徹底解答。我個人使用PyQt5。有一個設計器(http://pyqt.sourceforge.net/Docs/PyQt5/designer.html),您可以在其中使用鼠標放置所有加載條和按鈕。然後你需要將每個鏈接到你的函數。如果您從未編寫過GUI,請爲學習曲線做好準備。如果您對「盲目繪圖」確定可能不會使用設計師。 – Wli

回答

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