Python Forum
[PyQt] [Solved]Multiply a number to a DoubleSpinBox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] [Solved]Multiply a number to a DoubleSpinBox
#1
Hello,

I'm trying to make a sellPrice variable that takes in the user's input from the Price Input that I have and multiplies 1.10 to it.

This is a snippet of my PriceInput:
1
2
3
4
self.PriceInput = QtWidgets.QDoubleSpinBox(self.centralwidget)
       self.PriceInput.setStyleSheet("background-color: rgb(255, 255, 255);")
       self.PriceInput.setObjectName("PriceInput")
       self.formLayout_2.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.PriceInput)
And these are the variables I have that store the user's input:
1
2
3
4
5
6
7
8
9
10
11
#Store the user's inputs
ItemName = self.NameInput.text()
Quantity = self.QuantityInput.text()
Price = self.PriceInput.text()
Description = self.DescriptionInput.toPlainText()
Category = self.CategoryInput.currentText()
Location = self.LocationInput.text()
Barcode = self.BarcodeInput.text()
Length = self.spinBox.value()
sellPrice = Price * 1.10
mylist = [ItemName, Quantity, Price, sellPrice, Description, Category, Location, Length, Barcode]
If I try to run my code:
Error:
sellPrice = Price * 1.10 TypeError: can't multiply sequence by non-int of type 'float'
How do I fix this?

Thanks in advance.

This is my full code:
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
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
 
 
class Ui_AddItemMenu(QtWidgets.QMainWindow):
    def __init__(self, parent = None):
        super(Ui_AddItemMenu, self).__init__(parent)
        self.setObjectName("AddItemMenu")
        self.resize(804, 638)
        self.setStyleSheet("background-color: rgb(0, 170, 255);")
        self.centralwidget = QtWidgets.QWidget()
        self.centralwidget.setObjectName("centralwidget")
        self.formLayout_2 = QtWidgets.QFormLayout(self.centralwidget)
        self.formLayout_2.setObjectName("formLayout_2")
        self.AddItemTitle = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.AddItemTitle.setFont(font)
        self.AddItemTitle.setAlignment(QtCore.Qt.AlignCenter)
        self.AddItemTitle.setObjectName("AddItemTitle")
        self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.SpanningRole, self.AddItemTitle)
        self.NameLabel = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.NameLabel.setFont(font)
        self.NameLabel.setObjectName("NameLabel")
        self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.NameLabel)
        self.NameInput = QtWidgets.QLineEdit(self.centralwidget)
        self.NameInput.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.NameInput.setText("")
        self.NameInput.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
        self.NameInput.setObjectName("NameInput")
        self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.NameInput)
        spacerItem = QtWidgets.QSpacerItem(0, 5, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        self.formLayout_2.setItem(2, QtWidgets.QFormLayout.SpanningRole, spacerItem)
        self.QuantityLabel = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.QuantityLabel.setFont(font)
        self.QuantityLabel.setObjectName("QuantityLabel")
        self.formLayout_2.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.QuantityLabel)
        self.QuantityInput = QtWidgets.QSpinBox(self.centralwidget)
        self.QuantityInput.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.QuantityInput.setObjectName("QuantityInput")
        self.formLayout_2.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.QuantityInput)
        spacerItem1 = QtWidgets.QSpacerItem(0, 5, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        self.formLayout_2.setItem(4, QtWidgets.QFormLayout.FieldRole, spacerItem1)
        self.PriceLabel = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.PriceLabel.setFont(font)
        self.PriceLabel.setObjectName("PriceLabel")
        self.formLayout_2.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.PriceLabel)
        self.PriceInput = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.PriceInput.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.PriceInput.setObjectName("PriceInput")
        self.formLayout_2.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.PriceInput)
        spacerItem2 = QtWidgets.QSpacerItem(20, 5, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        self.formLayout_2.setItem(6, QtWidgets.QFormLayout.FieldRole, spacerItem2)
        self.DescriptionLabel = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.DescriptionLabel.setFont(font)
        self.DescriptionLabel.setObjectName("DescriptionLabel")
        self.formLayout_2.setWidget(7, QtWidgets.QFormLayout.LabelRole, self.DescriptionLabel)
        self.DescriptionInput = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.DescriptionInput.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.DescriptionInput.setObjectName("DescriptionInput")
        self.formLayout_2.setWidget(7, QtWidgets.QFormLayout.FieldRole, self.DescriptionInput)
        self.CategoryLabel = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.CategoryLabel.setFont(font)
        self.CategoryLabel.setObjectName("CategoryLabel")
        self.formLayout_2.setWidget(8, QtWidgets.QFormLayout.LabelRole, self.CategoryLabel)
        self.CategoryInput = QtWidgets.QComboBox(self.centralwidget)
        self.CategoryInput.setStyleSheet("background-color: rgb(225, 225, 225);")
        self.CategoryInput.setObjectName("CategoryInput")
        self.formLayout_2.setWidget(8, QtWidgets.QFormLayout.FieldRole, self.CategoryInput)
        spacerItem3 = QtWidgets.QSpacerItem(20, 5, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        self.formLayout_2.setItem(9, QtWidgets.QFormLayout.FieldRole, spacerItem3)
        self.LocationLabel = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.LocationLabel.setFont(font)
        self.LocationLabel.setObjectName("LocationLabel")
        self.formLayout_2.setWidget(10, QtWidgets.QFormLayout.LabelRole, self.LocationLabel)
        self.LocationInput = QtWidgets.QLineEdit(self.centralwidget)
        self.LocationInput.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.LocationInput.setObjectName("LocationInput")
        self.formLayout_2.setWidget(10, QtWidgets.QFormLayout.FieldRole, self.LocationInput)
        spacerItem4 = QtWidgets.QSpacerItem(20, 5, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        self.formLayout_2.setItem(11, QtWidgets.QFormLayout.FieldRole, spacerItem4)
        self.BarcodeLabel = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.BarcodeLabel.setFont(font)
        self.BarcodeLabel.setObjectName("BarcodeLabel")
        self.formLayout_2.setWidget(12, QtWidgets.QFormLayout.LabelRole, self.BarcodeLabel)
        self.BarcodeInput = QtWidgets.QLineEdit(self.centralwidget)
        self.BarcodeInput.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.BarcodeInput.setObjectName("BarcodeInput")
        self.formLayout_2.setWidget(12, QtWidgets.QFormLayout.FieldRole, self.BarcodeInput)
        spacerItem5 = QtWidgets.QSpacerItem(0, 5, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        self.formLayout_2.setItem(14, QtWidgets.QFormLayout.FieldRole, spacerItem5)
        self.label = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.formLayout_2.setWidget(15, QtWidgets.QFormLayout.LabelRole, self.label)
        self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
        self.spinBox.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.spinBox.setObjectName("spinBox")
        self.formLayout_2.setWidget(15, QtWidgets.QFormLayout.FieldRole, self.spinBox)
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setBold(False)
        font.setItalic(True)
        font.setWeight(50)
        self.label_2.setFont(font)
        self.label_2.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
        self.label_2.setObjectName("label_2")
        self.formLayout_2.setWidget(16, QtWidgets.QFormLayout.FieldRole, self.label_2)
        spacerItem6 = QtWidgets.QSpacerItem(20, 25, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        self.formLayout_2.setItem(17, QtWidgets.QFormLayout.FieldRole, spacerItem6)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setMinimumSize(QtCore.QSize(0, 0))
        self.pushButton.setMaximumSize(QtCore.QSize(16777215, 16777215))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.pushButton.setStyleSheet("background-color: rgb(225, 225, 225);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-color: black;\n"
"padding: 4px;")
        self.pushButton.setObjectName("pushButton")
        self.formLayout_2.setWidget(18, QtWidgets.QFormLayout.FieldRole, self.pushButton)
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setItalic(True)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.formLayout_2.setWidget(13, QtWidgets.QFormLayout.FieldRole, self.label_3)
        self.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar()
        self.statusbar.setObjectName("statusbar")
        self.setStatusBar(self.statusbar)
 
        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)
 
    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("AddItemMenu", "MainWindow"))
        self.AddItemTitle.setText(_translate("AddItemMenu", "Add Item Info"))
        self.NameLabel.setText(_translate("AddItemMenu", "Name"))
        self.QuantityLabel.setText(_translate("AddItemMenu", "Quantity"))
        self.PriceLabel.setText(_translate("AddItemMenu", "Price (per unit)"))
        self.DescriptionLabel.setText(_translate("AddItemMenu", "Description"))
        self.CategoryLabel.setText(_translate("AddItemMenu", "Category"))
        self.LocationLabel.setText(_translate("AddItemMenu", "Location"))
        self.BarcodeLabel.setText(_translate("AddItemMenu", "Barcode #"))
        self.label.setText(_translate("AddItemMenu", "Length (ft)"))
        self.label_2.setText(_translate("AddItemMenu", "*If the item is wire, please provide it\'s length in feet. If not, leave this field as is."))
        self.pushButton.setText(_translate("AddItemMenu", "Submit"))
        self.label_3.setText(_translate("AddItemMenu", "*Please generate a custom barcode for this item and enter that barcode number in this field "))
 
#----------------------------------------------------------------------------------------------------
#                                   Values for Categories
#----------------------------------------------------------------------------------------------------
        self.CategoryInput.addItems(['N/A','Ballast','Boxes','Breakers','Bulbs','Panels/Disconnects',
                                    'Pipes','Small Parts', 'Wire', 'Other'])
#----------------------------------------------------------------------------------------------------
 
#----------------------------------------------------------------------------------------------------
#                                       Button Actions
#----------------------------------------------------------------------------------------------------
        #------------------------------------------
                        #Submit Button
        #------------------------------------------
        #When the Submit button is clicked -> submitClicked Function
        SubmitButton = self.pushButton
        SubmitButton.clicked.connect(self.SubmitClicked)
        #------------------------------------------
#----------------------------------
    #Submit Function
    def SubmitClicked(self):
        #Store the user's inputs
        ItemName = self.NameInput.text()
        Quantity = self.QuantityInput.text()
        Price = self.PriceInput.text()
        Description = self.DescriptionInput.toPlainText()
        Category = self.CategoryInput.currentText()
        Location = self.LocationInput.text()
        Barcode = self.BarcodeInput.text()
        Length = self.spinBox.value()
        sellPrice = Price * 1.10
        mylist = [ItemName, Quantity, Price, sellPrice, Description, Category, Location, Length, Barcode]
 
        #Print in terminal for testing:
        print("The Submit Button was clicked")
        print(ItemName, Quantity, Price, Description, Category, Location, Barcode, Length)
 
        #Connect to the inventory database (inventory.db)
        connection = sqlite3.connect("inventory.db")
        cursor = connection.cursor()
     
        cursor = connection.cursor()
 
        cursor.execute('''
            insert into items (name, quantity, price, sell_price, description, category, location, Length_Ft, Barcode)
            values (?,?,?,?,?,?,?,?,?)
            ''', mylist)
        connection.commit()
 
        #Close the connection
        connection.close()
 
        #Switch from this screen to the AdminMenu
        #(Import AdminMenu here to prevent circular import error)
        from AdminMenu import Ui_MainDisplay
        self.win = Ui_MainDisplay() #Define LoginScreen
        self.win.show() #Show Login Screen
        self.close() #Close this screen (AdminMenu)
#----------------------------------
#----------------------------------------------------------------------------------------------------
 
 
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_AddItemMenu()
    ui.show()
    sys.exit(app.exec_())
Reply
#2
sellPrice = float(Price.replace(",", ".")) * 1.10
Reply
#3
(May-21-2022, 08:37 PM)Axel_Erfurt Wrote: sellPrice = float(Price.replace(",", ".")) * 1.10

(May-21-2022, 08:37 PM)Axel_Erfurt Wrote: sellPrice = float(Price.replace(",", ".")) * 1.10

Thanks, that worked!

I do have one more question though.
I'm trying to delete a row from the Table and I got it to delete from the InventoryDisplay (the table on the screen), but when I click my refresh button the row I "deleted" pops back up.
(I delete the rows by clicking the small number at the side of the table which highlights the whole row and then I click my delete button)

How do I get it so it actually deletes the row from the SQLite table and not just the screen?

Here's what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#----------------------------------
    #RemoveItem Function
    def RemoveItemClicked(self):
        #Print in terminal for testing:
        print("The Delete Item Button was clicked")
 
        #Delete Items from the Inventory
        indices = self.InventoryDisplay.selectionModel().selectedRows()
        for index in sorted(indices):
                x = self.InventoryDisplay.removeRow(index.row())
                connection = sqlite3.connect("inventory.db")
                cursor = connection.cursor()
                cursor.execute("DELETE FROM items WHERE id = ?", (x,))
                connection.commit()
                connection.close()
#----------------------------------
Full code:
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
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMainWindow,
                                QLabel, QLineEdit, QTableWidget, QTableWidgetItem,
                                QGridLayout, QVBoxLayout, QSizePolicy, QSpacerItem)
from PyQt5.QtCore import Qt, QMetaObject, QCoreApplication
from PyQt5.QtGui import QFont
 
import sqlite3
 
from AddItemScreen import Ui_AddItemMenu
 
class Ui_MainDisplay(QMainWindow):
    def __init__(self, parent = None):
        super(Ui_MainDisplay, self).__init__(parent)
        self.setObjectName("MainDisplay")
        self.setGeometry(0, 0, 1123, 903)
        self.setStyleSheet("background-color: rgb(0, 170, 255);")
        self.centralwidget = QWidget(self)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.verticalLayout = QVBoxLayout()
        self.verticalLayout.setContentsMargins(-1, 0, -1, 0)
        self.verticalLayout.setSpacing(6)
        self.verticalLayout.setObjectName("verticalLayout")
        self.AddItemButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.AddItemButton.setFont(font)
        self.AddItemButton.setStyleSheet("background-color: rgb(85, 255, 0);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"\n"
"")
        self.AddItemButton.setObjectName("AddItemButton")
        self.verticalLayout.addWidget(self.AddItemButton)
        self.DeleteItemButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.DeleteItemButton.setFont(font)
        self.DeleteItemButton.setStyleSheet("background-color: rgb(255, 65, 68);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.DeleteItemButton.setObjectName("DeleteItemButton")
        self.verticalLayout.addWidget(self.DeleteItemButton)
        self.CheckoutButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.CheckoutButton.setFont(font)
        self.CheckoutButton.setStyleSheet("background-color: rgb(255, 255, 0);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.CheckoutButton.setObjectName("CheckoutButton")
        self.verticalLayout.addWidget(self.CheckoutButton)
        self.ReturnButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.ReturnButton.setFont(font)
        self.ReturnButton.setStyleSheet("background-color: rgb(255, 170, 32);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.ReturnButton.setObjectName("ReturnButton")
        self.verticalLayout.addWidget(self.ReturnButton)
        self.ScanBarcodeButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.ScanBarcodeButton.setFont(font)
        self.ScanBarcodeButton.setStyleSheet("background-color: rgb(211, 211, 211);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.ScanBarcodeButton.setObjectName("ScanBarcodeButton")
        self.verticalLayout.addWidget(self.ScanBarcodeButton)
        self.MoreInfoButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.MoreInfoButton.setFont(font)
        self.MoreInfoButton.setStyleSheet("background-color: rgb(196, 17, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;")
        self.MoreInfoButton.setObjectName("MoreInfoButton")
        self.verticalLayout.addWidget(self.MoreInfoButton)
        self.RefreshButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.RefreshButton.setFont(font)
        self.RefreshButton.setStyleSheet("background-color: rgb(0, 255, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"\n"
"")
        self.RefreshButton.setObjectName("RefreshButton")
        self.verticalLayout.addWidget(self.RefreshButton)
        spacerItem = QSpacerItem(20, 40, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.LogoutButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.LogoutButton.setFont(font)
        self.LogoutButton.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.LogoutButton.setObjectName("LogoutButton")
        self.verticalLayout.addWidget(self.LogoutButton)
        self.gridLayout.addLayout(self.verticalLayout, 4, 3, 1, 1)
        self.Header = QLabel(self.centralwidget)
        font = QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.Header.setFont(font)
        self.Header.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;")
        self.Header.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.Header.setObjectName("Header")
        self.gridLayout.addWidget(self.Header, 0, 1, 1, 3)
        self.SearchButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.SearchButton.setFont(font)
        self.SearchButton.setStyleSheet("background-color: rgb(211, 211, 211);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.SearchButton.setObjectName("SearchButton")
        self.gridLayout.addWidget(self.SearchButton, 2, 1, 1, 1)
        self.SearchBar = QLineEdit(self.centralwidget)
        self.SearchBar.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.SearchBar.setObjectName("SearchBar")
        self.gridLayout.addWidget(self.SearchBar, 2, 2, 1, 1)
        self.InventoryDisplay = QTableWidget(self.centralwidget)
        font = QFont()
        font.setBold(False)
        font.setWeight(50)
        self.InventoryDisplay.setFont(font)
        self.InventoryDisplay.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"")
        self.InventoryDisplay.setObjectName("InventoryDisplay")
        self.InventoryDisplay.setColumnCount(11)
        self.InventoryDisplay.setRowCount(0)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(0, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(1, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(2, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(3, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(4, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(5, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(6, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(7, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(8, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(9, item)
        item = QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(10, item)
        self.gridLayout.addWidget(self.InventoryDisplay, 4, 1, 1, 2)
        self.setCentralWidget(self.centralwidget)
        self.statusbar = self.statusBar()
        self.statusbar.setObjectName("statusbar")
        #self.setStatusBar(self.statusbar)
 
        #Display the inventory
        self.DisplayInventory()
 
#----------------------------------------------------------------------------------------------------
#                                 Display Inventory
#----------------------------------------------------------------------------------------------------
 
    def DisplayInventory(self):
        connection = sqlite3.connect("inventory.db")
        cursor = connection.cursor()
        cursor.execute('select * from items')
        row = 0
        self.InventoryDisplay.setRowCount(40)
        while True:
                form = cursor.fetchone()
                if form == None:
                        break
                for column, item in enumerate(form):
                        self.InventoryDisplay.setItem(row, column, QTableWidgetItem(str(item)))
                print(str(item))
                row += 1
        connection.close()
#----------------------------------------------------------------------------------------------------
   
        self.retranslateUi(self)
        QMetaObject.connectSlotsByName(self)
   
    def retranslateUi(self, MainDisplay):
        _translate = QCoreApplication.translate
        self.setWindowTitle(_translate("MainDisplay", "AdminMenu"))
        self.AddItemButton.setText(_translate("MainDisplay", "Add Item"))
        self.DeleteItemButton.setText(_translate("MainDisplay", "Delete Item"))
        self.CheckoutButton.setText(_translate("MainDisplay", "Check Out"))
        self.ReturnButton.setText(_translate("MainDisplay", "Return"))
        self.ScanBarcodeButton.setText(_translate("MainDisplay", "Scan Barcode"))
        self.MoreInfoButton.setText(_translate("MainDisplay", "More Info"))
        self.RefreshButton.setText(_translate("MainDisplay", "Refresh"))
        self.LogoutButton.setText(_translate("MainDisplay", "Log Out"))
        self.Header.setText(_translate("MainDisplay", "Admin Menu"))
        self.SearchButton.setText(_translate("MainDisplay", "Search"))
        item = self.InventoryDisplay.horizontalHeaderItem(0)
        item.setText(_translate("MainDisplay", "ID"))
        item = self.InventoryDisplay.horizontalHeaderItem(1)
        item.setText(_translate("MainDisplay", "Name"))
        item = self.InventoryDisplay.horizontalHeaderItem(2)
        item.setText(_translate("MainDisplay", "Quantity"))
        item = self.InventoryDisplay.horizontalHeaderItem(3)
        item.setText(_translate("MainDisplay", "Price ($)"))
        item = self.InventoryDisplay.horizontalHeaderItem(4)
        item.setText(_translate("MainDisplay", "Sell Price ($)"))
        item = self.InventoryDisplay.horizontalHeaderItem(5)
        item.setText(_translate("MainDisplay", "Description"))
        item = self.InventoryDisplay.horizontalHeaderItem(6)
        item.setText(_translate("MainDisplay", "Category"))
        item = self.InventoryDisplay.horizontalHeaderItem(7)
        item.setText(_translate("MainDisplay", "Location"))
        item = self.InventoryDisplay.horizontalHeaderItem(8)
        item.setText(_translate("MainDisplay", "Length (Ft)"))
        item = self.InventoryDisplay.horizontalHeaderItem(9)
        item.setText(_translate("MainDisplay", "Barcode #"))
        item = self.InventoryDisplay.horizontalHeaderItem(10)
        item.setText(_translate("MainDisplay", "Date Updated"))
 
#----------------------------------------------------------------------------------------------------
#                                       Button Actions
#----------------------------------------------------------------------------------------------------
        #------------------------------------------
                        #Logout Button
        #------------------------------------------
        #When the Logout button is clicked -> LogoutClicked Function
        LogoutButton = self.LogoutButton
        LogoutButton.clicked.connect(self.LogoutClicked)
        #------------------------------------------
        #------------------------------------------
                        #Add Item Button
        #------------------------------------------
        #When the AddItem button is clicked -> AddItem Function
        AddItemButton = self.AddItemButton
        AddItemButton.clicked.connect(self.AddItemClicked)
        #------------------------------------------
        #------------------------------------------
                     #Remove Item Button
        #------------------------------------------
        #When the RemoveItem button is clicked -> RemoveItem Function
        RemoveItemButton = self.DeleteItemButton
        RemoveItemButton.clicked.connect(self.RemoveItemClicked)
        #------------------------------------------
        #------------------------------------------
                     #Checkout Item Button
        #------------------------------------------
        #When the Checkout button is clicked -> Checkout Function
        CheckoutButton = self.CheckoutButton
        CheckoutButton.clicked.connect(self.CheckoutClicked)
        #------------------------------------------
        #------------------------------------------
                     #Return Item Button
        #------------------------------------------
        #When the Return button is clicked -> Return Function
        ReturnButton = self.ReturnButton
        ReturnButton.clicked.connect(self.ReturnClicked)
        #------------------------------------------
        #------------------------------------------
                     #Scan Barcode Button
        #------------------------------------------
        #When the Scan Barcode button is clicked -> ScanBarcode Function
        ScanBarcodeButton = self.ScanBarcodeButton
        ScanBarcodeButton.clicked.connect(self.ScanBarcodeClicked)
        #------------------------------------------
        #------------------------------------------
                     #More Info Button
        #------------------------------------------
        #When the More Info button is clicked -> MoreInfo Function
        MoreInfoButton = self.MoreInfoButton
        MoreInfoButton.clicked.connect(self.MoreInfoClicked)
        #------------------------------------------
        #------------------------------------------
                     #Refresh Button
        #------------------------------------------
        #When the More Info button is clicked -> MoreInfo Function
        RefreshButton = self.RefreshButton
        RefreshButton.clicked.connect(self.RefreshClicked)
        #------------------------------------------
        #------------------------------------------
                     #Search Button
        #------------------------------------------
        #When the More Info button is clicked -> MoreInfo Function
        SearchButton = self.SearchButton
        SearchButton.clicked.connect(self.SearchClicked)
        #------------------------------------------
#----------------------------------
    #Logout Function
    def LogoutClicked(self):
        #Print in terminal for testing:
        print("The Logout Button was clicked")
        #Switch from this screen to the LoginScreen
        #(Import LoginScreen here to prevent circular import error)
        from LoginScreen import Ui_Loginscreen
        self.win = Ui_Loginscreen() #Define LoginScreen
        self.win.show() #Show Login Screen
        self.close() #Close this screen (AdminMenu)
#----------------------------------
#----------------------------------
    #AddItem Function
    def AddItemClicked(self):
        #Print in terminal for testing:
        print("The Add Item Button was clicked")
        #Switch from this screen to the AddItems Screen (Scene Swap):
        self.win = Ui_AddItemMenu()
        self.win.show()
        self.close()
#----------------------------------
#----------------------------------
    #RemoveItem Function
    def RemoveItemClicked(self):
        #Print in terminal for testing:
        print("The Delete Item Button was clicked")
 
        #Delete Items from the Inventory
        indices = self.InventoryDisplay.selectionModel().selectedRows()
        for index in sorted(indices):
                x = self.InventoryDisplay.removeRow(index.row())
                connection = sqlite3.connect("inventory.db")
                cursor = connection.cursor()
                cursor.execute("DELETE FROM items WHERE id = ?", (x,))
                connection.commit()
                connection.close()
#----------------------------------
#----------------------------------
    #Checkout Function
    def CheckoutClicked(self):
        #Print in terminal for testing:
        print("The Checkout Button was clicked")
#----------------------------------
#----------------------------------
    #Return Function
    def ReturnClicked(self):
        #Print in terminal for testing:
        print("The Return Button was clicked")
#----------------------------------
#----------------------------------
    #ScanBarcode Function
    def ScanBarcodeClicked(self):
        #Print in terminal for testing:
        print("The Scan Barcode Button was clicked")
#----------------------------------
#----------------------------------
    #MoreInfo Function
    def MoreInfoClicked(self):
        #Print in terminal for testing:
        print("The More Info Button was clicked")
#----------------------------------
#----------------------------------
    #Refresh Function
    def RefreshClicked(self):
        #Print in terminal for testing:
        print("The Refresh Button was clicked")
        #Close and reopen the app (Refresh)
        self.win = Ui_MainDisplay()
        self.win.show()
        self.close()
#----------------------------------
#----------------------------------
    #Search Function
    def SearchClicked(self):
        #Print in terminal for testing:
        print("The Search Button was clicked")
#----------------------------------
#----------------------------------------------------------------------------------------------------
 
  
if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Ui_MainDisplay()
    win.show()
   
    sys.exit(app.exec_())
Reply
#4
Have you checked whether the entry was really deleted in inventory.db?
Reply
#5
It was not really deleted in inventory.db. I think it has something to do with my DELETE FROM statement because I don't think I'm grabbing the index/row properly from the InventoryDisplay table on my screen/GUI and telling SQLite to delete it correctly.
Reply
#6
If you work with sqlite you should use QtSql.QSqlTableModel() preferably with QTableView.

Then you can delete a row and the database will be updated automatically.
Reply
#7
Just out of curiosity, is there a way to get the row number (the small numbers on the QtTable) that way I could set the Item's Id to the row number so if I were to delete the row the items below it would have their Id's subtracted by 1 to compensate for the deleted row?
Or how do I get/store the item's id from the id column on the Table when it's selected?

And thanks for the input. I'll start looking into QtSql.QSqlTableModel() and hopefully I can get it to work..
Reply
#8
Got it.
I'm now able to delete a selected row.

This is what I did:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#RemoveItem Function
def RemoveItemClicked(self):
    #Print in terminal for testing:
    print("The Delete Item Button was clicked")
 
    #Delete Items from the Inventory
    index = self.InventoryDisplay.currentIndex()
    NewIndex = self.InventoryDisplay.model().index(index.row(), 0)
    print('Index is :',NewIndex)
    indexID = self.InventoryDisplay.model().data(NewIndex)
    print(indexID)
 
    connection = sqlite3.connect("inventory.db")
    cursor = connection.cursor()
    cursor.execute("DELETE FROM items WHERE id = ?", (indexID,))
    connection.commit()
    connection.close()
Reply
#9
That's an example for QtSql

create Database

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
from PyQt5.QtSql import QSqlQuery, QSqlDatabase
from PyQt5.QtWidgets import QMessageBox, QApplication, qApp
  
def createDB():
   db = QSqlDatabase.addDatabase('QSQLITE')
   db.setDatabaseName('inventory2.db')
      
   if not db.open():
      QMessageBox.critical(None, qApp.tr("Cannot open database"),
         qApp.tr("Unable to establish a database connection.\n"
            "This example needs SQLite support. Please read "
            "the Qt SQL driver documentation for information "
            "how to build it.\n\n" "Click Cancel to exit."),
         QMessageBox.Cancel)
              
      return False
          
   query = QSqlQuery()
      
   query.exec_("""create table items (ID INTEGER PRIMARY KEY  AUTOINCREMENT,
                NAME           TEXT,
                Quantity       INT     NOT NULL,
                Price          DOUBLE  NOT NULL,
                Sell_Price     DOUBLE  NOT NULL,
                Description    TEXT,
                Category       TEXT,
                Location       TEXT,
                Length_Ft      INT,
                Barcode        INT,
                Image          BLOB,
                Date Updated   datetime default current_timestamp)""")
          
   query.exec_("insert into items values(0, 'MG996R ', '10', '5', '6', 'A Servo', 'Servos', 'S-1', '', '', '', '' )")
   query.exec_("insert into items values(1, 'MG997R ', '12', '6,45', '6,88', 'A Servo', 'Servos', 'S-1', '', '', '', '' )")
  
   return True
      
if __name__ == '__main__':
   import sys
   app = QApplication(sys.argv)
   createDB()
use it, changes are immediately transferred to the database.

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
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
from PyQt5.QtWidgets import (QApplication, QMainWindow, QDialog, QTableView,
                                QGridLayout, QPushButton, QWidget)
###################################
btnWidth = 100
class MyWindow(QMainWindow):
    def __init__(self, fileName, parent=None):
        super(MyWindow, self).__init__(parent)
        self.db = QSqlDatabase.addDatabase('QSQLITE')
        self.db.setDatabaseName('inventory2.db')
        self.model = QSqlTableModel()
        self.delrow = -1
        self.initializeModel()
 
        self.sbar = self.statusBar()
         
        self.viewer = QTableView()
        self.viewer.setModel(self.model)
        self.viewer.clicked.connect(self.findrow)
        self.viewer.selectionModel().selectionChanged.connect(self.getCellText)
         
        self.dlg = QDialog()
        self.layout = QGridLayout()
        self.layout.addWidget(self.viewer,0, 0, 1, 3)
         
        button = QPushButton("add a row")
        button.setFixedWidth(btnWidth)
        button.clicked.connect(self.addrow)
        self.layout.addWidget(button, 1, 0)
         
        btn1 = QPushButton("delete a row")
        btn1.setFixedWidth(btnWidth)
        btn1.clicked.connect(self.delete_row)
        self.layout.addWidget(btn1,1, 1)
 
        self.myWidget = QWidget()
        self.myWidget.setLayout(self.layout)
   
        self.setCentralWidget(self.myWidget)
        self.setGeometry(0,0,800,550)
        self.setWindowTitle("Database Demo")
        self.sbar.showMessage("Ready")
 
    def initializeModel(self):
       self.model.setTable('items')
       self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
       self.model.select()
       #self.model.setHeaderData(0, Qt.Horizontal, "Index")
       #self.model.setHeaderData(1, Qt.Horizontal, "Name")
       #self.model.setHeaderData(2, Qt.Horizontal, "Quantity")
       #self.model.setHeaderData(3, Qt.Horizontal, "Price")
       #self.model.setHeaderData(4, Qt.Horizontal, "Sell_Price")
       #self.model.setHeaderData(5, Qt.Horizontal, "Description")
       #self.model.setHeaderData(6, Qt.Horizontal, "Category")
       #self.model.setHeaderData(7, Qt.Horizontal, "Location")
       #self.model.setHeaderData(8, Qt.Horizontal, "Length_Ft ")
       #self.model.setHeaderData(9, Qt.Horizontal, "Barcode ")
       #self.model.setHeaderData(10, Qt.Horizontal, "Image ")
       #self.model.setHeaderData(11, Qt.Horizontal, "Date ")      
         
    def addrow(self):
        print(self.model.rowCount())
        ret = self.model.insertRows(self.model.rowCount(), 1)
        print(ret)
         
    def delete_row(self):
        row = self.viewer.currentIndex().row()
        self.model.removeRow(row)
        self.initializeModel()
        self.viewer.selectRow(row)
         
    def findrow(self, i):
        self.delrow = i.row()
 
    def getCellText(self):
        if self.viewer.selectedIndexes():
            item = self.viewer.selectedIndexes()[0]
            row = self.selectedRow()
            column = self.selectedColumn()
            if not item == None:
                name = item.data()
                self.sbar.showMessage(str(name))
 
    def selectedRow(self):
        if self.viewer.selectionModel().hasSelection():
            row =  self.viewer.selectionModel().selectedIndexes()[0].row()
            return int(row)
 
    def selectedColumn(self):
        column =  self.viewer.selectionModel().selectedIndexes()[0].column()
        return int(column)
 
###################################    
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    app.setApplicationName('MyWindow')
    main = MyWindow("")
    main.show()
    sys.exit(app.exec_())
Reply
#10
Oh ya, that's much better.
Thanks.

I do have one more questions though:

(1) I got the QsqlTableView formatted to my GUI but now there's a gray box in the top left corner of the screen/GUI under the Window/App name and I don't know what it is, or how it got there (I have attached a screenshot of the GUI/Screen). Why is that there and how do I get rid of it?



Thanks again.

My full code:
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
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMainWindow,
                                QLabel, QLineEdit, QTableWidget, QTableWidgetItem,
                                QGridLayout, QVBoxLayout, QSizePolicy, QSpacerItem,
                                QTableView)
from PyQt5.QtCore import Qt, QMetaObject, QCoreApplication
from PyQt5.QtGui import QFont
import sqlite3
from AddItemScreen import Ui_AddItemMenu
from Constants import MainDatabase
from Test2 import spindemo
 
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
 
class Ui_MainDisplay(QMainWindow):
    def __init__(self, parent = None):
        super(Ui_MainDisplay, self).__init__(parent)
        self.setObjectName("MainDisplay")
        self.setGeometry(0, 0, 1123, 903)
        self.setStyleSheet("background-color: rgb(0, 170, 255);")
        self.centralwidget = QWidget(self)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.verticalLayout = QVBoxLayout()
        self.verticalLayout.setContentsMargins(-1, 0, -1, 0)
        self.verticalLayout.setSpacing(6)
        self.verticalLayout.setObjectName("verticalLayout")
        self.AddItemButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.AddItemButton.setFont(font)
        self.AddItemButton.setStyleSheet("background-color: rgb(85, 255, 0);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"\n"
"")
        self.AddItemButton.setObjectName("AddItemButton")
        self.verticalLayout.addWidget(self.AddItemButton)
        self.DeleteItemButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.DeleteItemButton.setFont(font)
        self.DeleteItemButton.setStyleSheet("background-color: rgb(255, 65, 68);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.DeleteItemButton.setObjectName("DeleteItemButton")
        self.verticalLayout.addWidget(self.DeleteItemButton)
        self.CheckoutButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.CheckoutButton.setFont(font)
        self.CheckoutButton.setStyleSheet("background-color: rgb(255, 255, 0);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.CheckoutButton.setObjectName("CheckoutButton")
        self.verticalLayout.addWidget(self.CheckoutButton)
        self.ReturnButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.ReturnButton.setFont(font)
        self.ReturnButton.setStyleSheet("background-color: rgb(255, 170, 32);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.ReturnButton.setObjectName("ReturnButton")
        self.verticalLayout.addWidget(self.ReturnButton)
        self.ScanBarcodeButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.ScanBarcodeButton.setFont(font)
        self.ScanBarcodeButton.setStyleSheet("background-color: rgb(211, 211, 211);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.ScanBarcodeButton.setObjectName("ScanBarcodeButton")
        self.verticalLayout.addWidget(self.ScanBarcodeButton)
        self.MoreInfoButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.MoreInfoButton.setFont(font)
        self.MoreInfoButton.setStyleSheet("background-color: rgb(196, 17, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;")
        self.MoreInfoButton.setObjectName("MoreInfoButton")
        self.verticalLayout.addWidget(self.MoreInfoButton)
        self.RefreshButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.RefreshButton.setFont(font)
        self.RefreshButton.setStyleSheet("background-color: rgb(0, 255, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"\n"
"")
        self.RefreshButton.setObjectName("RefreshButton")
        self.verticalLayout.addWidget(self.RefreshButton)
        spacerItem = QSpacerItem(20, 40, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.LogoutButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.LogoutButton.setFont(font)
        self.LogoutButton.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.LogoutButton.setObjectName("LogoutButton")
        self.verticalLayout.addWidget(self.LogoutButton)
        self.gridLayout.addLayout(self.verticalLayout, 4, 3, 1, 1)
        self.Header = QLabel(self.centralwidget)
        font = QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.Header.setFont(font)
        self.Header.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;")
        self.Header.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.Header.setObjectName("Header")
        self.gridLayout.addWidget(self.Header, 0, 1, 1, 3)
        self.SearchButton = QPushButton(self.centralwidget)
        font = QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.SearchButton.setFont(font)
        self.SearchButton.setStyleSheet("background-color: rgb(211, 211, 211);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.SearchButton.setObjectName("SearchButton")
        self.gridLayout.addWidget(self.SearchButton, 2, 1, 1, 1)
        self.SearchBar = QLineEdit(self.centralwidget)
        self.SearchBar.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.SearchBar.setObjectName("SearchBar")
        self.gridLayout.addWidget(self.SearchBar, 2, 2, 1, 1)
        self.InventoryDisplay = QTableWidget(self.centralwidget)
        font = QFont()
        font.setBold(False)
        font.setWeight(50)
        self.InventoryDisplay.setFont(font)
        self.InventoryDisplay.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"")
        # self.InventoryDisplay.setObjectName("InventoryDisplay")
        # self.InventoryDisplay.setColumnCount(11)
        # self.InventoryDisplay.setRowCount(0)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(0, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(1, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(2, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(3, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(4, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(5, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(6, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(7, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(8, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(9, item)
        # item = QTableWidgetItem()
        # self.InventoryDisplay.setHorizontalHeaderItem(10, item)
        # self.gridLayout.addWidget(self.InventoryDisplay, 4, 1, 1, 2)
        # self.setCentralWidget(self.centralwidget)
        # self.statusbar = self.statusBar()
        # self.statusbar.setObjectName("statusbar")
        #self.setStatusBar(self.statusbar)
 
#----------------------------------------------------------------------------------------------------
        #Connect to Database
        self.db = QSqlDatabase.addDatabase('QSQLITE')
        self.db.setDatabaseName('inventory.db')
        self.model = QSqlTableModel()
        self.delrow = -1
        self.initializeModel()
 
        self.sbar = self.statusBar()
 
        self.InventoryDisplay = QTableView()
        self.InventoryDisplay.setModel(self.model)
        self.InventoryDisplay.clicked.connect(self.findrow)
        self.InventoryDisplay.selectionModel().selectionChanged.connect(self.getCellText)
 
        # self.layout = QGridLayout()
        # self.layout.addWidget(self.InventoryDisplay, 0, 0, 1, 3)
        self.gridLayout.addWidget(self.InventoryDisplay, 4, 1, 1, 2)
        self.setCentralWidget(self.centralwidget)
          
        # button = QPushButton("add a row")
        # button.setFixedWidth(btnWidth)
        # button.clicked.connect(self.addrow)
        # self.layout.addWidget(button, 1, 0)
          
        # btn1 = QPushButton("delete a row")
        # btn1.setFixedWidth(btnWidth)
        # btn1.clicked.connect(self.delete_row)
        # self.layout.addWidget(btn1,1, 1)
  
        # self.myWidget = QWidget()
        # self.myWidget.setLayout(self.layout)
    
        # self.setCentralWidget(self.myWidget)
        # self.setGeometry(0,0,800,550)
        # self.setWindowTitle("Database Demo")
        # self.sbar.showMessage("Ready")
         
        self.retranslateUi(self)
        QMetaObject.connectSlotsByName(self)
  
    def initializeModel(self):
       self.model.setTable('items')
       self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
       self.model.select()
       #self.model.setHeaderData(0, Qt.Horizontal, "Index")
       #self.model.setHeaderData(1, Qt.Horizontal, "Name")
       #self.model.setHeaderData(2, Qt.Horizontal, "Quantity")
       #self.model.setHeaderData(3, Qt.Horizontal, "Price")
       #self.model.setHeaderData(4, Qt.Horizontal, "Sell_Price")
       #self.model.setHeaderData(5, Qt.Horizontal, "Description")
       #self.model.setHeaderData(6, Qt.Horizontal, "Category")
       #self.model.setHeaderData(7, Qt.Horizontal, "Location")
       #self.model.setHeaderData(8, Qt.Horizontal, "Length_Ft ")
       #self.model.setHeaderData(9, Qt.Horizontal, "Barcode ")
       #self.model.setHeaderData(10, Qt.Horizontal, "Image ")
       #self.model.setHeaderData(11, Qt.Horizontal, "Date ")      
          
    def addrow(self):
        print(self.model.rowCount())
        ret = self.model.insertRows(self.model.rowCount(), 1)
        print(ret)
          
    def delete_row(self):
        row = self.InventoryDisplay.currentIndex().row()
        self.model.removeRow(row)
        self.initializeModel()
        self.InventoryDisplay.selectRow(row)
          
    def findrow(self, i):
        self.delrow = i.row()
  
    def getCellText(self):
        if self.InventoryDisplay.selectedIndexes():
            item = self.InventoryDisplay.selectedIndexes()[0]
            row = self.selectedRow()
            column = self.selectedColumn()
            if not item == None:
                name = item.data()
                self.sbar.showMessage(str(name))
  
    def selectedRow(self):
        if self.InventoryDisplay.selectionModel().hasSelection():
            row =  self.InventoryDisplay.selectionModel().selectedIndexes()[0].row()
            return int(row)
  
    def selectedColumn(self):
        column =  self.InventoryDisplay.selectionModel().selectedIndexes()[0].column()
        return int(column)
#----------------------------------------------------------------------------------------------------
 
        #Display the inventory
        #self.DisplayInventory()
 
#----------------------------------------------------------------------------------------------------
#                                 Display Inventory
#----------------------------------------------------------------------------------------------------
 
    def DisplayInventory(self):
        connection = sqlite3.connect(MainDatabase)
        cursor = connection.cursor()
        cursor.execute('SELECT * FROM items')
        row = 0
        self.InventoryDisplay.setRowCount(40)
        while True:
                form = cursor.fetchone()
                if form == None:
                        break
                for column, item in enumerate(form):
                        self.InventoryDisplay.setItem(row, column, QTableWidgetItem(str(item)))
                print(str(item))
                row += 1
        connection.close()
#----------------------------------------------------------------------------------------------------
 
   
    def retranslateUi(self, MainDisplay):
        _translate = QCoreApplication.translate
        self.setWindowTitle(_translate("MainDisplay", "AdminMenu"))
        self.AddItemButton.setText(_translate("MainDisplay", "Add Item"))
        self.DeleteItemButton.setText(_translate("MainDisplay", "Delete Item"))
        self.CheckoutButton.setText(_translate("MainDisplay", "Check Out"))
        self.ReturnButton.setText(_translate("MainDisplay", "Return"))
        self.ScanBarcodeButton.setText(_translate("MainDisplay", "Scan Barcode"))
        self.MoreInfoButton.setText(_translate("MainDisplay", "More Info"))
        self.RefreshButton.setText(_translate("MainDisplay", "Refresh"))
        self.LogoutButton.setText(_translate("MainDisplay", "Log Out"))
        self.Header.setText(_translate("MainDisplay", "Admin Menu"))
        self.SearchButton.setText(_translate("MainDisplay", "Search"))
        # item = self.InventoryDisplay.horizontalHeaderItem(0)
        # item.setText(_translate("MainDisplay", "ID"))
        # item = self.InventoryDisplay.horizontalHeaderItem(1)
        # item.setText(_translate("MainDisplay", "Name"))
        # item = self.InventoryDisplay.horizontalHeaderItem(2)
        # item.setText(_translate("MainDisplay", "Quantity"))
        # item = self.InventoryDisplay.horizontalHeaderItem(3)
        # item.setText(_translate("MainDisplay", "Price ($)"))
        # item = self.InventoryDisplay.horizontalHeaderItem(4)
        # item.setText(_translate("MainDisplay", "Sell Price ($)"))
        # item = self.InventoryDisplay.horizontalHeaderItem(5)
        # item.setText(_translate("MainDisplay", "Description"))
        # item = self.InventoryDisplay.horizontalHeaderItem(6)
        # item.setText(_translate("MainDisplay", "Category"))
        # item = self.InventoryDisplay.horizontalHeaderItem(7)
        # item.setText(_translate("MainDisplay", "Location"))
        # item = self.InventoryDisplay.horizontalHeaderItem(8)
        # item.setText(_translate("MainDisplay", "Length (Ft)"))
        # item = self.InventoryDisplay.horizontalHeaderItem(9)
        # item.setText(_translate("MainDisplay", "Barcode #"))
        # item = self.InventoryDisplay.horizontalHeaderItem(10)
        # item.setText(_translate("MainDisplay", "Date Updated"))
 
#----------------------------------------------------------------------------------------------------
#                                       Button Actions
#----------------------------------------------------------------------------------------------------
        #------------------------------------------
                        #Logout Button
        #------------------------------------------
        #When the Logout button is clicked -> LogoutClicked Function
        LogoutButton = self.LogoutButton
        LogoutButton.clicked.connect(self.LogoutClicked)
        #------------------------------------------
        #------------------------------------------
                        #Add Item Button
        #------------------------------------------
        #When the AddItem button is clicked -> AddItem Function
        AddItemButton = self.AddItemButton
        AddItemButton.clicked.connect(self.AddItemClicked)
        #------------------------------------------
        #------------------------------------------
                     #Remove Item Button
        #------------------------------------------
        #When the RemoveItem button is clicked -> RemoveItem Function
        RemoveItemButton = self.DeleteItemButton
        RemoveItemButton.clicked.connect(self.RemoveItemClicked)
        #------------------------------------------
        #------------------------------------------
                     #Checkout Item Button
        #------------------------------------------
        #When the Checkout button is clicked -> Checkout Function
        CheckoutButton = self.CheckoutButton
        CheckoutButton.clicked.connect(self.CheckoutClicked)
        #------------------------------------------
        #------------------------------------------
                     #Return Item Button
        #------------------------------------------
        #When the Return button is clicked -> Return Function
        ReturnButton = self.ReturnButton
        ReturnButton.clicked.connect(self.ReturnClicked)
        #------------------------------------------
        #------------------------------------------
                     #Scan Barcode Button
        #------------------------------------------
        #When the Scan Barcode button is clicked -> ScanBarcode Function
        ScanBarcodeButton = self.ScanBarcodeButton
        ScanBarcodeButton.clicked.connect(self.ScanBarcodeClicked)
        #------------------------------------------
        #------------------------------------------
                     #More Info Button
        #------------------------------------------
        #When the More Info button is clicked -> MoreInfo Function
        MoreInfoButton = self.MoreInfoButton
        MoreInfoButton.clicked.connect(self.MoreInfoClicked)
        #------------------------------------------
        #------------------------------------------
                     #Refresh Button
        #------------------------------------------
        #When the More Info button is clicked -> MoreInfo Function
        RefreshButton = self.RefreshButton
        RefreshButton.clicked.connect(self.RefreshClicked)
        #------------------------------------------
        #------------------------------------------
                     #Search Button
        #------------------------------------------
        #When the More Info button is clicked -> MoreInfo Function
        SearchButton = self.SearchButton
        SearchButton.clicked.connect(self.SearchClicked)
        #------------------------------------------
#----------------------------------
    #Logout Function
    def LogoutClicked(self):
        #Print in terminal for testing:
        print("The Logout Button was clicked")
        #Switch from this screen to the LoginScreen
        #(Import LoginScreen here to prevent circular import error)
        from LoginScreen import Ui_Loginscreen
        self.win = Ui_Loginscreen() #Define LoginScreen
        self.win.show() #Show Login Screen
        self.close() #Close this screen (AdminMenu)
#----------------------------------
#----------------------------------
    #AddItem Function
    def AddItemClicked(self):
        #Print in terminal for testing:
        print("The Add Item Button was clicked")
        #Switch from this screen to the AddItems Screen (Scene Swap):
        self.win = Ui_AddItemMenu()
        self.win.show()
        self.close()
#----------------------------------
#----------------------------------
    #RemoveItem Function
    def RemoveItemClicked(self):
        #Print in terminal for testing:
        print("The Delete Item Button was clicked")
 
        #Delete Items from the Inventory
        index = self.InventoryDisplay.currentIndex()
        NewIndex = self.InventoryDisplay.model().index(index.row(), 0)
        indexID = self.InventoryDisplay.model().data(NewIndex)
        print(indexID)
 
        connection = sqlite3.connect("inventory.db")
        cursor = connection.cursor()
        cursor.execute("DELETE FROM items WHERE id = ?", (indexID,))
        connection.commit()
        connection.close()
#----------------------------------
#----------------------------------
    #Checkout Function
    def CheckoutClicked(self):
        #Print in terminal for testing:
        print("The Checkout Button was clicked")
        #Switch from this screen to the AddItems Screen (Scene Swap):
        self.win = spindemo()
        self.win.show()
        # self.close()
        print()
#----------------------------------
#----------------------------------
    #Return Function
    def ReturnClicked(self):
        #Print in terminal for testing:
        print("The Return Button was clicked")
#----------------------------------
#----------------------------------
    #ScanBarcode Function
    def ScanBarcodeClicked(self):
        #Print in terminal for testing:
        print("The Scan Barcode Button was clicked")
#----------------------------------
#----------------------------------
    #MoreInfo Function
    def MoreInfoClicked(self):
        #Print in terminal for testing:
        print("The More Info Button was clicked")
#----------------------------------
#----------------------------------
    #Refresh Function
    def RefreshClicked(self):
        #Print in terminal for testing:
        print("The Refresh Button was clicked")
        #Close and reopen the app (Refresh)
        self.win = Ui_MainDisplay()
        self.win.show()
        self.close()
#----------------------------------
#----------------------------------
    #Search Function
    def SearchClicked(self):
        #Print in terminal for testing:
        print("The Search Button was clicked")
#----------------------------------
#----------------------------------------------------------------------------------------------------
 
  
if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Ui_MainDisplay()
    win.show()
 
    sys.exit(app.exec_())
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020