简体   繁体   English

如何为强化学习环境显示 tkinter-canvas

[英]How to display tkinter-canvas for Reinforcement Learning environment

I'm creating a custom Reinforcement Learning environment .我正在创建一个自定义强化学习环境 So far, the environment is simply a 3 x 3 grid.到目前为止,环境只是一个 3 x 3 的网格。 I would like to create a custom environment which is why I do not work with OpenAI Gym.我想创建一个自定义环境,这就是我不使用 OpenAI Gym 的原因。 The ultimate objective is that a DQN- agent finds a suitable path to maximize the possible rewards and reaches a destination on the grid (Let's say, eg, goal is to get to field with coordinates [2|2]).最终目标是 DQN 代理找到一条合适的路径以最大化可能的奖励并到达网格上的目的地(例如,目标是到达坐标 [2|2] 的场)。

I have created a sample class for the environment (class Env).我为环境(环境类)创建了一个示例 class。 The "architecture" of the grid is described in the function build_canvas(self) . function build_canvas(self)中描述了网格的“架构”。 As visible, I used tkinter.canvas for defining the grid system.可见,我使用tkinter.canvas来定义网格系统。 Unfortunately, when I try to instantiate an object of type Env , the grid is not displayed.不幸的是,当我尝试实例化Env类型的 object 时,不会显示网格。

    class Env(tk.Tk):

        def __init__(self):
            super(Env, self).__init__()
            print("This is the standard constructor of our 
                   environment class.")
            self.build_canvas()

        def build_canvas(self):
            canvas = tk.Canvas(self, bg='green', height=HEIGHT, 
            width=WIDTH)

           ## Create grid with 3x3 (3 rows with 3 columns)
            for c in range(0, WIDTH, 60):
                 x1, y1, x2, y2 = c, 0, c, HEIGHT
                 canvas.create_line(x1, y1, x2, y2)
            for r in range(0, HEIGHT, 60):
                 x1, y1, x2, y2 = 0, r, HEIGHT, r
                 canvas.create_line(x1, y1, x2, y2)

            canvas.pack()
            return canvas


        def render(self):
            print("This renders the environment to the screen.")

        def reset(self):
            print("This resets the environment.")

       def step(self, action):
            print("This takes an action and the environment.")


   if __name__ == "__main__":
           env = Env()

It just prints out the strings to the console, however, the grid is not loaded at all.它只是将字符串打印到控制台,但是根本没有加载网格。 Does anyone have suggestions?有人有建议吗?

1- you need to call mainloop() on your root (here env ). 1-您需要在根目录(此处为env )上调用mainloop() ) 。
2- you must keep a reference of the canvas ( self.canvas ) 2-您必须保留 canvas 的参考( self.canvas
3- In python 3, you can call super like this: super().__init__() , without arguments. 3- 在 python 3 中,您可以像这样调用 super: super().__init__() ,没有 arguments。
4- There were some confusions between WIDTH and HEIGHT in the loop drawing the lines. 4- 在画线的循环中, WIDTHHEIGHT之间存在一些混淆。

import tkinter as tk


HEIGHT, WIDTH = 500, 500


class Env(tk.Tk):

    def __init__(self):
        super().__init__()
        print("This is the standard constructor of ourenvironment class.")
        self.canvas = self.build_canvas()
        self.canvas.pack()

    def build_canvas(self):
        canvas = tk.Canvas(self, bg='green', height=HEIGHT, width=WIDTH)

       ## Create grid with 3x3 (3 rows with 3 columns)
        for c in range(0, WIDTH, 60):
            x1, y1, x2, y2 = c, 0, c, HEIGHT
            canvas.create_line(x1, y1, x2, y2)
        for r in range(0, HEIGHT, 60):
            x1, y1, x2, y2 = 0, r, WIDTH, r
            canvas.create_line(x1, y1, x2, y2)

        return canvas


    def render(self):
        print("This renders the environment to the screen.")

    def reset(self):
        print("This resets the environment.")

    def step(self, action):
         print("This takes an action and the environment.")


if __name__ == "__main__":
    env = Env()
    env.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM