私のリアクションゲーム用のユーザーインターフェイスを構築しようとしています。お互いの上にフレームを設定しましたが、ストップウォッチ機能をフレームの1つに追加すると、開始ボタンが機能しません。私は、リセットボタンと終了ボタンをテストし、正常に動作します。ストップウォッチが始まらないので、一時停止ボタンをテストすることはできません。誰でも助けてくれますか?ここでPython Tkinterストップウォッチがフレーム上で動作しません
は、フレームとストップウォッチのためのコードである:ここで
class LvlOneR(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Level One",
background='white',
foreground='dodger blue',
font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
#stopwatch
def update_timeText():
if (state):
global timer
# Every time this function is called,
# we will increment 1 centisecond (1/100 of a second)
timer[2] += 1
# Every 100 centisecond is equal to 1 second
if (timer[2] >= 100):
timer[2] = 0
timer[1] += 1
# Every 60 seconds is equal to 1 min
if (timer[1] >= 60):
timer[0] += 1
timer[1] = 0
# We create our time string here
timeString = pattern.format(timer[0], timer[1], timer[2])
# Update the timeText Label box with the current time
timeText.configure(text=timeString)
# Call the update_timeText() function after 1 centisecond
self.after(10, update_timeText)
# To start game
def start():
global state
state = True
# To pause the game
def pause():
global state
state = False
# To reset the timer to 00:00:00
def reset():
global timer
timer = [0, 0, 0]
timeText.configure(text='00:00:00')
# To leave game our program
def leave():
controller.show_frame("ReactionGame")
# Simple status flag
# False mean the timer is not running
# True means the timer is running (counting)
state = False
# Our time structure [min, sec, centsec]
timer = [0, 0, 0]
# The format is padding all the
pattern = '{0:02d}:{1:02d}:{2:02d}'
# Create a timeText Label (a text box)
timeText = tk.Label(self, text="00:00:00",
background='white',
foreground='dodger blue',
font=("Helvetica", 150))
startButton = tk.Button(self, text='Start', command=start)
pauseButton = tk.Button(self, text='Pause', command=pause)
resetButton = tk.Button(self, text='Reset', command=reset)
quitButton = tk.Button(self, text='Quit', command=leave)
timeText.pack()
startButton.pack()
pauseButton.pack()
resetButton.pack()
quitButton.pack()
update_timeText()
は、フレームのセットアップのためのコードです:
class Game(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (LoginScreen, MainMenu, ReactionGame, MemoryGame, HighScores, LvlOneR):
page_name = F.__name__
frame = F(container, self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
frame.configure(background='white')
self.show_frame("LoginScreen")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
初めてこのコードを使用して申し訳ありません。見やすくするためにどのようなコードを入力する必要がありますか?ストップウォッチは、自分の作成したフレームに追加したときにはうまく動作しません。私はあなたの編集2を試しましたが、私はグローバル変数をupdate_timeTextの後に置くとタイマ変数を認識しません。 – James
... Windows上で動作します...あなたはどっちですか? – R4PH43L
Windows。私は問題を整理しました。とにかく助けてくれてありがとう – James