-
Notifications
You must be signed in to change notification settings - Fork 0
/
kho.py
242 lines (196 loc) · 9.18 KB
/
kho.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
from PySide6 import QtGui,QtCore,QtWidgets
import sys
from bson.objectid import ObjectId
import pymongo
from pymongo import MongoClient
client = pymongo.MongoClient("mongodb+srv://hoangthong:hoangthong16@onmyojibot.fgbvh.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
db = client["QuanLyKhoHang"]
collection= db["Kho"]
class Kho(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.w = 1500
self.h = 600
self.setMinimumSize(self.w, self.h)
self.r = 0
self.label_title = ["Mã Kho", "Tên Kho", "Địa chỉ"]
self.setContentsMargins(10, 0, 10, 10)
# layout chính
layout = QtWidgets.QVBoxLayout()
self.setLayout(layout)
# Bảng dữ liệu
self.tableWidget = QtWidgets.QTableWidget(50, 3)
self.tableWidget.setMinimumSize(600,300)
self.tableWidget.setStyleSheet("font-size: 14px")
#self.tableWidget.setSortingEnabled(True)
# set tiêu đề cột
self.tableWidget.setHorizontalHeaderLabels(self.label_title)
# Kéo độ dài của cột đến cuối
self.tableWidget.horizontalHeader().setStretchLastSection(True)
# Chia đều độ dài của cột
self.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
# Tiêu đề bảng
self.lb_title = QtWidgets.QLabel("Kho")
self.lb_title.setStyleSheet("color: blue;"
"font: bold 24px;"
"margin-bottom: 30px")
self.lb_title.setAlignment(QtCore.Qt.AlignCenter)
# Layout tìm kiếm
self.layout_timkiem = QtWidgets.QGridLayout()
self.layout_timkiem.setContentsMargins(0, 10, 0, 20)
# Thanh tìm kiếm
self.le_thanhtimkiem = QtWidgets.QLineEdit()
self.le_thanhtimkiem.setStyleSheet("width: 100px;"
"height: 25px;"
"font-size: 14px")
# ComboBox Tìm kiếm theo
self.cbb_timkiemtheo = QtWidgets.QComboBox()
self.cbb_timkiemtheo.setStyleSheet( "height: 25px;"
"font-size: 14px")
self.cbb_timkiemtheo.setMinimumContentsLength(20)
self.cbb_timkiemtheo.addItems(self.label_title)
self.cbb_timkiemtheo.setCurrentIndex(1)
# buttom tìm kiếm
self.btn_timkiem = QtWidgets.QPushButton("Tìm kiếm")
self.btn_timkiem.setIcon(QtGui.QIcon("image\\search.png"))
self.btn_timkiem.setStyleSheet("font-size: 14px")
# button refresh
self.btn_refresh =QtWidgets.QPushButton("Refresh")
self.btn_refresh.setIcon(QtGui.QIcon("image\\refresh.png"))
self.btn_refresh.setStyleSheet("font-size: 14px")
# Chèn Item vào layout tìm kiếm
self.layout_timkiem.addWidget(self.le_thanhtimkiem, 0, 0)
self.layout_timkiem.addWidget(self.cbb_timkiemtheo, 0, 1)
self.layout_timkiem.addWidget(self.btn_timkiem, 0, 2)
self.layout_timkiem.addWidget(self.btn_refresh,0,3)
self.layout_thaotac = QtWidgets.QGridLayout()
# Label Mã Kho
self.lb_ma = QtWidgets.QLabel("Mã Kho")
self.lb_ma.setStyleSheet("font-size: 14px")
# Label Tên Kho
self.lb_ten = QtWidgets.QLabel("Tên Kho")
self.lb_ten.setStyleSheet("font-size: 14px")
# Label Địa chỉ
self.lb_diachi = QtWidgets.QLabel("Địa Chỉ")
self.lb_diachi.setStyleSheet("font-size: 14px")
# LineEdit Mã Kho
self.le_ma = QtWidgets.QLineEdit()
self.le_ma.setEnabled(False)
self.le_ma.setStyleSheet("width: 100px;"
"height: 25px;"
"font-size: 14px")
# LineEdit Tên Kho
self.le_ten = QtWidgets.QLineEdit()
self.le_ten.setStyleSheet("width: 100px;"
"height: 25px;"
"font-size: 14px")
# LineEdit Địa chỉ
self.le_diachi = QtWidgets.QLineEdit()
self.le_diachi.setStyleSheet("width: 100px;"
"height: 25px;"
"font-size: 14px")
# button Thêm
self.btn_them = QtWidgets.QPushButton("Thêm")
self.btn_them.setIcon(QtGui.QIcon("image\\plus.png"))
self.btn_them.setStyleSheet("width: 150px;"
"height: 30px;"
"margin-left: 100px;"
"font-size: 14px")
# button Sửa
self.btn_sua = QtWidgets.QPushButton("Sửa")
self.btn_sua.setIcon(QtGui.QIcon("image\\loop.png"))
self.btn_sua.setStyleSheet("width: 150px;"
"height: 30px;"
"margin-left: 100px;"
"font-size: 14px")
# button Xoá
self.btn_xoa = QtWidgets.QPushButton("Xoá")
self.btn_xoa.setIcon(QtGui.QIcon("image\\remove.png"))
self.btn_xoa.setStyleSheet("width: 150px;"
"height: 30px;"
"margin-left: 100px;"
"font-size: 14px"
)
# chèn Label vào layout_thaotac
self.layout_thaotac.addWidget(self.lb_ma, 0, 0)
self.layout_thaotac.addWidget(self.lb_ten, 1, 0)
self.layout_thaotac.addWidget(self.lb_diachi, 2, 0)
# chèn LineEdit vào layout_thaotac
self.layout_thaotac.addWidget(self.le_ma, 0, 1)
self.layout_thaotac.addWidget(self.le_ten, 1, 1)
self.layout_thaotac.addWidget(self.le_diachi, 2, 1)
# chèn Button vào layout_thaotac
self.layout_thaotac.addWidget(self.btn_them, 0, 2)
self.layout_thaotac.addWidget(self.btn_sua,1,2)
self.layout_thaotac.addWidget(self.btn_xoa, 2, 2)
# Chèn các layout vào layout chính
layout.addWidget(self.lb_title)
layout.addLayout(self.layout_timkiem)
layout.addWidget(self.tableWidget)
layout.addLayout(self.layout_thaotac)
# Hiện thị dữ liệu
self.HienThi()
# signal
self.btn_them.clicked.connect(self.Them)
self.btn_sua.clicked.connect(self.Sua)
self.btn_xoa.clicked.connect(self.Xoa)
self.tableWidget.clicked.connect(self.SelectedItem)
self.btn_timkiem.clicked.connect(self.TimKiem)
self.btn_refresh.clicked.connect(self.HienThi)
def HienThi(self):
"""Hiện thị dữ liệu"""
self.tableWidget.clear()
self.tableWidget.setHorizontalHeaderLabels(self.label_title)
loaddata = collection.find()
item_count = loaddata.count()
if item_count >50:
self.tableWidget.setRowCount(item_count)
i = 0
for data in loaddata:
self.tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(str(data["_id"])))
self.tableWidget.setItem(i, 1, QtWidgets.QTableWidgetItem(data["tenkho"]))
self.tableWidget.setItem(i, 2, QtWidgets.QTableWidgetItem(data["diachi"]))
i = i + 1
def Them(self):
"""Thêm dữ liệu"""
data = {"tenkho": self.le_ten.text(), "diachi":self.le_diachi.text()}
collection.insert_one(data)
self.HienThi()
def Sua(self):
"""Sửa dữ liệu"""
if self.le_ma.text():
collection.update_one({"_id": ObjectId(self.le_ma.text())},{"$set":{"tenkho":self.le_ten.text(), "diachi":self.le_diachi.text()}})
self.HienThi()
else:
QtWidgets.QMessageBox.critical(self,"Thông báo","Không thể sửa khi thiếu mã")
def Xoa(self):
"""Xoá dữ liệu"""
collection.delete_one({"_id":ObjectId(self.le_ma.text())})
self.HienThi()
def SelectedItem(self, event):
"""Hiện thị dữ liệu ở ô đang chọn trong LineEdit"""
self.r = event.row()
try:
self.le_ma.setText(self.tableWidget.item(self.r, 0).text())
self.le_ten.setText(self.tableWidget.item(self.r, 1).text())
self.le_diachi.setText(self.tableWidget.item(self.r,2).text())
except:
self.le_ma.setText("")
self.le_ten.setText("")
self.le_diachi.setText("")
def TimKiem(self):
"""Tìm kiếm dữ liệu"""
self.tableWidget.clear()
self.tableWidget.setHorizontalHeaderLabels(self.label_title)
if self.cbb_timkiemtheo.currentIndex() == 0:
loaddata = collection.find({"_id":ObjectId(self.le_thanhtimkiem.text())})
elif self.cbb_timkiemtheo.currentIndex() == 1:
loaddata = collection.find({"tenkho": self.le_thanhtimkiem.text()})
else:
loaddata = collection.find({"diachi": self.le_thanhtimkiem.text()})
i = 0
for data in loaddata:
self.tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(str(data["_id"])))
self.tableWidget.setItem(i, 1, QtWidgets.QTableWidgetItem(data["tenkho"]))
self.tableWidget.setItem(i, 2, QtWidgets.QTableWidgetItem(data["diachi"]))
i = i + 1