Two-dimension code generation software _python implemented by Python

Source: Internet
Author: User
Tags bind

A few days ago, I reckoned to be able to generate QR Code applet and be able to display it on the screen with Wxpython. Of course, I wanted to do it in pure python, and after a while, I found three candidates:

The Python-qrcode on the GitHub
The Pyqrcode on the SourceForge
The pyqrnative on the Goolge code

I tried python-qrcode as well as pyqrnative because they were able to run in Windows/mac/linux. There is no need to rely on additional libraries other than the Python image library. The Pyqrcode project requires some other prerequisites and cannot run on windows, so I don't want to dwell on it. I finally used some of the previously written photo Viewer program code, and then slightly modified, it became a QRCode viewer.

Begin

As I mentioned above, you only need the Python image Library, the GUI section we will use Wxpython. Python-qrcode is faster than pyqrnative to generate pictures and contains most of the QR code types you've seen.

Generate QR Codes

When you're ready for all you need, you can run the following code to see what Python does:

Import OS import wx try:import qrcode except importerror:qrcode = None try:import pyqrnative except Ror:pyqrnative = None ######################################################################## class QRPanel (WX. 
    Panel): "" "" "#----------------------------------------------------------------------def __init__ (self, parent): "" "Constructor" "WX. Panel.__init__ (self, parent=parent) self.photo_max_size = sp = wx. Standardpaths.get () self.defaultlocation = sp. Getdocumentsdir () img = wx. Emptyimage (240,240) Self.imagectrl = wx. Staticbitmap (self, Wx.id_any, WX.) Bitmapfromimage (img)) QRDATALBL = WX. Statictext (self, label= "Text to turn into QR Code:") Self.qrdatatxt = wx. Textctrl (self, value= "http://www.mousevspython.com", Size= (200,-1)) instructions = "Name QR image File" Instructlb L = wx. Statictext (self, label=instructions) Self.qrphototxt = wx. Textctrl (Self, size= (200,-1)) browsEBTN = wx. Button (self, label= ' change Save Location ') browsebtn.bind (WX. Evt_button, self.onbrowse) deflbl = "Default save location:" + self.defaultlocation self.defaultlocationlbl = wx. Statictext (self, label=deflbl) qrcodebtn = wx. button (self, label= "Create QR with QRCode") Qrcodebtn.bind (WX. Evt_button, self.onuseqrcode) pyqrnativebtn = wx. button (self, label= "Create QR with Pyqrnative") Pyqrnativebtn.bind (WX. Evt_button, SELF.ONUSEPYQR) # Create Sizer Self.mainsizer = wx. Boxsizer (WX. VERTICAL) Qrdatasizer = wx. Boxsizer (WX. Horizontal) Locationsizer = wx. Boxsizer (WX. Horizontal) Qrbtnsizer = wx. Boxsizer (WX. VERTICAL) Qrdatasizer.add (qrdatalbl, 0, WX. All, 5) Qrdatasizer.add (Self.qrdatatxt, 1, WX. All|wx. EXPAND, 5) self.mainSizer.Add (WX. Staticline (self, Wx.id_any), 0, WX. All|wx. EXPAND, 5 self.mainSizer.Add (qrdatasizer, 0, WX. EXPAND) self.mainSizer.Add (Self.imagectrl, 0, WX. All, 5) Locationsizer.add (InstRUCTLBL, 0, WX. All, 5) locationsizer.add (self.qrphototxt, 0, WX. All, 5) locationsizer.add (browsebtn, 0, WX. All, 5) self.mainSizer.Add (Locationsizer, 0, WX. All, 5) self.mainSizer.Add (SELF.DEFAULTLOCATIONLBL, 0, WX. All, 5) qrbtnsizer.add (qrcodebtn, 0, WX. All, 5) qrbtnsizer.add (pyqrnativebtn, 0, WX. All, 5) self.mainSizer.Add (Qrbtnsizer, 0, WX. All|wx. CENTER,) self. Setsizer (Self.mainsizer) self. Layout () #----------------------------------------------------------------------def onbrowse (Self, event): "" " "" Dlg = wx. Dirdialog (self, "Choose a directory:", style=wx. Dd_default_style) if dlg. ShowModal () = = Wx.ID_OK:path = dlg. GetPath () self.defaultlocation = Path Self.defaultLocationLbl.SetLabel ("Save location:%s"% path) dlg.
    Destroy () #----------------------------------------------------------------------def onuseqrcode (Self, event): "" "Https://github.com/lincolnloop/python-qrcode
    "" "QR = QRCode. QRCode (Version=1, box_size=10, border=4) Qr.add_data (Self.qrDataTxt.GetValue ()) Qr.make (fit=true) x = Qr.make_ Image () Qr_file = Os.path.join (Self.defaultlocation, Self.qrPhotoTxt.GetValue () + ". jpg") Img_file = open (qr_fil E, ' WB ') X.save (img_file, ' JPEG ') img_file.close () Self.showqrcode (qr_file) #-----------------------------

    -----------------------------------------def onusepyqr (Self, event): "" "http://code.google.com/p/pyqrnative/ "" "QR = Pyqrnative.qrcode (PYQRNATIVE.QRERRORCORRECTLEVEL.L) Qr.adddata (Self.qrDataTxt.GetValue ()) Qr.mak E () im = Qr.makeimage () Qr_file = Os.path.join (Self.defaultlocation, Self.qrPhotoTxt.GetValue () + ". jpg") im G_file = open (Qr_file, ' WB ') Im.save (img_file, ' JPEG ') img_file.close () Self.showqrcode (qr_file) #-------- --------------------------------------------------------------def showqrcode (self, filepath): "" "" "img = wx.Image (filepath, WX. Bitmap_type_any) # Scale The image, preserving the aspect ratio W = img. GetWidth () H = img. GetHeight () if W > h:neww = self.photo_max_size newh = self.photo_max_size * h/w else:new H = Self.photo_max_size NEWW = self.photo_max_size * w/h img = img. Scale (NEWW,NEWH) Self.imageCtrl.SetBitmap (WX. Bitmapfromimage (IMG)) self. Refresh () ######################################################################## class Qrframe (WX. Frame): "" "" "#----------------------------------------------------------------------def __init__ (self):" " Constructor "" "WX.
  Frame.__init__ (self, None, title= "QR Code Viewer", size= (550,500)) panel = Qrpanel (self) if __name__ = "__main__": App = WX. APP (False) frame = Qrframe () frame. Show () app. Mainloop ()

Python-qrcode Build Effect Chart:

Pyqrnative Build Effect Chart:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.