Python3——利用pyqt制作的仓管系统

帮单位做了一个小型本地仓管系统,避免了在线系统更新不及时的缺点,可以及时记录仓库领料和入库的信息。共有5个文件main.py、Left.py、Right.py、InAndOut.py、IOTable.py。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#main.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
from Right import *
from Left import *
 
 
class WareHouse(QWidget):
    """docstring for wareHouse"""
    def __init__(self, arg=None):
        super(WareHouse, self).__init__()
        self.arg = arg
        self.init()
 
    def init(self):
        l = LeftSide()
        r = RightSide(lw=l)
        l.mt.setItemClickSignal(r)
 
        box = QHBoxLayout()
        box.addWidget(l)
        box.addWidget(r)
 
        box.setStretch(0,5)
        box.setStretch(1,3)
 
        self.setWindowTitle('震泽仓管')
        self.setLayout(box)
        self.showMaximized()
 
app = QApplication(sys.argv)
main = WareHouse()
sys.exit(app.exec_())
 
#Left.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sqlite3
 
class MainTable(QTableWidget):
    def __init__(self, arg=None):
        QTableWidget.__init__(self,arg)
        self.init()
 
    def init(self):
        conn = sqlite3.connect('database/test.db')
        c = conn.execute('SELECT * FROM warehouse')
        datas = c.fetchall()
 
        self.setWindowTitle('My Table')
        self.setRowCount(len(datas))
        self.setColumnCount(7)
 
        #设置表头
        self.setHorizontalHeaderLabels(['物料编码','物品名称','规格型号','批次','实物数量','单位','ID'])
 
        #设置为只读状态
        self.setEditTriggers(QAbstractItemView.NoEditTriggers);
        #设置为选中一行,默认为选中单格
        self.setSelectionBehavior(QAbstractItemView.SelectRows);
        #为双击事件添加一个方法(信号和槽)
        self.itemDoubleClicked.connect(self.cellDoubleClickedPrint)
 
        #隐藏列表头
        #tableWidget.verticalHeader().setVisible(False);
        #隐藏行表头
        #tableWidget.horizontalHeader().setVisible(False);
 
        #设置列表头宽度
        self.verticalHeader().setFixedWidth(35)
        #设置列宽
        self.setColumnWidth(0,110)
        self.setColumnWidth(1,260)
        self.setColumnWidth(2,190)
        self.setColumnWidth(3,100)
        self.setColumnWidth(4,80)
        self.setColumnWidth(5,65)
 
        #设置id列隐藏
        self.setColumnHidden(6,True)
 
        for i in range(len(datas)):
            for j in range(7):
                self.setItem(i,j, QTableWidgetItem(str(datas[i][j]))) 
        c.close() 
 
    def setItemClickSignal(self,rw):
        self.currentCellChanged.connect(lambda:rw.outTable.getCode(self))
        self.currentCellChanged.connect(lambda:rw.inTable.getCode(self))
 
    def cellDoubleClickedPrint(self):
        item = self.selectedItems()
        itemRow = item[0].row()
        keyword = item[1].text()
        print(keyword)
 
        conn = sqlite3.connect('database/test.db')
        c = conn.execute('SELECT * FROM warehouse WHERE item LIKE "%{}%"'.format(keyword))
        datas = c.fetchall()
        self.clear()
 
        self.setRowCount(len(datas))
        self.setColumnCount(7)
        self.setHorizontalHeaderLabels(['物料编码','物品名称','规格型号','批次','实物数量','单位'])
        for i in range(len(datas)):
            for j in range(7):
                self.setItem(i,j,QTableWidgetItem(str(datas[i][j])))
        self.setCurrentCell(1,1)
        c.close()
 
 
 
 
 
class SearchItem(QHBoxLayout):
    """docstring for searchItem"""
    def __init__(self, arg=None,mt=None):
        super(SearchItem, self).__init__()
        self.arg = arg
        self.mt = mt
        self.init()
 
    def init(self):
        label = QLabel('搜索框')
        self.line = QLineEdit()
        self.line.textChanged.connect(self.getSearchText)
 
        self.addWidget(label)
        self.addWidget(self.line)
 
 
    def getSearchText(self):
        searchText = self.line.text()
        print(searchText)
 
        #通过搜索框获取的文本内容更,从数据库中筛选数据并显示在表格内
        conn = sqlite3.connect('database/test.db')
        if not searchText == []: 
            c = conn.execute('''
                SELECT * FROM warehouse WHERE item LIKE "%{}%" 
                OR code LIKE "%{}%" 
                OR format LIKE "%{}%"
                '''.format(searchText,searchText,searchText))
        else:
            c = conn.execute('SELECT * FROM warehouse')
        datas = c.fetchall()
        self.mt.clear()
 
        self.mt.setRowCount(len(datas))
        self.mt.setColumnCount(6)
        self.mt.setHorizontalHeaderLabels(['物料编码','物品名称','规格型号','批次','实物数量','单位'])
        for i in range(len(datas)):
            for j in range(6):
                self.mt.setItem(i,j,QTableWidgetItem(str(datas[i][j])))
        c.close()
 
 
 
class LeftSide(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        vbox = QVBoxLayout()
        self.mt = MainTable()
        search = SearchItem(mt=self.mt)
 
        vbox.addLayout(search)
 
 
        vbox.addWidget(self.mt)
 
        self.setLayout(vbox)
        self.show()
 
#Right.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from InAndOut import *
from IOTable import *
import sqlite3
 
class RightSide(QWidget):
    """docstring for RightSide"""
    def __init__(self, arg=None, lw=None):
        super(RightSide, self).__init__()
        self.arg = arg
        self.lw = lw
        self.init()
 
    def init(self):
        outButton = QPushButton('领料出库')
        outLabel = QLabel('领料信息')
        self.outTable = OutTable()
        inButton = QPushButton('物品入库')
        inLabel = QLabel('入库信息')
        self.inTable = InTable()
 
        vbox = QVBoxLayout()
        vbox.addWidget(outButton)
        vbox.addWidget(outLabel)
        vbox.addWidget(self.outTable)
        vbox.addWidget(inButton)
        vbox.addWidget(inLabel)
        vbox.addWidget(self.inTable)
 
        desktop = QApplication.desktop()
        self.x = (desktop.width() - 380 )// 2
        self.y = (desktop.height() - 200)// 2
 
 
        #信号和槽
        outButton.clicked.connect(self.createOut)
        inButton.clicked.connect(self.createIn)
 
        self.setLayout(vbox)
        self.show()
 
    def createOut(self):
        self.outbutton = OutButton(win=self)
        self.outbutton.setGeometry(self.x, self.y, 380, 170)
        #子窗口模态化显示,要求outbutton是QDialog子类
        self.outbutton.exec_()
 
 
    def createIn(self):
        self.inbutton = InButton(win=self)
        self.inbutton.setGeometry(self.x, self.y, 380, 170)
        self.inbutton.exec_()
 
#InAndOut.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sqlite3
import re
import time
 
class LabelAndLine(QHBoxLayout):
    def __init__(self, arg=None):
        super(LabelAndLine,self).__init__()
        self.arg = arg
        self.init()
 
    def init(self):
        self.line = QLineEdit()
        self.addWidget(QLabel(self.arg))
        self.addWidget(self.line)
 
 
class OutButton(QDialog):
    def __init__(self, arg=None, win=None):
        super(OutButton,self).__init__()
        self.arg = arg
        self.lw = win.lw
        self.mt = win.lw.mt
        self.rw = win
        self.init()
 
    def init(self):
        self.setWindowTitle('物品领料')
        code = LabelAndLine('物品编码')
        name = LabelAndLine('物料名称')
        batch = LabelAndLine('物品批次')
        self.number = LabelAndLine('领料数量')
        self.remark = LabelAndLine('备注信息')
 
        self.button = QPushButton('确定')
        self.button.clicked.connect(self.buttonClicked)
 
        vbox = QVBoxLayout()
 
        #获取当前选中的条目信息
        item = self.mt.selectedItems()
        if(item):
            self.getCode = item[0].text()
            getName = item[1].text()
            getBatch = item[3].text()
 
            #设置默认占位符
            code.line.setPlaceholderText(self.getCode)
            name.line.setPlaceholderText(getName)
            batch.line.setPlaceholderText(getBatch)
           	#由于是领料,直接选取类别,不需要修改物品编码、名称、批次信息
            code.line.setEnabled(False)
            name.line.setEnabled(False)
            batch.line.setEnabled(False)
 
            vbox.addLayout(code)
            vbox.addLayout(name)
            vbox.addLayout(batch)
            vbox.addLayout(self.number)
            vbox.addLayout(self.remark)
            vbox.addWidget(self.button)
 
        else:
            vbox.addWidget(QLabel('请选择项目'))
 
        self.setLayout(vbox)
 
 
    def buttonClicked(self):
        #利用正则表达式判断输入是否为数字
        number = self.number.line.text()
        value = re.compile('\d*\.?\d*')
        result = value.match(number)
        #只有填写了数字,按确定才能反应
        if result.group() and result.group() == number:
            code = self.getCode
            num = float(number)
            data = time.strftime('%Y/%m/%d %H:%M:%S')
            remark = self.remark.line.text()
 
            oldNum = float(self.lw.mt.selectedItems()[4].text())
            newNum = oldNum - num
            row = self.lw.mt.row(self.lw.mt.selectedItems()[4])
            column = self.lw.mt.column(self.lw.mt.selectedItems()[4])
 
            conn = sqlite3.connect('database/test.db')
            c = conn.cursor()
            if newNum <= 0:
                c.execute('INSERT INTO outTable(code,num,data,remark) VALUES(?,?,?,?)',(code,oldNum,data,remark))
            else:
                c.execute('INSERT INTO outTable(code,num,data,remark) VALUES(?,?,?,?)',(code,num,data,remark))
            conn.commit()
            c.close()
 
 
 
            conn = sqlite3.connect('database/test.db')
            c = conn.cursor()
            if newNum <= 0:
                #c.execute('UPDATE warehouse SET num=? WHERE code=?;',(0,code))
                #self.lw.mt.setItem(row,column,QTableWidgetItem('0'))
                self.lw.mt.removeRow(row)
                c.execute('DELETE FROM warehouse WHERE code="{}";'.format(code))
            else:
                c.execute('UPDATE warehouse SET num=? WHERE code=?;',(newNum,code))
                self.lw.mt.setItem(row,column,QTableWidgetItem(str(newNum)))
 
            conn.commit()
            c.close()
 
            #按确定后刷新领料表格信息
            self.rw.outTable.getCode(self.lw.mt,code)
            self.close()
 
 
 
class InButton(QDialog):
    """docstring for InButton"""
    def __init__(self, arg=None, win=None):
        super(InButton, self).__init__()
        self.arg = arg
        self.lw = win.lw
        self.mt = win.lw.mt
        self.rw = win	
        self.init()
 
    def init(self):
        self.setWindowTitle('物品入库')
        self.code = LabelAndLine('物品编码')
        self.name = LabelAndLine('物料名称')
        batch = LabelAndLine('物品批次')
        self.number = LabelAndLine('入库数量')
        self.unit = LabelAndLine('物品单位')
        self.remark = LabelAndLine('备注信息')
 
 
        self.button = QPushButton('确定')
        self.button.clicked.connect(self.buttonClicked)
 
        #获取当前选中的条目信息
        item = self.mt.selectedItems()
        if(item):
            getCode = item[0].text()
            getName = item[1].text()
            getBatch = item[3].text()
            getNumber = item[4].text()
            self.code.line.setPlaceholderText(getCode)
            self.name.line.setPlaceholderText(getName)
            batch.line.setPlaceholderText(getBatch)
            #number.line.setPlaceholderText(getNumber)
 
        vbox = QVBoxLayout()
        vbox.addLayout(self.code)
        vbox.addLayout(self.name)
        vbox.addLayout(batch)
        vbox.addLayout(self.number)
        vbox.addLayout(self.unit)
        vbox.addLayout(self.remark)
        vbox.addWidget(self.button)
 
        self.setLayout(vbox)
 
    def buttonClicked(self):
        #利用正则表达式判断输入是否为数字
        number = self.number.line.text()
        value = re.compile('\d*\.?\d*')
        result = value.match(number)
        #只有填写了数字,按确定才能反应
        if result.group() and result.group() == number:          
            num = float(number)
            data = time.strftime('%Y/%m/%d %H:%M:%S')
            remark = self.remark.line.text()
 
            #判断是在原有编码的物品增加,还是新建一个物品
            code = self.code.line.text()
            item = self.mt.selectedItems()
            #在原有上增加
            if code == '' and item:
 
                code = item[0].text()
                oldNum = float(item[4].text())
                newNum = oldNum + num
                row = self.lw.mt.row(self.lw.mt.selectedItems()[4])
                column = self.lw.mt.column(self.lw.mt.selectedItems()[4])
 
                conn = sqlite3.connect('database/test.db')
                c = conn.cursor()
                c.execute('INSERT INTO inTable(code,num,data,remark) VALUES(?,?,?,?)',(code,num,data,remark))
                c.execute('UPDATE warehouse SET num=? WHERE code=?;',(newNum,code))
                self.lw.mt.setItem(row,column,QTableWidgetItem(str(newNum)))
                conn.commit()
                c.close()
 
            #新建物品
            else:
                itemName = self.name.line.text()
                itemFormat = itemBatch = ''
                itemUnit = self.unit.line.text()
 
 
                conn = sqlite3.connect('database/test.db')
                c = conn.cursor()
                c.execute('INSERT INTO inTable(code,num,data,remark) VALUES(?,?,?,?)',(code,num,data,remark))
                if item:
                    c.execute('INSERT INTO warehouse VALUES(?,?,?,?,?,?,NULL)', (code,itemName,itemFormat,itemBatch,num,itemUnit))
                else:
                	itemName = 'NoName'
                	c.execute('INSERT INTO warehouse VALUES(?,?,?,?,?,?,NULL)', (code,itemName,itemFormat,itemBatch,num,itemUnit))
                conn.commit()
                c.close()
 
                row = self.lw.mt.rowCount()
                datas = [code,itemName,itemFormat,itemBatch,num,itemUnit]
                #刷新主表格,增加新的一行
                self.lw.mt.setRowCount(row + 1)
                for i in range(6):
                    self.lw.mt.setItem(row,i,QTableWidgetItem(str(datas[i])))
                self.lw.mt.setCurrentCell(row,1)
 
 
 
            #按确定后刷新领料和出库表格信息
            self.rw.inTable.getCode(self.lw.mt,code)  
            self.rw.outTable.getCode(self.lw.mt,code)  
            self.close()
 
#IOTable.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sqlite3
 
 
class OutTable(QTableWidget):
    """docstring for OutTable"""
    def __init__(self, arg=None):
        super(OutTable, self).__init__()
        self.arg = arg
        self.init()
 
    def init(self):
        self.setColumnCount(3)
 
        #设置表头
        self.setHorizontalHeaderLabels(['领料时间','领料数量','备注'])
        #设置为只读状态
        self.setEditTriggers(QAbstractItemView.NoEditTriggers);
        #设置为选中一行,默认为选中单格
        self.setSelectionBehavior(QAbstractItemView.SelectRows);
 
        #设置列表头宽度
        self.verticalHeader().setFixedWidth(35)
        #设置列宽
        self.setColumnWidth(0,150)
        self.setColumnWidth(1,80)
        self.setColumnWidth(2,230)
        self.setRowCount(1)
        for i in range(3):
            self.setItem(0,i, QTableWidgetItem('无'))
 
    def getCode(self,item,code=None):
        item = item.selectedItems()
        if item:
            code = item[0].text()
 
        conn = sqlite3.connect('database/test.db')
        c = conn.execute('SELECT data,num,remark FROM outTable WHERE code = "{}" ORDER BY ID'.format(code))
        #c = conn.execute('SELECT data,num,remark FROM out ORDER BY ID')
        datas = c.fetchall()
        if datas:       
            self.setRowCount(len(datas))
            for i in range(len(datas)):
                for j in range(3):
                    self.setItem(i,j, QTableWidgetItem(str(datas[i][j])))
            c.close()
 
        else:
            self.setRowCount(1)
            for i in range(3):
                    self.setItem(0,i, QTableWidgetItem('无'))
 
class InTable(QTableWidget):
    """docstring for inTable"""
    def __init__(self, arg=None):
        super(InTable, self).__init__()
        self.arg = arg
        self.init()
 
    def init(self):
        self.setColumnCount(3)
 
        #设置表头
        self.setHorizontalHeaderLabels(['领料时间','领料数量','备注'])
        #设置为只读状态
        self.setEditTriggers(QAbstractItemView.NoEditTriggers);
        #设置为选中一行,默认为选中单格
        self.setSelectionBehavior(QAbstractItemView.SelectRows);
 
        #设置列表头宽度
        self.verticalHeader().setFixedWidth(35)
        #设置列宽
        self.setColumnWidth(0,150)
        self.setColumnWidth(1,80)
        self.setColumnWidth(2,230)
        self.setRowCount(1)
        for i in range(3):
            self.setItem(0,i, QTableWidgetItem('无'))
 
    def getCode(self,item,code=None):
        item = item.selectedItems()
        conn = sqlite3.connect('database/test.db')
        if item:
            code = item[0].text()
        c = conn.execute('SELECT data,num,remark FROM inTable WHERE code = "{}" ORDER BY ID'.format(code))
        #c = conn.execute('SELECT data,num,remark FROM out ORDER BY ID')
 
        datas = c.fetchall()
        if datas:
            self.setRowCount(len(datas))
 
            for i in range(len(datas)):
                for j in range(3):
                    self.setItem(i,j, QTableWidgetItem(str(datas[i][j])))
            c.close()
 
        else:
            self.setRowCount(1)
            for i in range(3):
                    self.setItem(0,i, QTableWidgetItem('无'))

Add a Comment

电子邮件地址不会被公开。 必填项已用*标注