# Working in Linux 02: Jtext2code–Use Python to make a QR code generator

Sometimes, I need copy some links or text from computer to my smart phone.  It's such a bad experience to type all of the contents by myself. So I made a tool  in Linux called "Jtext2code", which  can  generate a QR code according to the text you choose. Use your smart phone application like Scanner to scan the picture generated, you can get the information you choose easily.

Let's build this.

0.Preparations:

Install python, python-pip:

sudo apt-get install python python-pip


Install qrcode, a python lib can generate QR code:

sudo pip install qrcode


Install feh, a command line tool to view pictures:

sudo apt-get install feh


Jtext2code was made up with three parts.

• Get the clipboard information.
• Generate the QR code.
• Display the QR code picture.

I'll explain the three one by one.

1.Use python to get the text we choose. [1]

#!/usr/bin/python2.7
import pygtk
import gtk
# get the clipboard
clipboard = gtk.clipboard_get()
# read the clipboard text data. you can also read image and
# rich text clipboard data with the
# wait_for_image and wait_for_rich_text methods.
text=clipboard.wait_for_text()


2.Generate the QR code picture.

import qrcode
# Save text information to QR.png
qrcode_image=qrcode.make(text)
qrcode_image.save('QR.png')


3.Display the picture we just created.

import os
# Call feh in python to display picture
s=os.popen('feh QR.png')


Save all the three parts to one file name "Jtext2code.py"

Make it  executable:

chmod +x Jtext2code.py


sudo ln -s pwd/Jtext2code.py /bin/Jtext2code


When you use the tool you should first copy the text you want to transfer, then go to the terminal then run

Jtext2code


and you can get the QR code picture. Finally, use your smart phone to scan the picture.

Tip: You can create a launcher  in the LinuxMint(XFCE) for better using experience: Right click in the desktop to Create launcher

Besides, you can also drag the launcher to the panel.

So that it will be more convenient to open a terminal to run the program.

Remember: Copy text, then run Jtext2code

I want to know:

1. How can I get the PRIMARY selection in python, not CLIPBOARD selection?  (If I can get PRIMARY selection, I don't have to copy the text to the clipboard, just select the text,and run Jtext2code. More convenient, right? )

-------------------Oct 29th 2016---------------------------------

One possible answer: Use python to call Linux commands.

import commands
text=commands.getoutput("xsel -o")


Use the "xsel -o" to get the PRIMARY selection.

1. How can I add a new item like "Jtext2code" to the right click menu ?

(If I can do this, I can just select the text and right click, then -->Jtext2code. It's pretty cool.)

--------------Oct 29th 2016---------------------------------

Although I don't get the answer, but this is a solution to make it more convenient--Add a shortcut for this application:

Edit xfce4-keyboard-shortcuts.xml (for I can't add shortcuts in MintXfce settings.)

vim ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml


add the following line to this file

&lt;property name="&amp;lt;Alt&amp;gt;&amp;lt;Control&amp;gt;S" type="string" value="Jtext2code"/&gt;


Save this  file, and login out and in. Type Ctrl+Alt+S will trigger Jtext2code

Thank you very much.

References:

[微信] 扫描二维码打赏

[支付宝] 扫描二维码打赏

## “Working in Linux 02: Jtext2code–Use Python to make a QR code generator”的7个回复

1. LinuxMr说道：

Hello,Jack! I generated a code called "Hello,Jack!"

1. Now I know who the brainy one is, I'll keep loknoig for your posts.

1. It's really great that people are sharing this information.

2. LinuxMr说道：

Hello,Jack! Using your code, I generated a code called "Hello,Jack!" !

3. YUAN说道：

I just want to tell you I have been to this and play soy sauce . GOOD NIGHT.