paint-brush
How to Make a Twitter Desktop Widget with Pythonby@bigpesh
195 reads

How to Make a Twitter Desktop Widget with Python

by Grant PeachJanuary 6th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

You will need a Twitter account (either your own or create a new one) to create a quick way to send a tweet without having to open up a browser or the Twitter app altogether. We need to install and import a library to handle the Twitter API for us called tweepy. We then create a simple little Twitter Desktop Widget with a simple GUI, which just sits on my Desktop waiting for me to post a tweet. The code below is the code to build our own.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to Make a Twitter Desktop Widget with Python
Grant Peach HackerNoon profile picture


So the other night I found myself scrolling through the Twitter app on my Mac and decided to post a tweet, I posted the tweet and then thought to myself, why don’t I just make a quick way to send a tweet without having to open up a browser or the Twitter app altogether.
So I started to make a simple little Twitter Desktop Widget with a simple GUI, which just sits on my desktop waiting for me to post a tweet.


Here’s how to build your own…


First, we need to create a Project/Python file, let’s call thissqt.py(Super Quick Tweet)

Once this is done open up thesqt.pyin your favourite IDE or text editor.

Ok now we need to install and import a library to handle the Twitter API for us called tweepy, you can read the documentation__here__.

To install tweepy paste the below command into your terminal…

pip3 install tweepy

Then we can import it…

import tweepy

Now we need to create a Twitter Developer account and get our access tokens etc.

Note — You will need a Twitter account (either your own or create a new one)

You can do thishere
, it’s really simple to do.

After you have done the above, you should now be able to copy your key and token to your application like below…

Unknown

You have now assigned your keys to variables for us to use in our application, these are what grant us access to your Twitter account.

Next, we need to create another variable, let’s call this auth and set it to handle the authorisation for our application, by passing it our `consumer_key` and `consumer_secret`.

__auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

We can then use this to also pass our access_token.

auth.set_access_token(access_token, access_token_secret)

We can now assign another variable called API and pass it to our auth.

api = tweepy.API(auth)

Ok, that’s all we need to do for us to be able to interact with the Twitter API.

Next, we need to set up our GUI so we have somewhere to type our status and a way to post them.

We need to import some more libraries, we don’t need to usepip to install these as they are already built into Python.

import tkinter as tkfrom Tkinter.filedialog import askopenfilename

tkinteris a Python library used to build simple GUI’s below is the code to build ours…

I have added comments on what each line of code is doing. (# comment)

carbon

If you now run your application you should have a window on screen which looks like the one below…

Screenshot 2021-12-10 at 20.57.59

Ok, now we have a GUI we need to give our application some functionality.

First, we need to create apost_sqtfunction…

carbon-2

The above function grabs any text inside thetweet_input entry box and passes it to the tweepyAPI and sends the tweet to Twitter. We then clear the entry box ready for another tweet.


We now need to assign this function to ourpost_btn we do this by adding command=post_sqt to the end of post_btncreation like below…

carbon-3

Go on run your application and test it works.

If your application is posted to your Twitter timeline then it’s working as we intended.

Next, we need to add another function to do the same but this time has the ability to add an image.

So let’s create another function like below…

carbon-4

The above function does the same as thepost_sqtfunction except this time prompts us to select a file to upload to Twitter.

Assign this function to yourpost_wm_btnlike below…

carbon-5

You should now have a Python file that looks like the one below…

carbon-6

If your app works, we now need to make a real application by making it run without our IDE or terminal.

We do this by using a little program called py2app (this is for macOS) you can read the docshere.

Note — If you are on Windows then you can use py2exe, the docs on how to do this are
here__, the process is similar.

Open up your Terminal andcdinto your Python file directory.

Then run the below command to create asetup.pyfile.

$ py2applet --make-setup sqt.py

You will now see asetup.pyfile appear inside your applications directory.

If you open up yoursetup.pyfile you should have something similar to the below…

carbon-7
This is what py2app is going to use to build your application, in other words, it’s the settings for your application.

You can add an icon to your application by adding the below code inside the OPTIONS brackets like below…

Note - Your image needs to be of the format.icns

OPTIONS = {'iconfile': '/Users/medium/PycharmProjects/pythonProject/sqt/YOUR_IMAGE.icns'}

You can find .icns files here__if you don't want to create your own.

Ok, let’s build our application, run the below command inside your terminal…

__$ python setup.py py2app

Let it work its magic and you should see two folders appear inside your application directory,
build and dist

Screenshot 2021-12-10 at 21.41.59

Once complete, if you look inside yourdistfolder, you should now see your App. You should now be able to drag the App to your Applications folder on your Mac for you to run when you like.

I hope you have enjoyed this article be sure to follow me on Twitterhere__and be sure to tweet me from your SQT app.

If you don't want to build your own SQT App I have compiled SQT to download, no programming is needed 😃.

Download SQT (macOS x64)
Download SQT (Apple Silicon x64)