-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
207 lines (155 loc) · 8.08 KB
/
main.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
import sys
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QSize, QDate, QPropertyAnimation, QRect, Qt
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QMainWindow,QCompleter,QAction,QLineEdit
from query import Ui_MainWindow
from tool import Utility
from myCalendar import MyCalendar
from stationCodes import StationCodes
bottomSpace = 30 #日历控件距离底部间距
calHeight = 240 #日历高度
checkBoxQSS = '''
QCheckBox{color: white}
QCheckBox::indicator {width: 20px; height: 20px}
QCheckBox::indicator:unchecked {image:url(Pictures/unselect.png)}
QCheckBox::indicator:checked {image:url(Pictures/selected.png)}
'''
class CompleterDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(CompleterDelegate, self).initStyleOption(option, index)
option.backgroundBrush = QtGui.QColor('white')
option.palette.setBrush(QtGui.QPalette.Text, QtGui.QColor('black'))
option.displayAlignment = Qt.AlignLeft
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.initUI()
def initUI(self):
self.setupUi(self)
self.setupCSSStyle()
self.utility = Utility() #实例化工具类
# self.setFixedSize(QSize(self.width(),self.height())) #禁止拉伸窗口大小
self.winHeight = self.height() # 获取窗体高度,便用
self.setWindowIcon(QIcon('Pictures/train.png')) #设置图标
self.logoLabel.resize(150,150)
self.logoLabel.setScaledContents(True) #图片填满label
self.logoLabel.setPixmap(QPixmap('Pictures/train3.png')) #设置label上的图片
self.exchangeButton.setIcon(QIcon('Pictures/exchange.png')) # 设置Icon
self.exchangeButton.setIconSize(QSize(50, 50)) # 设置交换按钮icon size
self.exchangeButton.clicked.connect(self.exchangePlace)
self.timeButton.setText(self.utility.getDepartureDate()) # 设置出发日期
self.timeButton.setIcon(QIcon('Pictures/calendar.png')) # 设置Icon
self.timeButton.clicked[bool].connect(self.selectDate)
self.isTimeButtonPressed = False #按钮点初始状态
self.studentCheckBox.stateChanged.connect(lambda: self.iSBoxChecked(self.studentCheckBox))
self.highSpeedCheckBox.stateChanged.connect(lambda: self.iSBoxChecked(self.highSpeedCheckBox))
self.stations = StationCodes().getStations()
self.setupLineEdit()
self.initCalendar()
def initCalendar(self):
x = self.timeButton.x() # 获取时间按钮的坐标x
y = self.timeButton.y() + self.timeButton.height()
w = self.timeButton.width()
h = calHeight
self.cal = MyCalendar(self)
self.cal.setGeometry(x, y, w, h)
self.cal.clicked[QDate].connect(self.showDate) # clicked[参数],即定义showDate是传入的参数类型设置
self.cal.close()
def setupLineEdit(self):
# 增加自动补全
self.completer = QCompleter(self.stations)
self.completer.setFilterMode(Qt.MatchStartsWith) # 起始位置
delegate = CompleterDelegate(self)
self.completer.popup().setStyleSheet("QScrollBar{background:skyBlue;}")
self.completer.popup().setItemDelegate(delegate)
self.setupEditIcon(self.startLineEdit,'始发地')
self.setupEditIcon(self.destinationLineEdit,'目的地')
# def selectStation(self):
#
# self.placeLists = QListWidget(self)
# self.placeLists.setGeometry(QRect(self.startButton.x(),self.startButton.y()+self.startButton.height(),self.startButton.width(),self.height()-self.startButton.y()-self.startButton.height()))
# self.placeLists.addItems(self.stations)
#
# self.placeLists.setSortingEnabled(True)
# # self.placeLists.sortItems() # 排序
# self.placeLists.setCurrentRow(0)
# self.placeLists.setStyleSheet("QListWidget{color:black; background:white}"
# "QListWidget::Item{padding-top:2.5px; padding-bottom:2.5px; }"
# "QListWidget::Item:hover{background:skyblue; }" #下拉滑动背景色
# "QListWidget::item:selected{ color:red; background:skyblue;}" #当前被选择的item背景,颜色
# "QListWidget::item:selected:!active{ background:skyblue; }")
#
# self.placeLists.currentItemChanged.connect(self.selectPlaceItem)
#
# self.placeLists.show()
#
#
# def selectPlaceItem(self):
#
# self.startButton.setText(self.placeLists.currentItem().text())
# self.placeLists.close()
def setupEditIcon(self,lineEdit,placeHolderText):
if lineEdit==self.startLineEdit:
iconPath = 'Pictures/start.png'
else:
iconPath = 'Pictures/destination.png'
lineEdit.setCompleter(self.completer)
lineEdit.setPlaceholderText(placeHolderText)
action = QAction(lineEdit)
action.setIcon(QIcon(iconPath))
lineEdit.addAction(action, QLineEdit.LeadingPosition)
def selectDate(self,pressed):
if self.isTimeButtonPressed == False:
self.cal.show()
self.startRect = QRect(self.geometry().x(), self.geometry().y(), self.width(), self.height())
self.endRect = QRect(self.geometry().x(), self.geometry().y(), self.width(),self.cal.y() + self.cal.height() + bottomSpace)
self.setFrameAnimation(self.startRect, self.endRect)
self.isTimeButtonPressed = True
else:
self.setFrameAnimation(self.endRect, self.startRect)
self.cal.close()
self.isTimeButtonPressed = False
def setFrameAnimation(self, startRect, endRect):
self.animation = QPropertyAnimation(self, b'geometry')
self.animation.setDuration(250)
self.animation.setStartValue(startRect)
self.animation.setEndValue(endRect)
self.animation.start()
def showDate(self,date):
dateStr = date.toString('yyyy-MM-dd')
dateTime = self.utility.stringToDatetime(dateStr)
self.timeButton.setText(self.utility.getDepartureDate(dateTime)) #"yyyy-MM-dd ddd(星期)"
self.setFrameAnimation(self.endRect, self.startRect)
self.isTimeButtonPressed = False
self.cal.close() # 关闭日期控件
def iSBoxChecked(self,checkBox):
if checkBox.isChecked():
checkBox.setStyleSheet(checkBoxQSS + "QCheckBox{color:#d81e06}")
else:
checkBox.setStyleSheet(checkBoxQSS)
if self.studentCheckBox.isChecked():
self.queryButton.setText('查询学生票')
else:
self.queryButton.setText('查询车票')
def exchangePlace(self):
startPlace = self.startLineEdit.text()
self.startLineEdit.setText(self.destinationLineEdit.text())
self.destinationLineEdit.setText(startPlace)
def setupCSSStyle(self):
# self.setStyleSheet("QMainWindow{background-color:white}")
self.setStyleSheet("QMainWindow{border-image: url(Pictures/bg3.jpg)}") #设置背景图
self.exchangeButton.setStyleSheet("QPushButton{background-color:transparent}")
self.queryButton.setStyleSheet('QPushButton{color:white;background-color:#d81e06;border:1px;border-radius:5px}')
self.timeButton.setStyleSheet('QPushButton{text-align:left;color:white;background-color:transparent;qproperty-iconSize: 25px}')
for lineEdit in (self.startLineEdit,self.destinationLineEdit):
lineEdit.setStyleSheet("QLineEdit{border-width:5px;border-radius:5px;font-size:12pt;padding-left:2px;"
"background-color:transparent;color:white;font-familiy:黑体;font-weight:bold;"
"border: 1px solid lightGray;}")
self.highSpeedCheckBox.setStyleSheet(checkBoxQSS)
self.studentCheckBox.setStyleSheet(checkBoxQSS)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())