From the course: Python Data Structures and Algorithms

Pathfinding algorithms in the course maze GUI - Python Tutorial

From the course: Python Data Structures and Algorithms

Pathfinding algorithms in the course maze GUI

- [Instructor] In this video, we're just going to take a quick look at the code that generates the GUI that you've been working with on this course. So if we go into GUI Code and let's look at search dot PY. So this is an imported file that the GUI uses, and it contains the same data structures that we've been working with. So it has a priority queue. Then it has the algorithms. Now these algorithms are very similar to what we've done in the videos specific to each algorithm with one difference. And the main difference is that I wanted to create the full path. So rather than just coming up with the final path and then the opponent executing that kind of best result, I wanted you to see the path being generated. So I've created this variable called full path. And if the code is found that I've written in all the twists and turns in my return valley, rather than just the final path 'cause I didn't think that was as interesting. And that's true for all of them. So in BFS, I've returned to full path, which are built just by adding the current cell to the full path, which starts off as an empty list. Same in A-star as well. So that's the main thing in there. But just a couple of other things about the code, just in case you're interested in how it was generated. So basically it's using Python turtle graphics. So then let's look at the game version. Now, Python turtle graphics is an amazing tool, whatever level of learning you're out of your Python from total beginner to really quite advanced mathematical simulations and modeling and that kind of stuff, Python turtle is very, very powerful and very interesting to work with 'cause you get this instant visual feedback from your code. So I've used that as a module here. There's a couple of things about it that are slightly beyond what turtle graphics can do. I'll see if I can find an example. So these buttons. Here we go. The buttons location of the controls. Some people don't know that Python turtle graphics is built on the Tkinter module, Tkinter algorithm. Tkinter, different people pronounce it differently. So it's built on top of Tkinter, which is a much more powerful library than turtle graphics. So in a sense, turtle graphics is a subset of Tkinter. But from turtle graphics, you can access to Tkinter methods. So my general approach, when I'm doing these kind of GUI is I work within turtle graphics. And if that's all I need then great. But if not, then I bring in some functionality rather than going straight in for Tkinter 'cause usually is more than I need, often more than I need. What I've done is with my turtle graphics, I've used for example, TK-buttons, okay? And a few other features of TK. And so TK-button which was imported at the top as import Tkiniter as TK is. In order to be able to access those methods, I did have to create a canvas object and that's this line 43. I probably says the most crucial bit of this. If you're interested in working with Python turtle graphics and Tkinter in some combination. So line 43 is created a canvas object using turtle methods. Screen dot get canvas is a turtle method. But then I can use that canvas, which is a Tkinter object to do to Tkinter related stuff on like for example, putting my buttons on it as we saw in Create controls on line 136. So really that's not the focus of this course at all, but if you're interested in GUI development, quite in turtle graphics and to Tkinter, then there's a lot of ingredients here that you can mix and match in your own projects.

Contents