Python Forum
[PyGame] Window not updating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Window not updating
#2
Here's the Graphics.draw method:
    def draw(self, input, hidden, output, __weights_ih, __weights_ho):
        pg.init()
        size = (self.__SCREEN_WIDTH, self.__SCREEN_HEIGHT)
        screen = pg.display.set_mode(size)
        pg.display.set_caption("Neural Network Visualization")

        game_exit = False
        while not game_exit:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    sys.exit(0)

            screen.fill(self.white)
            self.update_input_nodes(matrix.Matrix.to_array(input))
            self.update_hidden_nodes(matrix.Matrix.to_array(hidden))
            self.update_output_nodes(matrix.Matrix.to_array(output))
            self.update_ih_weights(matrix.Matrix.to_array(__weights_ih))
            self.update_ho_weights(matrix.Matrix.to_array(__weights_ho))
            self.draw_ih_weights(screen, self.black, self.red)
            self.draw_ho_weights(screen, self.black, self.red)
            self.draw_input_nodes(screen, self.black, self.red)
            self.draw_hidden_nodes(screen, self.black, self.red)
            self.draw_output_nodes(screen, self.black, self.red)
            pg.display.update()
            game_exit = True
This is called by NeuralNet.train(), which itself is called 4 times for each iteration (40,000 times total).

With that in mind, here's what I see...
1) the draw method creates a window. It doesn't just draw, it creates a new window, starts a new event loop, etc. If it wasn't for the game_exit = True at the end of the while loop, this would be an infinite loop that would never do anything.
2) because the draw method is called everytime there's new data to draw, I don't think the Graphics class needs to maintain updating the interface at all. ie: the event loop can just be removed entirely, as it'll be called when it needs to update the screen.
3) I'd suggest moving the window-creation to it's own function and calling that from init (or just moving the creation to init). draw() shouldn't have anything to do with that, unless you want thousands of windows popping up with each individual state of the neural net.

So that brings us to here:
class Graphics:
    def __init__(self):
        # whatever's currently in your init
        self.screen = self.open_window()

    def open_window(self):
        pg.init()
        size = (self.__SCREEN_WIDTH, self.__SCREEN_HEIGHT)
        screen = pg.display.set_mode(size)
        pg.display.set_caption("Neural Network Visualization")
        return screen

    def draw(self, input, hidden, output, __weights_ih, __weights_ho):
        self.screen.fill(self.white)
        self.update_input_nodes(matrix.Matrix.to_array(input))
        self.update_hidden_nodes(matrix.Matrix.to_array(hidden))
        self.update_output_nodes(matrix.Matrix.to_array(output))
        self.update_ih_weights(matrix.Matrix.to_array(__weights_ih))
        self.update_ho_weights(matrix.Matrix.to_array(__weights_ho))
        self.draw_ih_weights(self.screen, self.black, self.red)
        self.draw_ho_weights(self.screen, self.black, self.red)
        self.draw_input_nodes(self.screen, self.black, self.red)
        self.draw_hidden_nodes(self.screen, self.black, self.red)
        self.draw_output_nodes(self.screen, self.black, self.red)
        pg.display.update()
Reply


Messages In This Thread
Window not updating - by mberge - Jun-19-2019, 09:00 PM
RE: Window not updating - by nilamo - Jun-19-2019, 09:39 PM
RE: Window not updating - by metulburr - Jun-19-2019, 11:33 PM
RE: Window not updating - by mberge - Jun-21-2019, 12:12 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020