[英]Why is my Tkinter canvas displaying?
我正在嘗試使用Tkinter 2d圖形用戶界面生成一種星形算法。 我修改了下面的代碼,這些代碼最初是在這里找到的:
from tkinter import *
class CellGrid(Canvas):
def __init__(self,master, rowNumber, columnNumber, cellSize, theMap):
Canvas.__init__(self, master, width = cellSize * columnNumber , height = cellSize * rowNumber)
self.cellSize = cellSize
self.grid = []
for row in range(rowNumber):
line = []
for column in range(columnNumber):
line.append(Cell(self, column, row, cellSize, theMap[row][column]))
self.grid.append(line)
print (self.grid[0][0].value)
self.draw()
def draw(self):
for row in self.grid:
for cell in row:
cell.draw()
class Cell():
colors = {
0: 'white', # untried
1: 'black', # obstacle
2: 'green', # start
3: 'red', # finish
4: 'blue', # open
5: 'gray', # closed
6: 'orange', # path
}
def __init__(self, master, x, y, size, value):
self.master = master
self.abs = x
self.ord = y
self.size= size
self.fill = "white"
self.value = value
def setValue(self, value):
self.value = value
def draw(self):
if self.master != None :
if self.value == 0:
self.fill = self.white
elif self.value == 1:
self.fill = self.black
elif self.value == 2:
self.fill = self.green
elif self.value == 3:
self.fill = self.red
elif self.value == 4:
self.fill = self.blue
elif self.value == 5:
self.fill = self.gray
elif self.value == 6:
self.fill = self.orange
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = self.fill, outline = "black")
def main():
Map = [
[2, 0, 0, 0, 0],
[0, 1, 1, 1, 1],
[0, 1, 3, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0],
]
root = Tk()
c = tk.Canvas(root, height=1000, width=1000, bg='white')
my_gui = CellGrid(root, len(Map), len(Map[0]), 40, Map)
root.mainloop()
但是當我運行它時,什么也沒有顯示。
許多小的和不小的誤解阻礙了star算法的顯示。
問題1-調用main()
函數不完整。
正如@avysk建議的那樣,第一個阻塞點是由於Python中缺少
main()
前言所致。
只需在def main()
函數之后添加以下代碼:
if __name__ == '__main__':
main()
問題2-錯字和缺少Canvas
主顯示的pack()
。
由於未定義tk
符號,因此應將主Canvas聲明如下:
c = Canvas(root, height=1000, width=1000, bg='white')
代替:
c = tk.Canvas(root, height=1000, width=1000, bg='white')
在魔術丟失的電話之后是pack()
:
c.pack() # magic call to display the (1000 x 1000) white canvas
問題3-缺少對輔助Canvas
顯示的調用。
至於主要的Canvas
,它缺少對pack()
的調用,可以使用兩種可能性來顯示輔助的Canvas
。
my_gui.pack()
以使主Canvas
適應輔助Canvas
的大小, my_gui.place(relx=0.5, rely=0.5, anchor=CENTER)
以將輔助Canvas
my_gui.place(relx=0.5, rely=0.5, anchor=CENTER)
主Canvas
。 要顯示輔助Canvas
:
my_gui = CellGrid(c, len(Map), len(Map[0]), 40, Map)
my_gui.place(relx=0.5, rely=0.5, anchor=CENTER)
問題4 –對Cell::draw()
函數中的字典用法有誤解。
顯示的錯誤為“
AttributeError: 'Cell' object has no attribute 'green'
“。
第一個單元格self.grid[0][0].value = 2
並且在繪制單元格時,以下self.fill = self.green
生成錯誤。
可以使用三種可能性解決該錯誤:
self.fill = self.<color>
(7種情況)替換為self.fill = self.<color>
self.fill = '<color>'
, colors
並通過唯一調用替換這7種情況。 字典的使用可能是(sol 2):
if 0 <= self.value <= 6: # check if value in range of colors
self.fill = Cell.colors[self.value]
代替長的if-elif(sol 1):
if self.value == 0:
self.fill = 'white' # Error: self.white
elif self.value == 1:
self.fill = 'black' # Error: self.black
elif self.value == 2:
self.fill = 'green' # Error: self.green
elif self.value == 3:
self.fill = 'red' # Error: self.red
elif self.value == 4:
self.fill = 'blue' # Error: self.blue
elif self.value == 5:
self.fill = 'gray' # Error: self.gray
elif self.value == 6:
self.fill = 'orange' # Error: self.orange
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.