Posts

Showing posts from October, 2021

HTML Web Page Builder

Image
I am attending college part time right now. The college gave me access to an O'Reilly digital library. It has books published by O'Reilly. But it also has a lot of other books too. It feels like an Amazon Kindle Unlimited subscription for technical books. So far, I have checked out quite a few of the books, especially those related to Python. I have stored a picture of all the covers of the good books on my local hard drive. Each book has a directory with the book name as the directory name. I have standardized on calling the book cover image "cover.jpg". Now I would like to browse all these books of interest. There are over 100 of them. So I wrote a small Python program to go through the root directory, find all the book names, and create a web page with all the covers. I standardized the size of the covers, and made them small so I could see more per page. Voila.

Tkinter Details

Image
I was working on a very simple game. When you see two shapes of the same color, you need to "snap" them. I was following a tutorial from a book to write this program. They decided to use the tkinter module for the GUI. You would think that pygame is the right tool for the job. But I guess there are many ways to code a game. The user presses a key (such as the letter q) to to perform a snap when they see matching colors. I was trying to trap this key press event like this:     Canvas.bind('q',my_handler_fxn) However, my function was never getting called when I pressed the key. I was banging my head against the wall for a little while. Then I identified that you need to tell tkinter to send those key press events to the Canvas object with this simple call:     Canvas.focus_set() With that small details taken care of, I was off to the races playing my snap game.

Memories of Logo

Image
The turtle module in python reminds me of the old Logo programming language. I recall it as a teaching language from the 1980s. It was marketed as a simple language that kids could use to learn how to do rudimentary programming. You would give the turtle directions, and it would draw pictures on the screen for you. I am reading a book where they encourage you to write your own functions to create shapes using turtle. For example, while you could write the turtle calls out to draw a rectangle, it would be easier just to call your own rectangle() function. Sounds good to me. Right now I am doing some block style drawing. But pretty soon I am going to make some patterns like Spirograph, another item from the 1980s.

Trouble Connecting to Database

Image
I had just discovered how to get data from Hacker News via their API. My python program could grab all kinds of info that came in the form of JSON. There was a ton of data available. I would be pulling a thousand posts from there. What was I going to do with this data? Figured I might as well stash it in a database of my own to play around with. I have a local Oracle 12 database running on my computer. Why not store the data there? There is a handy cx_Oracle module that is supposed to make it easy to connect to an Oracle database and save data. For quite some time, I could not get my python program to connect to my Oracle database. At first my problem was that I specify which database using an Oracle SID. But the connection from cx_Oracle wanted a DSN. Luckily I found that cx_Oracle has a makedsn() function which will convert my SID to a DSN. Still had problems making a connection to Oracle. All right. It was time to trap the DatabaseError exception provided by cx_Oracle. The error was...

Game Programming Basics

Image
I had purchased a book that had two parts: (1) Python programming and (2) Python projects. By now I had learned enough Python basics. So I only needed ideas for interesting programs to write. I jumped right into the projects section of the book with enthusiasm. The projects were split into three sections. The first section was Python game programming with the pygame module. I was annoyed that the example game code resulted in a game window that stuck around, even when the program had ended. WTF? Again, I had previously done some Python learning. This included pygame. The missing part was the call to shut down the pygame game engine. Here is the code required to do so, in context: while True:     for event in pygame.event.get():         if event.type == pygame.QUIT:             pygame.quit()             sys.exit() I highlighted th...

Fighting with Matplotlib

Image
So I was doing an assignment from a Python book I was reading. Seemed simple enough. Estimate how much money you would have in retirement based on starting capital, yearly expenses, and the length of your retirement. How better to understand the results than putting the numbers in a chart? I turned to the common Matplotlib python package. Funny story - I originally thought it was called Math plotlib. My first graph came up. I was impressed on how much graphics I could get for the short amount of code I had written. One thing that bugged me was that the y-axis values were displayed in scientific notation. That was because I was assuming I started my retirement with a huge nest egg. By default, Matplotlib will show those big numbers in scientific notation. Ugly. After some investigation, I found a quick way to override this was to specify a font style of "plain". That would prevent Matplotlib.from using scientific notation. Here is the code to accomplish that:      matplotl...

Annoying Text Wrap

Image
So I was trying to learn some Tkinter GUI. Had a couple controls on the screen. Needed to display some text instructions and feedback to the user. Sounded like a job for a Message object. The only problem was that there was excessive word wrapping going on. I had a single sentence. But it was broken up into 4 lines like this: That was very annoying. My code seemed simple enough: output = tkinter.Message(text='Welcome to the game. Choose a door.') output.place(x=10,y=330,width=500,height=150) You would think the 500 width would allow the whole sentence to be draw in a single line. Turns out that is not the default behavior of this Message control. It took a while to find the property I needed to modify was the aspect. It controls the width to height ratio. The default wants the text to seem to be as high as it is wide. But that just does not look good. Here is my modification that corrected that: output = tkinter.Message(text='Welcome to the game. Choose a door.',aspect=...