用python编写一个程序,用kivy生成GUI界面,实现扫码数据存储在MySQL数据库中,同时在GUI界面可以实时显示扫码的数据,也能查询历史扫码存储的数据。为这个程序生产EXE可执行文件
时间: 2024-02-13 21:05:47 浏览: 172
以下是一个简单的Python程序,它使用Kivy生成GUI界面,将扫描到的数据存储在MySQL数据库中,并允许您查询历史扫描数据。为了将其转换为可执行文件,请使用PyInstaller或类似的工具。
```python
import mysql.connector
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from pyzbar import pyzbar
from datetime import datetime
# 创建数据库连接
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
# 创建数据表
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE IF NOT EXISTS scans (id INT AUTO_INCREMENT PRIMARY KEY, data VARCHAR(255), date DATETIME)")
# Kivy GUI界面布局
Builder.load_string('''
<ScanButton>:
text: 'Scan'
size_hint: 0.2, 0.2
<ScanResult>:
id: scan_result
orientation: 'vertical'
<ScanHistory>:
id: scan_history
orientation: 'vertical'
<MainScreen>:
orientation: 'horizontal'
ScanButton:
on_press: root.scan()
BoxLayout:
orientation: 'vertical'
Label:
text: 'Scan Result'
font_size: '20sp'
ScanResult:
BoxLayout:
orientation: 'vertical'
Label:
text: 'Scan History'
font_size: '20sp'
ScanHistory:
''')
# 扫描按钮
class ScanButton(Button):
def __init__(self, **kwargs):
super(ScanButton, self).__init__(**kwargs)
def on_press(self):
# 扫描二维码
result = self.scan_qr_code()
if result is not None:
# 将扫描结果添加到数据库
mycursor.execute("INSERT INTO scans (data, date) VALUES (%s, %s)", (result, datetime.now()))
mydb.commit()
# 更新扫描结果列表
scan_result = self.parent.parent.children[1].ids.scan_result
scan_result.add_scan_result(result)
# 扫描二维码
def scan_qr_code(self):
result = None
try:
# 使用摄像头扫描二维码
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
decoded_objs = pyzbar.decode(frame)
for obj in decoded_objs:
result = obj.data.decode('utf-8')
if result is not None:
break
cv2.imshow("QR Code Scanner", frame)
cv2.waitKey(1)
cv2.destroyAllWindows()
except:
# 摄像头无法使用
self.show_popup('Error', 'Cannot access camera')
return result
# 显示弹出窗口
def show_popup(self, title, message):
box = BoxLayout(orientation='vertical')
box.add_widget(Label(text=message))
button = Button(text='OK', size_hint=(1, 0.2))
box.add_widget(button)
popup = Popup(title=title, content=box, auto_dismiss=False)
button.bind(on_press=popup.dismiss)
popup.open()
# 扫描结果列表
class ScanResult(BoxLayout):
def __init__(self, **kwargs):
super(ScanResult, self).__init__(**kwargs)
self.scan_results = []
# 添加扫描结果
def add_scan_result(self, result):
self.scan_results.append(result)
self.clear_widgets()
for scan_result in self.scan_results:
self.add_widget(Label(text=scan_result))
# 扫描历史记录列表
class ScanHistory(BoxLayout):
def __init__(self, **kwargs):
super(ScanHistory, self).__init__(**kwargs)
self.scan_history = []
# 显示历史记录
def show_scan_history(self):
mycursor.execute("SELECT * FROM scans")
self.scan_history = mycursor.fetchall()
self.clear_widgets()
for scan in self.scan_history:
self.add_widget(Label(text=str(scan)))
# 主屏幕
class MainScreen(BoxLayout):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
# 扫描二维码
def scan(self):
self.children[0].on_press()
# Kivy应用程序
class ScanApp(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
ScanApp().run()
```
这个程序使用MySQL数据库来存储扫描到的数据,并使用Kivy生成GUI界面。它具有扫描按钮,用于扫描二维码,并将扫描结果添加到数据库。它还具有一个实时显示扫描数据的扫描结果列表和一个查询历史扫描数据的扫描历史记录列表。将此程序转换为EXE可执行文件,可以使用PyInstaller或其他类似的工具。
阅读全文