Generating a Postnet barcode

From Scribus Wiki
Revision as of 03:54, 28 February 2006 by Gpittman (talk | contribs)
Jump to navigation Jump to search

This script will generate a Postnet barcode (US Postal Service) for a Zipcode that you enter. It uses Tkinter to give you a requestor for the entry. You may enter your numbers and include hyphens if you wish, since these and any other non-numeric characters are ignored.

The barcode is a series of lines drawn to USPS specifications, and in this script is placed vertically (meaning the lines themselves are horizonal), with the beginning of the barcode toward the top of the page at position X = 130, Y = 130 (points).

To relocate the barcode, make the entire barcode a group, so that you can drag it wherever you like or use Properties to place it.

#!/usr/bin/env python

# File: postnet.py
# originally created 2006.02.26

import scribus
import Tkinter

class ImageDialog(Tkinter.Toplevel):
    
    def __init__(self, parent):
        Tkinter.Toplevel.__init__(self, parent, bg="#bbbbff")

        Tkinter.Label(self, text='Enter the Postal Code, Click OK',bg="#bbbbff").grid(row=0,columnspan=6)

        self.e = Tkinter.Entry(self)
        self.e.grid(row=1,columnspan=6)
        b = Tkinter.Button(self, text='OK', bg="#55ff88", command=self.ok)
        b.grid(row=2,columnspan=6)
        self.protocol("WM_DELETE_WINDOW", self.quit)

    def ok(self):
        S = self.e.get()
        m = Tkinter.Message(root, text=self.e.get()+'\nClose this\n window')
        m.grid(row=0, columnspan=4)
        postcode = ['llsss','sssll','sslsl','sslls','slssl','slsls','sllss','lsssl','lssls','lslss']
        a = 1.44      #line width
        b="Black"     #line color
        relx=130      #Start X
        rely=130      #Start Y
        if scribus.haveDoc():
            scribus.setRedraw(1)
            scribus.setUnit(0)
            d = scribus.createLine(relx,rely,relx+9,rely,) #Long line
            scribus.setLineWidth(a, d)
            scribus.setLineColor(b, d)
            scribus.setFillColor(b, d)
            rely = rely + 3.4
            for x in S[0:]:
               if x.isdigit():
                  xnum = int(x)
                  code = postcode[xnum]
                  for y in code[0:]:
                     if y == 'l':
                        d = scribus.createLine(relx,rely,relx+9,rely,) #Long line
                        scribus.setLineWidth(a, d)
                        scribus.setLineColor(b, d)
                        scribus.setFillColor(b, d)
                        rely = rely + 3.4
                     elif y == 's':
                        d = scribus.createLine(relx,rely,relx+3.6,rely,) #short line
                        scribus.setLineWidth(a, d)
                        scribus.setLineColor(b, d)
                        scribus.setFillColor(b, d)
                        rely = rely + 3.4
            d = scribus.createLine(relx,rely,relx+9,rely,) #long line
            scribus.setLineWidth(a, d)
            scribus.setLineColor(b, d)
            scribus.setFillColor(b, d)
            
            redrawAll()
            self.Tkinter.Toplevel.destroy()

root = Tkinter.Tk()
root.update()
z = ImageDialog(root)
root.wait_window(z)