我想創建一個網格并按順序為一些單元格著色,以便用戶可以看到單元格打開。
目前我有:
from tkinter import *
center=Tk()
center.geometry('455x455')
center.title("9x9 grid")
cells = {}
for row in range(9):
for column in range(9):
cell = Frame(center, bg='white', highlightbackground="black",
highlightcolor="black", highlightthickness=1,
width=50, height=50, padx=3, pady=3)
cell.grid(row=row, column=column)
cells[(row, column)] = cell
def color_cell(cells, i, j, color="red"):
cells[(i,j)].configure(background=color)
center.after(5000, color_cell(cells, 3, 4))
center.mainloop()
問題是我在看到一切之前等待了 5000 毫秒。我想先看到空白網格,然后在 5000 毫秒后,其中一個單元格變為紅色。最終目標是能夠說明滲透演算法(因此可視化必須高效)。目前我選擇了 Tkinter 但這可能有點矯枉過正,如果你有一個更簡單的選擇,我全都聽著。謝謝。
uj5u.com熱心網友回復:
問題是你叫color_cell的center.after(),而不是傳遞一個參考的功能。你必須這樣做center.after(5000, color_cell, cells, 3, 4):
center = Tk()
center.geometry('455x455')
center.title("9x9 grid")
cells = {}
for row in range(9):
for column in range(9):
cell = Frame(center, bg='white', highlightbackground="black",
highlightcolor="black", highlightthickness=1,
width=50, height=50, padx=3, pady=3)
cell.grid(row=row, column=column)
cells[(row, column)] = cell
def color_cell(cells, i, j, color="red"):
cells[(i, j)].configure(background="red")
center.after(5000, color_cell, cells, 3, 4)
center.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398958.html
上一篇:如何從最近到最近的日期排序
