本文整理汇总了Python中tkinter.Frame.after方法的典型用法代码示例。如果您正苦于以下问题:Python Frame.after方法的具体用法?Python Frame.after怎么用?Python Frame.after使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tkinter.Frame
的用法示例。
在下文中一共展示了Frame.after方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from tkinter import Frame [as 别名]
# 或者: from tkinter.Frame import after [as 别名]
class App:
def __init__(self, master):
self.frame = Frame(master)
self.frame.pack()
self.var = StringVar()
self.data_queue = Queue()
self.button = Button(self.frame, text=self.var)
self.button.pack(side=LEFT)
# start the "fetch" thread (just generate data from the clock)
self.stop_thread = False
self.thread = Thread(target=self.get_data, args=("DATA thread", self.data_queue))
self.thread.start()
# when user clicks "close" widget, call self.stop_all()
master.protocol("WM_DELETE_WINDOW", self.stop_all )
self.frame.after(500, self.updater)
def get_data(self, threadname, q):
while not self.stop_thread:
a = str(time.time())
self.data_queue.put(a)
print(f'get_data: put "{a}" to queue')
time.sleep(3)
def updater(self):
while not self.data_queue.empty():
var = self.data_queue.get()
print(f'updater: got {var}')
self.frame.after(500, self.updater) # call again after 500ms
def stop_all(self):
"""Catch the "window close event".
We must kill the thread and then call normal quit() function.
"""
self.stop_thread = True
self.thread.join() # wait for thread to stop
self.frame.quit() # call normal frame quit()
示例2: __init__
# 需要导入模块: from tkinter import Frame [as 别名]
# 或者: from tkinter.Frame import after [as 别名]
class TicTacToeGUI:
ai = None
def __init__(self, master):
# Initial Frame
self.frame = Frame(master)
self.frame.pack(fill="both", expand=True)
# Board canvas
self.canvas = Canvas(self.frame, width=300, height=300)
# Symbol selection buttons
self.x_button = Button(self.frame, text='Play as X', height=4,
command=self.set_player_x, bg='white',
fg='black')
self.o_button = Button(self.frame, text='Play as O', height=4,
command=self.set_player_o, bg='white',
fg='red')
# Game start button and info box
self.start_button = Button(self.frame, text="START", height=4,
command=self.start, bg='white', fg='purple')
self.info_box = Label(self.frame, text='Tic Tac Toe Game', height=4,
bg='white', fg='blue')
self.clean_game_board()
def start(self):
"""Sets up game board, starts a tic tac toe game,
and a new AI if one doesn't exist. AI makes first move if
playing as X
"""
self.set_game_board()
self.game = TicTacToe()
self.game.start()
if not self.ai:
self.ai = TicTacToeAI()
if self.ai_symbol == 'x':
self.ai_action()
def _board(self):
"""Draws tic tac toe board"""
self.canvas.create_rectangle(0, 0, 300, 300, outline="black")
self.canvas.create_rectangle(100, 300, 200, 0, outline="black")
self.canvas.create_rectangle(0, 100, 300, 200, outline="black")
def user_action(self, event):
"""Attempts to take action that matches user click. If the move is valid,
then calls the AI to make the next move. If not, displays the error.
"""
move_x = event.x // 100
move_y = event.y // 100
move_result = self.game.update(self.player_symbol, (move_x, move_y))
if move_result == "Success":
board_x = (200 * move_x + 100) / 2
board_y = (200 * move_y + 100) / 2
if self.player_symbol == 'x':
self.draw_x(board_x, board_y)
else:
self.draw_o(board_x, board_y)
if not self.completed():
# Wait a bit before calling the ai, for visual style
self.frame.after(500, self.ai_action)
else:
self.info_box['text'] = move_result
def ai_action(self):
"""Gets the next move from the AI based on current game state,
and plays.
"""
state = self.game.get_board()
move = self.ai.get_move(state)
move_result = self.game.update(self.ai_symbol, move)
if move_result == "Success":
board_x = (200 * move[0] + 100) / 2
board_y = (200 * move[1] + 100) / 2
if self.ai_symbol == 'x':
self.draw_x(board_x, board_y)
else:
self.draw_o(board_x, board_y)
self.completed()
def completed(self):
"""Checks the game status. If completed, displays the result,
and asks whether the player would like to start another game.
"""
status = self.game.done()
if status == 'e':
return False
message = "Click to start a new game."
if status == 't':
message = "Tie game. " + message
else:
message = "Player " + status.upper() + " has won. " + message
self.info_box.pack_forget()
self.start_button.pack(fill="both", expand=True)
self.start_button["text"] = message
self.start_button["command"] = self.clean_game_board
#.........这里部分代码省略.........