簡體   English   中英

Python - 在給定時間后退出程序的倒數計時器

[英]Python - Countdown timer that exits program after given time

我對 Python 還很陌生,我正在和我的大學同事一起做簡單的拼圖項目。 我們要更新程序並向其添加計時器和重置按鈕。

計時器的想法是倒計時 X 分鍾,然后顯示一些帶有退出/退出按鈕的消息,或者只是退出主應用程序。 重置按鈕可以放置在應用程序窗口的某個位置並重置游戲。

程序可以在這里找到: http : //pastebin.com/79zRqhnH

謝謝你的幫助。

一種。

首先只是一些建議,但請記住,我也不是代碼專家。

我建議你導入烏龜,並在需要某個函數時調用turtle.FUNCTIONNAME ,即使只是為了以后的可讀性(盡管我很確定它在內存和代碼運行時間方面也更好)不要一次導入所有函數,這就是調用FROM libraryname import *時發生的情況。

你也可以在你的MAIN函數中初始化你的變量,而不是在文件中全局初始化,以提高可讀性和組織性,而且如果你想導入這個文件中的函數,那么未在MAIN初始化的全局變量會妨礙.

還嘗試將代碼組織在函數中,在當前示例中,您調用的海龜函數無緣無故地分散在各處,您可以將它們全部包裝在一個新函數中並改為調用該函數一次。

最后確保刪除程序末尾的turtle.exitonclick() 調用。

import tkMessageBox
import turtle
def main () :
    turtle.onscreenclick(getPos)
    SetTimer() #call for the function to setup the timer, you can call this function when certain conditions are met rather than at startup.
    mainloop()

def SetTimer():
    canvas = turtle.getcanvas() #get current turtle canvas to set the after call on.
    canvas.after(2000, CalledAfter2000ms, canvas) #first parameter specifies time in miliseconds to wait before the call to CalledAfter2000ms function which is in the second paramater, the third paramater here is the paramater im passing to the CalledAfter2000ms function.

def CalledAfter2000ms(canvas): #function passed to canvas.after(...)
    if tkMessageBox.askyesno("EXIT","Do you really want to exit?"): #if yes is pressed, True is returned, if no is pressed then False is returned.
        turtle.bye() #exits the application

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM