Category Archives: Python

Is related to the Python Programming language in some way

Getting the Correct Notebook Tab Across Platforms in wxPython

I was recently working on a GUI application that had a wx.Notebook in it. When the user changed tabs in the notebook, I wanted the application to do an update based on the newly shown (i.e. selected) tab. I quickly discovered that while it is easy to catch the tab change event, getting the right tab is not as obvious.

This article will walk you through my mistake and show you two solutions to the issue.

Here is an example of what I did originally:

# simple_note.py
 
import random
import wx
 
 
class TabPanel(wx.Panel):
 
    def __init__(self, parent, name):
        """"""
        super().__init__(parent=parent)
        self.name = name
 
        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))
 
        btn = wx.Button(self, label="Press Me")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        self.SetSizer(sizer)
 
 
class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """
 
 
    def __init__(self):
        """Constructor"""
        super().__init__(None, wx.ID_ANY,
                         "Notebook Tutorial",
                         size=(600,400)
                         )
        panel = wx.Panel(self)
 
        self.notebook = wx.Notebook(panel)
        self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.on_tab_change)
        tabOne = TabPanel(self.notebook, name='Tab 1')
        self.notebook.AddPage(tabOne, "Tab 1")
 
        tabTwo = TabPanel(self.notebook, name='Tab 2')
        self.notebook.AddPage(tabTwo, "Tab 2")
 
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        self.Layout()
 
        self.Show()
 
    def on_tab_change(self, event):
        # Works on Windows and Linux, but not Mac
        current_page = self.notebook.GetCurrentPage()
        print(current_page.name)
        event.Skip()
 
 
if __name__ == "__main__":
    app = wx.App(False)
    frame = DemoFrame()
    app.MainLoop()

Continue reading Getting the Correct Notebook Tab Across Platforms in wxPython

Sending email with EZGmail and Python

Have you ever wanted to send an email with GMail using the Python programming language? In 2018, Al Sweigart, best-selling author of Automate the Boring Stuff with Python, created a package called EZGmail. You can also use Google’s own bindings to do this sort of thing, but it’s a lot more complicated than using EZGmail.

In this article, we will take a quick look at how to use this package.


Setting Up

Your first step is to install EZGmail using pip. Here’s how:

pip install ezgmail

Then go to https://developers.google.com/gmail/api/quickstart/python and click the Enable the Gmail API button. This will allow you to download a credentials.json file and also give you a client ID and client secret. You can use the latter credentials with Google’s Python API client and you can manage these credentials here if you need to.

Now copy the credentials file to the location that you plan on writing your code. You will then need to run Python in your terminal in the same location that your downloaded credentials file is located.

The next step is to run ezgmail.init(). This will open up a web browser to Gmail where it will ask you to allow access to your application. If you grant access, EZGmail will download a tokens file so that it doesn’t need to have you reauthorize it every time you use it.

To verify everything is working correctly, you can run the following code:

>>> ezgmail.EMAIL_ADDRESS
'your_email_address@gmail.com'

This should cause your Gmail account name to print out. Continue reading Sending email with EZGmail and Python

PyDev of the Week: Stefan van der Walt

This week we welcome Stefan van der Walt (@stefanvdwalt) as our PyDev of the Week! Stefan is the creator of scikit-image, which is a collection of algorithms for image processing. You can see some of the projects that he is a part of on Github or on Berkeley’s website. Stefan also has his own website which is worth checking out. Let’s take a few moments to get to know Stefan better!

Can you tell us a little about yourself (hobbies, education, etc):

I am currently a researcher at the Berkeley Institute for Data Science (BIDS) at the University of California, Berkeley. I was born and raised in the university town of Stellenbosch, South Africa—renowned for its beautiful nature and world-class wines—where I studied electronic engineering, computer science, and applied mathematics. Growing up there, it was easy to fall in love with nature: I love running and hiking in the mountains, and exploring in general. Nowadays, most of my hobby time is spent with my two children, aged 1 and 3. Continue reading PyDev of the Week: Stefan van der Walt

PyDev of the Week: Sean McManus

This week we welcome Sean McManus (@musicandwords) as our PyDev of the Week! Sean is the author of several books, including Mission Python: Code a Space Adventure Game!, which was reviewed on this site in March. There are free chapters from his book available here. You can learn more about Sean on his website. Let’s take some time to get to know him better!

Can you tell us a little about yourself (hobbies, education, etc):

I’m a writer specialising in technology. In recent years I’ve written several books to get children and young adults into coding. The launch of the Raspberry Pi and initiatives such as Code Club have helped to make coding much more accessible to young people than it was for many years.

As a kid, I loved programming my Amstrad CPC computer and in many ways it started me on my career path. I had listings and articles published in the leading magazines of the day, and my first book was about Amstrad programming. Today, I can still remember how much I loved programming as a kid, and I hope that my books bring some of that excitement to today’s budding coders.

As well as writing books and articles, I help some of the world’s leading technology companies with their copywriting requirements.

Outside of writing, I enjoy photography and making music. I have recorded an album of electronic music I plan to release online later this year, and I am a member of a singing group which is a great way to unwind. Continue reading PyDev of the Week: Sean McManus

PyDev of the Week: Adrienne Tacke

This week we welcome Adrienne Tacke (@AdrienneTacke) as our PyDev of the Week! Adrienne is the author of Coding for Kids: Python: Learn to Code with 50 Awesome Games and Activities and her book came out earlier this year. You can see what Adrienne is up to on Instagram or via her website. Let’s take some time to get to know her better!

Can you tell us a little about yourself (hobbies, education, etc):

I’m a software engineer in Las Vegas and have a degree in Management Information Systems from UNLV. I’ve worked in the education and healthcare industries and now focus on building awesome things in the fintech space. I love learning new languages (spoken and programming), eating every dessert imaginable, traveling the world with my husband, and finding ways to encourage more young girls and women to try out a career as a software engineer. Continue reading PyDev of the Week: Adrienne Tacke

How to Extract Build Info from Jenkins with Python

I work with continuous integration software as a part of my job. I use both Hudson and Jenkins in my role and occasionally need to interact with them programmatically. There are two Python packages you can use for this task:

The Python Jenkins package will work with both Hudson and Jenkins which JenkinsAPI only works with Jenkins. I usually use Python Jenkins because of this, although I have recently started looking to see which one works better with artifacts and I discovered that JenkinsAPI is actually better for that sort of thing. So you will need to evaluate both of these packages depending on what you need to do. Continue reading How to Extract Build Info from Jenkins with Python

PyDev of the Week: Tania Allard

This week we welcome Tania Allard (@ixek) as our PyDev of the Week! Tania is a developer advocate at Microsoft. She is also a speaker at multiple conferences. If you’d like to learn more about her, you should check out her blog. She also has some of her projects up on Github for you to peruse. Let’s take a few moments to get to know Tania!

Can you tell us a little about yourself (hobbies, education, etc):

I am originally from Mexico but have lived in the USA and in the UK for the last 8 years.

I have a bachelor’s in Mechatronic engineering and have also always been fascinated by technology and I can class myself as a lifelong learner. As such I got a PhD from the University of Manchester in Data science applied to Materials science, during which I discovered and fell in love with Python. Since completing my PhD I have worked as a research software engineer, research engineer, data engineer, and more recently could advocate.

Apart from tech I love Olympic weightlifting, so I spend quite a good amount of time in the gym every week and I am already looking forward for this year’s competition season!

I also love craft beer and recently joined the women in beer scene in Manchester, UK where I live. Continue reading PyDev of the Week: Tania Allard

An Intro to StaticBox and StaticBoxSizers

There are many widgets that are included with the wxPython GUI toolkit. One of them is a fairly handy widget called wx.StaticBox. This widget accepts a string and then will draw a box with the string in the upper left-hand corned of the box. However this only works when you use it in conjunction with wx.StaticBoxSizer.

Here is an example of what one might look like:

Simple wx.StaticBox

Now let’s go ahead and write the code you would use to create the example above: Continue reading An Intro to StaticBox and StaticBoxSizers

Creating GUI Applications with wxPython Now Available

My latest book, Creating GUI Applications with wxPython is now available for purchase.

Cover art for Creating GUI Applications with wxPython

Creating GUI Applications with wxPython is a book that will teach you how to use wxPython to create applications by actually creating several mini-programs. I have found that while learning how the various widgets work in wxPython is valuable, it is even better to learn by creating a simple application that does something useful.

In this book, you will be creating the following applications:

  • A simple image viewer
  • A database viewer
  • A database editor
  • Calculator
  • An Archiving application (tar)
  • PDF Merging application
  • XML Editor
  • File search utility
  • Simple FTP application
  • NASA Image downloader

As you learn how to create these applications, you will also learn how wxPython works. You will go over how wxPython’s event system works, how to use threads in wxPython, make use of sizers and much, much more!

The eBook version is on sale on Leanpub for $14.99 until May 15th. You can also purchase the book on Gumroad, or get the paperback or Kindle version on Amazon.

PyDev of the Week: Joel Grus

This week we welcome Joel Grus (@joelgrus) as our PyDev of the Week! Joel is the author of Data Science From Scratch: First Principles with Python from O’Reilly. You can catch up with Joel on his website or on Github. Let’s take some time to get to know Joel better!

Can you tell us a little about yourself (hobbies, education, etc):

In school I studied math and economics. I started my career doing quantitative finance (options pricing, financial risk, and stuff like that). I got very, very good at Excel, and I learned a tiny amount of SQL. But I kind of hated working in finance (and also I got laid off), so I joined an online travel startup as a “data analyst” doing BI stuff (lots of spreadsheets, lots of SQL, some very light scripting). That startup got acquired by Microsoft, who at the time had basically no idea what to do with my more-than-a-financial-analyst-less-than-a-software-engineer skillset. (Nor did I, really.)

Then in 2011 I saw that the winds were blowing toward “data science”, so I sort of BS-ed my way into a data scientist job at a tiny startup. I took a bunch of Coursera courses to fill in gaps in my knowledge, and then I learned to write (ugly) production code and discovered I really enjoyed building software. Through doing well in coding competitions I had the opportunity to interview for a software engineer job at Google, so I spent 6 really hectic weeks cramming computer science and then somehow passed the interview. I spent a couple of years at Google, and then I found I missed doing data and ML stuff, and so now I’m at the Allen Institute for Artificial Intelligence, where I build deep learning tools for NLP researchers. My current job is right at the intersection of deep learning and Python library design, which is a pretty great match for my interests.

I don’t really have time for hobbies 😢. I have an 8-year-old daughter, and I spend a lot of my free time with her, and then I keep agreeing and/or volunteering to write things and give talks and make livecoding videos, which takes up most of the rest. And then I have a podcast and a Twitter to stay on top of. I have long-term hobby goals of (1) learning jazz piano and (2) writing a novel, but I’m not really making much progress on either. Continue reading PyDev of the Week: Joel Grus