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.
First, we need to create a Project/Python file, let’s call thissqt.py
(Super Quick Tweet)
Once this is done open up thesqt.py
in 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
After you have done the above, you should now be able to copy your key and token to your application like below…
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
tkinter
is 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)
If you now run your application you should have a window on screen which looks like the one below…
Ok, now we have a GUI we need to give our application some functionality.
First, we need to create apost_sqt
function…
The above function grabs any text inside thetweet_input
entry box and passes it to the tweepy
API 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_btn
creation like below…
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…
The above function does the same as thepost_sqt
function except this time prompts us to select a file to upload to Twitter.
Assign this function to yourpost_wm_btn
like below…
You should now have a Python file that looks like the one below…
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
Open up your Terminal andcd
into your Python file directory.
Then run the below command to create asetup.py
file.$ py2applet --make-setup sqt.py
You will now see asetup.py
file appear inside your applications directory.
If you open up yoursetup.py
file you should have something similar to the below…
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
Once complete, if you look inside yourdist
folder, 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)