使用 Tkinter 編程還很陌生,我很難用它。我正在嘗試使用 python重新創建它,并且似乎是最合適的庫:Tkinter。
如視頻所示,我希望能夠看到我的代碼“正在運行”,并希望為迷宮創建添加一點睡眠。我目前只創建了網格和一個為其著色的函式。雖然,我無法一一獲得我的視窗著色矩形。添加睡眠會阻止程式,沒有它,所有顏色都會一起出現。
我見過的所有解決方案都在談論帶有文本修改的標簽,但我無法讓它為我作業。我錯過了什么嗎?
謝謝你的幫助 !
from tkinter import Tk, Canvas, Frame, BOTH
import random
from time import sleep
class Labyrinth(Frame):
def __init__(self):
super().__init__()
self.master.title("Laby")
self.pack(fill=BOTH, expand=1)
def creat_grid(self, square_side, laby_side):
grid = []
canvas = Canvas(self, width = laby_side * square_side, height = laby_side * square_side)
for y in range(laby_side):
subgrid = []
for x in range(laby_side):
if ((y % 2 == 0 or y == laby_side - 1 or x % 2 == 0) and not (x == 0 and y == 1) and not (x == laby_side - 1 and y == laby_side - 2)):
subgrid.append(1)
canvas.create_rectangle(square_side * x, square_side * y, square_side * x square_side, square_side * y square_side, fill="black")
else:
subgrid.append(0)
grid.append(subgrid)
canvas.pack(fill=BOTH, expand=1)
return canvas, grid
def color_grid(root, canvas, grid, square_side):
for y in range(len(grid)):
for x in range(len(grid)):
#sleep there blocks the program
if (grid[y][x] == 0 and not (x == 0 and y == 1) and not (x == len(grid) - 1 and y == len(grid) - 2)):
color = '#x' % random.randrange(16**3)
while (color in grid):
color = '#x' % random.randrange(16**3)
grid[y][x] = color
canvas.create_rectangle(square_side * x, square_side * y, square_side * x square_side, square_side * y square_side, fill=color)
#sleep there blocks the program
root.update()
#sleep there blocks the program
canvas.pack(fill=BOTH, expand=1)
def main():
root = Tk()
laby = Labyrinth()
square_side = 10
laby_side = 51
canvas, grid = laby.creat_grid(square_side, laby_side)
color_grid(root, canvas, grid, square_side)
root.mainloop()
if __name__ == '__main__':
main()
uj5u.com熱心網友回復:
可以.after()用來模擬跑步效果。此外,最好創建一次要填充的矩形,然后使用.after()以下方法一一更新它們的顏色:
from tkinter import Tk, Canvas, Frame, BOTH
import random
class Labyrinth(Frame):
def __init__(self):
super().__init__()
self.master.title("Laby")
self.pack(fill=BOTH, expand=1)
def create_grid(self, square_side, laby_side):
grid = []
canvas = Canvas(self, width=laby_side*square_side, height=laby_side*square_side, bg='black', highlightthickness=0)
# create the to-be-filled rectangles
for y in range(1, laby_side, 2):
for x in range(1, laby_side, 2):
x1, y1 = x*square_side, y*square_side
grid.append(canvas.create_rectangle(x1, y1, x1 square_side, y1 square_side, fill='white'))
canvas.pack()
return canvas, grid
# create set of colors
colors = [f'#{x:03x}' for x in range(0x1000)]
# shuffle the colors in random order
random.shuffle(colors)
def color_grid(canvas, grid, i=0):
canvas.itemconfig(grid[i], fill=colors[i])
if i < len(grid)-1:
# adjust the delay to suit your running effect
canvas.after(5, color_grid, canvas, grid, i 1)
def main():
root = Tk()
laby = Labyrinth()
square_side = 10
laby_side = 51
canvas, grid = laby.create_grid(square_side, laby_side)
color_grid(canvas, grid)
root.mainloop()
if __name__ == '__main__':
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323745.html
上一篇:將代碼和不一致描述的2列資料框轉換為每個唯一代碼的所有可能描述的嵌套串列
下一篇:使用帶有self的變數呼叫函式
