0

グラフィックウィンドウに時間(秒単位)をどのように表示できるか疑問に思っていました。ストップ ウォッチを作成しようとしていますが、ウィンドウにストップ ウォッチの png が表示されていますが、特定の領域のウィンドウに時間を表示するにはどうすればよいですか?
また、実際のストップウォッチ (hh:mm:ss) のように時間をフォーマットして、60 秒後に 1 分追加する方法はありますか?

from graphics import *
import time

def main():
    win = GraphWin('Stopwatch', 600, 600)
    win.yUp()

    #Assigning images
    stopWatchImage = Image(Point (300, 300), "stopwatch.png")
    startImage = Image(Point (210, 170), "startbutton.png")
    stopImage = Image(Point (390, 170), "stopbutton.png")
    lapImage = Image(Point (300, 110), "lapbutton.png")

    #Drawing images
    stopWatchImage.draw(win)
    startImage.draw(win)
    stopImage.draw(win)
    lapImage.draw(win)

main()
4

1 に答える 1

0

可能であれば、次のことを試すことができますが、実行時にこのエラーに気付くでしょう...

RuntimeError: メイン スレッドがメイン ループにありません

(Zelle Graphics は Tkinter の単なるラッパーです)

すべてのグラフィックスをコメント アウトすると、print ステートメントが 1 秒ごとに増加することがわかります。

from graphics import *
import datetime
from threading import Thread
import time

class StopWatch:
    def __init__(self):
        self.timestamp = datetime.time(hour = 0, minute = 0, second = 0)

    def __str__(self):
        return datetime.time.strftime(self.timestamp, '%H:%M:%S')

    def increment(self, hours=0, minutes=0, seconds=1):
        dummy_date = datetime.date(1, 1, 1)
        full_datetime = datetime.datetime.combine(dummy_date, self.timestamp)
        full_datetime += datetime.timedelta(seconds=seconds, hours=hours, minutes=minutes)
        self.timestamp = full_datetime.time()


def main():
    win = GraphWin('Stopwatch', 600, 600)
    watch = StopWatch()

    timer_text = Text(Point(200, 200), str(watch))
    timer_text.draw(win)

    def updateWatch():
        while True:
            time.sleep(1)
            watch.increment()
            print(str(watch))
            timer_text.setText(str(watch))

    t1 = Thread(target=updateWatch)
    t1.setDaemon(True)
    t1.start()
    t1.join()

    win.getMouse()


if __name__ == "__main__":
    main()
于 2016-11-08T23:44:43.047 に答える