用qpython 3C写个扫码器

b站链接
收到反馈说gitee的链接失效,github的又打不开,所以写个博文

一、qpython 3C新建项目的步骤

1、 进入APP后点击编辑器

在这里插入图片描述

2、点击右上角的新建按钮,选择QSL4AAPP

在这里插入图片描述

3、输入项目名后,APP会自动新建一个空白的main.py,将下面的代码复制上去,保存并运行就可以开始运行扫码器

在这里插入图片描述

代码

#-*-coding:utf8;-*-
"""
This is a sample project which use SL4A UI Framework,
There is another Sample project: https://github.com/qpython-android/qpy-calcount

#中国条码查询中心
#https://www.gds.org.cn/#/barcodeList/index?type=barcode&keyword=6901108291090 

"""
import qpy
import androidhelper
import urllib.request as ur
from qsl4ahelper.fullscreenwrapper2 import *

droid = androidhelper.Android()

class MainScreen(Layout):
    def __init__(self):
        self.scanbarcode_result_list=[]
        super(MainScreen,self).__init__(str("""<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background="#ff0E4200"
	android:orientation="vertical"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<ImageView
		android:id="@+id/logo"
		android:layout_width="fill_parent"
		android:layout_height="0px"
		android:layout_weight="10"
	/>
	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="0px"
		android:orientation="vertical"
		android:layout_weight="20">

		<TextView
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:textSize="8dp"
			android:text="扫码结果"
			android:textColor="#ffffffff"
			android:layout_weight="1"
			android:gravity="center"
		/>
    </LinearLayout>

	<ListView
		android:id="@+id/data_list"
		android:layout_width="fill_parent"
		android:layout_height="0px"
		android:layout_weight="55"
		android:gravity="center"
 />

	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="0px"
		android:orientation="horizontal"
		android:layout_weight="8">
		<Button
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:text="扫码"
			android:id="@+id/but_scanbarcode"
			android:textSize="8dp"
			android:background="#ffEFC802"
			android:textColor="#ffffffff"
			android:layout_weight="1"
			android:gravity="center"/>
			<Button
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:text="复制"
			android:id="@+id/but_copybarcode"
			android:textSize="8dp"
			android:background="#00BFFF"
			android:textColor="#ffffffff"
			android:layout_weight="1"
			android:gravity="center"/>
		<Button
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:text="取消"
			android:id="@+id/but_exit"
			android:textSize="8dp"
			android:background="#ff06AF00"
			android:textColor="#ffffffff"
			android:layout_weight="1"
			android:gravity="center"/>
	</LinearLayout>
</LinearLayout>
"""),"SL4AApp")

    def ButtonText(self,button):
        droid = FullScreenWrapper2App.get_android_instance()
        Lc=len(button)
        if Lc>2:droid.dialogSetNeutralButtonText(button[2])
        if Lc>1:droid.dialogSetNegativeButtonText(button[1])
        if Lc==0:button=('OK',)
        droid.dialogSetPositiveButtonText(button[0])
    OK=('OK',)
    def Button(self,title='Test',message='Click OK',button=OK):
        droid = FullScreenWrapper2App.get_android_instance()
        droid.dialogCreateAlert(title, message)
        self.ButtonText(button)
        droid.dialogShow()
        return droid.dialogGetResponse().result

    def on_show(self):
        self.views.but_exit.add_event(click_EventHandler(self.views.but_exit, self.exit))
        #self.views.but_load.add_event(click_EventHandler(self.views.but_load, self.load))
        self.views.but_scanbarcode.add_event(click_EventHandler(self.views.but_scanbarcode, self.scanbarcode))
        self.views.but_copybarcode.add_event(click_EventHandler(self.views.but_copybarcode, self.copybarcode))
        self.views.data_list.add_event(itemclick_EventHandler(self.views.data_list, self.deletebarcode))

        pass

    def on_close(self):
        pass

    def load(self, view, dummy):
        droid = FullScreenWrapper2App.get_android_instance()
        droid.makeToast("Load")

        saved_logo = qpy.tmp+"/qpy.logo"
        ur.urlretrieve("https://www.qpython.org/static/img_logo.png", saved_logo)
        self.views.logo.src = "file://"+saved_logo
        
    def scanbarcode(self,view,dummy):
        try:
            droid = FullScreenWrapper2App.get_android_instance()
            droid.makeToast("扫码")
            barcode = droid.scanBarcode()
            #print(barcode.result)
            self.scanbarcode_result_list.append(barcode.result)
            #self.views.scanbarcode_results.text="\n".join(self.scanbarcode_result_list)
            #print(dir(self.views.data_list))
            self.views.data_list.set_listitems(self.scanbarcode_result_list)
        except Exception as e:
            print("扫码错误:",str(e))
            droid.makeToast("扫码错误:",str(e))
            
    def copybarcode(self,view,dummy):
        droid = FullScreenWrapper2App.get_android_instance()
        droid.setClipboard("\n".join(self.scanbarcode_result_list))
        droid.makeToast("已复制到粘贴板")
        
    def deletebarcode(self,view,dummy):
        droid = FullScreenWrapper2App.get_android_instance()
        #print(dummy.values())
        #print(list(dummy.values())[1]['position'])
        #droid.makeToast('delete'+str(list(dummy.values())[1]['position']))
        #print(type(list(dummy.values())[1]['position']))
        item=self.scanbarcode_result_list[int(list(dummy.values())[1]['position'])]
        a=self.Button('确认删除','从列表中删除{}'.format(item),('确定',))
        if not a.get('canceled'):
            a=a['which']
            if a=='positive':
                self.scanbarcode_result_list.pop(int(list(dummy.values())[1]['position']))
                self.views.data_list.set_listitems(self.scanbarcode_result_list)

    def exit(self, view, dummy):
        droid = FullScreenWrapper2App.get_android_instance()
        droid.makeToast("退出")
        FullScreenWrapper2App.close_layout()

if __name__ == '__main__':
    FullScreenWrapper2App.initialize(droid)
    FullScreenWrapper2App.show_layout(MainScreen())
    FullScreenWrapper2App.eventloop()

二、运行已有项目

后面重新进入app后怎么打开之前新建的项目呢?
很简单,点击项目和脚本,找到刚刚新建的项目,点击运行即可
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值