Last active
December 29, 2021 22:05
-
-
Save yinsee/a36f825e970f2534a2025927fbbe05b4 to your computer and use it in GitHub Desktop.
barcode scanner python gui
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
import requests | |
import json | |
import serial | |
import pymodbus | |
from pymodbus.client.sync import ModbusSerialClient | |
barcode = "" | |
toggle = True | |
def callapi(barcode): | |
myapp.lstatus["text"] = "Loading..." | |
mywin.update() | |
url = 'https://api.upcdatabase.org/product/' + barcode + '/097093029E4C133C95259D03085DC466' | |
headers = { "Accept": "application/json" } | |
response = requests.get(url, headers = headers) | |
print ("Api status: ", response.status_code) | |
print ("Api return: ", response.text) | |
jsondata = json.loads(response.text) | |
global myapp | |
myapp.lstatus["text"] = jsondata.get("title", 'Not Found') | |
if jsondata.get("title", False): | |
client.write_coil(16, True, unit=1) | |
else: | |
client.write_coil(16, False, unit=1) | |
class MyAppClass(Frame): | |
def __init__(self, master=None): | |
Frame.__init__(self, master) | |
self.grid() | |
self.create_label() | |
self.create_button() | |
def create_label(self): | |
self.lattrs = Label(self) | |
self.lattrs["text"] = "Scan Barcode" | |
self.lattrs.grid() | |
self.lstatus = Label(self) | |
self.lstatus["text"] = "..." | |
self.lstatus.grid() | |
def create_button(self): | |
self.battrs = Button(self) | |
self.battrs["text"] = "Click Me" | |
self.battrs["command"] = self.my_event | |
self.battrs.grid() | |
def my_event(self): | |
global toggle | |
toggle = not toggle | |
client.write_coil(17, toggle, unit=1) | |
def up(self,e): | |
global barcode | |
if e.keysym == 'Return': | |
self.lattrs["text"] = barcode | |
print (barcode) | |
callapi(barcode) | |
barcode = "" | |
else: | |
barcode = barcode + e.char | |
mywin = Tk() | |
mywin.title("Rasp PI GUI") | |
myapp = MyAppClass(mywin) | |
#mywin.bind('<KeyPress>', myapp.down) | |
mywin.bind('<KeyRelease>', myapp.up) | |
client = ModbusSerialClient(method='rtu', port='/dev/ttyUSB0', timeout=1, baudrate=9600, parity='N', bytesize=8) | |
if client.connect(): | |
myapp.lstatus["text"] = "Modsbus connection success" | |
else: | |
myapp.lstatus["text"] = "Modsbus connection failed" | |
mywin.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment