作为作业的一部分,我必须实施强化学习算法。我首先为自己设计了一些可视化,这样一旦我真正开始实施算法,我就可以知道发生了什么。我想使用 Tkinter,因为它似乎很容易根据代理(红色圆圈,见附图)采取的行动来进行图像迭代。本质上,我只想要代理移动到另一个方块的图像。它不能通过用户输入发生,一旦我按下运行,它应该自己执行整个算法。问题在于,由于root.mainloop()
,似乎无法更新视图。我应该怎么做,是否应该使用不同的可视化工具,或者有什么方法可以在每次迭代时简单地显示一个新屏幕(无需用户输入)?最后,我尝试使用.update()
但显然不鼓励使用该功能。
class GraphicalInterface:
##Initialisation of the GUI class.
def __init__(self, height, width):
self.blockColor = 'DarkSeaGreen3'
self.block_size=75
self.padding=1
self.frame_height = height*self.block_size
self.frame_width = width*self.block_size
self.root = Tk()
self.root.resizable(width=0, height=0)
self.frm = Frame(self.root, height=self.frame_height+8+self.padding*(height*2),
width=self.frame_width+8+self.padding*(width*2), background='lightgray',
cursor='circle', relief='sunken', borderwidth=4)
self.frm.grid_propagate(0)
self.frm.grid(column=0, row=0, padx=20, pady=20)
for i in range(0,width):
for j in range(0,height):
cur_frame = Frame(self.frm, background=self.blockColor, height=self.block_size, width=self.block_size)
cur_frame.grid(column=i,row=j, padx=self.padding, pady=self.padding)
def create_circle(self, x, y, r, canvasName, color): # center coordinates, radius https://stackoverflow.com/questions/17985216/simpler-way-to-draw-a-circle-with-tkinter
x0 = x - r
y0 = y - r
x1 = x + r
y1 = y + r
return canvasName.create_oval(x0, y0, x1, y1, fill=color)
def setObstacle(self, x, y):
cv = Canvas(self.frm, height=self.block_size-15, width=self.block_size-15, background= self.blockColor, highlightthickness=0)
self.create_circle(x = cv.winfo_reqheight()/2, y=cv.winfo_reqwidth()/2, r=30, canvasName=cv, color='black')
cv.grid(column=x, row=y, padx=0, pady=0)
def setAgent(self, x, y):
cv = Canvas(self.frm, height=self.block_size-15, width=self.block_size-15, background=self.blockColor, highlightthickness=0)
self.create_circle(x=cv.winfo_reqheight()/2, y=cv.winfo_reqwidth()/2, r=25, canvasName=cv, color='firebrick4')
cv.grid(column=x, row=y)
def run(self):
self.root.mainloop()