PyQt5, Garbage Classification Small Program-Executable File of Early Generation Window Program

Keywords: PHP Database Qt Programming

It's another day. Today's mental journey: (1) How to connect the front end and the back end? (2) Back-end database insertion data (3) completely do not use the previous dictionary check method (4) Suddenly found that object-oriented programming is actually very useful, and even more addictive (5) QLineEdit, QInputDialog, QGridLayout (6) with clicked.connect() trigger "events" (7) design functions to connect the database, QtSql (8) QSqlDatabase, QSqlQuery these two small things really let me tread on. The key code query = QSqlQuery() "instantiation", "query. prepare" (f'select Rclassification from Rub_cl where Rname="{text}") double quotation mark is extremely critical, the meaning of query.next() 10) finally lazily wrapped in pyinstaller as. exe even if hastily done hh. Note that the - D mode is used because the attached database is required.

The garbage sorting program is probably over. Although there are still many things to optimize, and there are many idea s in my mind, but I do not want to do at least now is lazy (smile). Yesterday, I saw a completed Wechat program, which is really very good, and I feel that it has reached the extreme function I can think of (Wechat Search program: "How to divide up domestic garbage"). It's really great. I still have a long way to go.

 

 1 import sys, sqlite3
 2 #from PyQt5.QtWidgets import (QWidget, QPushButton, QLabel, QLineEdit, QInputDialog, QApplication, QGridLayout)
 3 from PyQt5.QtWidgets import *
 4 from PyQt5 import QtSql
 5 from PyQt5.QtSql import *
 6 #from PyQt5.QtSql import QSqlDatabase, QSqlQuery
 7 
 8 class Example(QWidget):
 9     def __init__(self):
10         super().__init__()
11         self.initUI()
12 
13     def initUI(self):
14         self.btn = QPushButton('Start',self)#Button
15         self.btn.clicked.connect(self.showDialog)
16 
17         self.le = QLineEdit(self)#SingleLineEdit
18 
19         text = QLabel('yes')
20 
21         self.answer = QLineEdit(self)
22 
23         grid = QGridLayout()
24         grid.setSpacing(10)
25 
26         grid.addWidget(self.btn,1,0,1,3)#The last two parameters can set the row span. The row spans here are one row and three rows.
27         grid.addWidget(self.le,2,0)
28         grid.addWidget(text,2,1)
29         grid.addWidget(self.answer,2,2)
30 
31         self.setLayout(grid)
32 
33         self.setGeometry(300,300,350,350)
34         self.setWindowTitle('Input dialog')
35         self.show()
36 
37     def showDialog(self):
38         global text
39         text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter the rubbish:')
40         if ok:
41             self.le.setText(str(text))
42             self.showAnswer()
43 
44     def showAnswer(self):
45         db = QSqlDatabase.addDatabase('QSQLITE')
46         db.setDatabaseName('Rubbish.db')
47         db.open()
48         query = QSqlQuery()
49         query.prepare(f'select Rclassification from Rub_cl where Rname="{text}"')#Double quotation marks are the key
50         query.exec()
51         #query.next()
52         #print(query.value(0))
53         if not query.exec():
54             query.lastError()#Returns the last error message
55         else:
56             query.next()
57             '''
58             QSqlQuery The data set returned, record It stopped before the first record.
59             So, after obtaining the data set, it must be executed next()or first()To the first record,
60             This time record That's what works.
61             '''
62             answertoprint = query.value(0)
63             self.answer.setText(str(answertoprint))
64         
65         
66 
67 if __name__ == '__main__':
68 
69     app = QApplication(sys.argv)
70     ex = Example()
71     sys.exit(app.exec_())

pyinstaller's command-line code:

cd blablablablabla
pyinstaller -D(-F) -i icon.ico rubbish_classification.py

Add a few more websites for fear that I may forget later:

ico icon: https://www.easyicon.net/

Qt Documentation: https://doc.qt.io/qt-5/qsqlquery.html

QMessageBox class Doc: http://www.kuqin.com/qtdocument/qmessagebox.html

DevDocs API Documentation: https://docs.segmentfault.com/

 

 

 

 

I will do my homework tomorrow: -)

Posted by kilby on Tue, 04 Jun 2019 10:44:02 -0700