-
Notifications
You must be signed in to change notification settings - Fork 1
/
sub2.py
290 lines (250 loc) · 11.2 KB
/
sub2.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
# -*- coding: utf-8 -*-
from random import choice
from PySide6.QtCore import (QCoreApplication, QMetaObject, QRect, Qt)
from PySide6.QtGui import (QFont)
from PySide6.QtWidgets import (QLabel, QListWidget, QWidget, QVBoxLayout, QLineEdit, QPushButton, QSpacerItem,
QSizePolicy)
import global1
################################################################################
## Form generated from reading UI file 'designerxaOMVi.ui'
##
## Created by: Qt User Interface Compiler version 6.5.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
class Ui_eating(object):
def setupUi(self, eating):
if not eating.objectName():
eating.setObjectName(u"eating")
eating.resize(532, 695)
self.listWidget = QListWidget(eating)
self.listWidget.setObjectName(u"listWidget")
self.listWidget.setGeometry(QRect(130, 100, 256, 451))
font = QFont()
font.setFamilies([u"\u5b8b\u4f53"])
font.setBold(True)
font.setPointSize(12)
self.listWidget.setFont(font)
self.label = QLabel(eating)
self.label.setObjectName(u"label")
self.label.setGeometry(10, 15, 500, 100)
self.label.setMaximumWidth(1000)
# 将标签水平居中对齐
self.label.setAlignment(Qt.AlignCenter)
font1 = QFont()
font1.setPointSize(22)
self.label.setFont(font1)
self.retranslateUi(eating)
QMetaObject.connectSlotsByName(eating)
# setupUi
def retranslateUi(self, eating):
eating.setWindowTitle(QCoreApplication.translate("eating", u"Form", None))
self.label.setText(QCoreApplication.translate("eating", u"\u98df\u54c1\u5217\u8868", None))
# retranslateUi
class sub2(QWidget, Ui_eating):
def __init__(self, parent, neme, count=True):
super().__init__()
self.neme = neme
self.parent = parent
self.setupUi(self)
self.label.setText("食品列表")
self.setWindowTitle(neme + "吃点什么好呢")
self.food = []
self.ope()
self.load()
self.listWidget.itemClicked.connect(
lambda item: self.open_new_window(item.text(), item.text().split()[0],
item.text().split()[2].split('¥')[1], item.text().split()[4],
item.text().split()[6], item.text().split()[8]))
self.layout = QVBoxLayout(self)
spacer_item = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.layout.addSpacerItem(spacer_item)
# 创建输入框
self.input_edit = QLineEdit(self)
self.input_edit.setPlaceholderText("输入关键词就能查找啦\U0001F600")
self.layout.addWidget(self.input_edit)
# 创建按钮
self.button1 = QPushButton("查找", self)
self.button1.clicked.connect(self.sortByKeyword)
self.layout.addWidget(self.button1, alignment=Qt.Alignment(Qt.AlignRight | Qt.AlignBottom))
self.button2 = QPushButton("价格排序", self)
self.button2.clicked.connect(self.sortByCost)
self.layout.addWidget(self.button2, alignment=Qt.Alignment(Qt.AlignRight | Qt.AlignBottom))
self.button3 = QPushButton("返回", self)
self.button3.clicked.connect(self.close)
self.layout.addWidget(self.button3, alignment=Qt.Alignment(Qt.AlignRight | Qt.AlignBottom))
self.setLayout(self.layout)
self.count = 0
self.show()
def load(self):
for da in self.data:
self.listWidget.addItem(
"{} - ¥{} - {} - {} - {}".format(da['菜名'], da['价格'], da['类别'], da['食堂'], da['档口']))
def ope(self):
import dbconnect
self.data = dbconnect.findAllFood()
def sortByCost(self):
sorted_items = sorted(self.data, key=lambda x: float(x['价格']), reverse=bool(self.count & 1))
self.listWidget.clear()
# 根据排序结果重新创建标签
for dish in sorted_items:
label = "{} - ¥{} - {}".format(dish['菜名'], dish['价格'], dish['类别'])
self.listWidget.addItem(label)
self.count += 1
def sortByKeyword(self):
keyword = self.input_edit.text()
# 计算关键词与每个条目的相关度
scores = {}
for item in self.data:
score = self.calculateScore(item, keyword)
scores[item['菜名']] = score
# 根据相关度对条目排序
sorted_items = sorted(self.data, key=lambda x: scores[x['菜名']], reverse=True)
self.listWidget.clear()
# 根据排序结果重新创建标签
for dish in sorted_items:
label = "{} - ¥{} - {}".format(dish['菜名'], dish['价格'], dish['类别'])
self.listWidget.addItem(label)
def calculateScore(self, item, keyword):
# 实现自定义的相关度计算逻辑
# 这里可以使用各种方法来计算关键词和条目之间的相关度得分
# 返回得分值,得分越高表示相关度越高
pric = float(item['价格'])
if keyword in item['档口'] or keyword in item['食堂']:
return 20 + pric
if keyword in item['菜名']:
return 15 + pric
elif keyword in item['类别']:
return 12 + pric
elif keyword in item['时间']:
return 20 + pric
elif keyword in str(item['价格']):
return 10
else:
l1 = self.longest_common_substring(keyword, item['菜名'])
l2 = self.longest_common_substring(keyword, item['类别'])
l3 = self.longest_common_substring(keyword, str(item['价格']))
l4 = self.longest_common_substring(keyword, item['时间'])
l5 = self.longest_common_substring(keyword, item['档口'])
l6 = self.longest_common_substring(keyword, item['食堂'])
return max(l1, l2, l3, l4, l5, l6)
def longest_common_substring(self, str1, str2):
# 创建一个二维数组来记录最长公共子串的长度
dp = [[0] * (len(str2) + 1) for _ in range(len(str1) + 1)]
max_length = 0 # 最长公共子串长度
for i in range(1, len(str1) + 1):
for j in range(1, len(str2) + 1):
if str1[i - 1] == str2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
max_length = max(max_length, dp[i][j])
return max_length
def open_new_window(self, item, food, cost, kind, count, house):
# 获取所选项的文本
selected_item_text = item
global new_window
# 创建新窗口并显示
new_window = NewWindow(selected_item_text, food, cost, self.neme, kind, count, house)
new_window.show()
def closeEvent(self, event):
self.parent.show() # 关闭子窗口时显示母窗口
event.accept()
class sub22(sub2):
def __init__(self, parent, uname, tName):
ran = choice(['大酬宾', '促销', '正在营业'])
if ran != '正在营业':
self.count = True
dic = {0: '学二', 3: '合一', 5: '新北', 2: '美食苑', 4: 'wings'}
self.uname = uname
self.tName = dic[tName]
super().__init__(parent, uname, False)
self.label.setText(self.tName + '食堂' + ran)
self.setWindowTitle(uname + "走了20min路终于到" + self.tName + "食堂了。。吃点什么好呢")
self.parent = parent
self.resize(532, 780)
self.button4 = QPushButton("切换视图", self)
self.button4.clicked.connect(self.inin)
self.layout.addWidget(self.button4, alignment=Qt.Alignment(Qt.AlignRight | Qt.AlignBottom))
def ope(self):
from dbconnect import getplacefood
self.data = getplacefood(self.tName)
def inin(self):
from subdang import subdang
self.subdang = subdang(parent=self, tname=self.tName, uname=self.uname, dataspe=self.data)
self.subdang.show()
self.hide()
class sub23(sub2):
def __init__(self, parent, uname, category):
self.uname = uname
self.kind = category
super().__init__(parent, uname, False)
self.label.setText(category + '类 菜单')
self.setWindowTitle(uname + "想看看" + category + "种类的食物")
self.parent = parent
def ope(self):
from dbconnect import getkindfood
self.data = getkindfood(self.kind)
class sub24(sub2):
def __init__(self, uname, data, dangname):
self.data = data
self.uname = uname
super().__init__(None, uname)
self.setWindowTitle(dangname)
self.button3.hide()
def ope(self):
pass
def closeEvent(self, event):
event.accept()
def reload(self, data, dang):
self.listWidget.clear()
self.data = data
self.load()
self.setWindowTitle(dang)
class NewWindow(QWidget):
def __init__(self, item_text, foodname, cost, username, kind, housename, countname):
self.kind = kind
self.username = username
self.foodname = foodname
self.countname = countname
self.housename = housename
self.cost = cost
font = QFont()
font.setBold(True) # 设置字体为粗体
font.setPointSize(12) # 设置字体大小
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowMinimizeButtonHint)
self.setWindowTitle("真的好吃的")
self.setGeometry(700, 170, 300, 600)
layout = QVBoxLayout()
label = QLabel(item_text)
layout.addWidget(label)
self.button = QPushButton("进食", self)
self.button.setFont(font) # 设置按钮字体
layout.addWidget(self.button)
self.button.clicked.connect(self.eat)
self.button = QPushButton("收藏", self)
self.button.setFont(font) # 设置按钮字体
self.button.clicked.connect(self.star)
layout.addWidget(self.button)
self.back_button = QPushButton("返回", self)
self.back_button.setFont(font) # 设置按钮字体
self.back_button.clicked.connect(self.close)
layout.addWidget(self.back_button)
from submit import wid
self.subwid = wid(username, foodname, countname, housename)
self.subwid.setFont(font)
layout.addWidget(self.subwid)
self.setLayout(layout)
def star(self):
name = self.username
from dbconnect import addstar
addstar(name, self.foodname)
def eat(self):
name = self.username
from dbconnect import eatchange, addcost
eatchange(self.housename, self.countname, self.foodname)
addcost(name, self.cost)
from dbconnect import lastchange
lastchange(name, self.kind)
def close(self):
self.subwid.save_comments()
super().close()