def onViewDate(self, MainWindow, clientProgram: ClientProgram): day = self.date_box.text() if day == 'today': state, self.weatherdata = clientProgram.RequestWeatherDataAll( datetime.date.today().strftime('%Y/%m/%d')) else: state, self.weatherdata = clientProgram.RequestWeatherDataAll(day) if state == clientProgram.State.BADCONNECTION or state == clientProgram.State.BADMESSAGE: QtWidgets.QMessageBox.about(MainWindow, "", "Lỗi kết nối tới server") elif state == clientProgram.State.FAILED: QtWidgets.QMessageBox.about(MainWindow, "", "Ngày không hợp lệ.") elif state == clientProgram.State.SUCCEEDED: print(self.weatherdata) while (self.date_table.rowCount() > 1): self.date_table.removeRow(1) for i in range(len(self.weatherdata)): self.date_table.insertRow(i + 1) for j in range(6): item = QtWidgets.QTableWidgetItem() item.setTextAlignment(QtCore.Qt.AlignCenter) font = QtGui.QFont() font.setFamily("Helvetica") item.setFont(font) item.setText(str(self.weatherdata[i][j])) self.date_table.setItem(i + 1, j, item) self.date_table.show()
def onLogin(self, MainWindow, clientProgram:ClientProgram): username = self.username_box.text() password = self.password_box.text() state, error = clientProgram.Login(username, password) if state == ClientProgram.State.SUCCEEDED: QMessageBox.about(MainWindow, "", "Đăng nhập thành công") weatherWindow = WeatherWindow() weatherWindow.setupUI(MainWindow, clientProgram) elif state == ClientProgram.State.FAILED: QMessageBox.about(MainWindow, "Đăng nhập thất bại. Lỗi:", error) else: QMessageBox.about(MainWindow, "","Lỗi kết nối đến server") pass
def onViewCity(self, MainWindow, clientProgram: ClientProgram, usesID=True): city_id = None if usesID: try: city_id = int(self.city_id_box.text()) except: QtWidgets.QMessageBox.about(MainWindow, "", "ID không hợp lệ") return else: city_name = self.city_box.text() duplicate = False city_id = None if self.weatherdata: for data in self.weatherdata: if data[1] == city_name: if not city_id: city_id = data[0] else: duplicate = True if city_id and duplicate: QtWidgets.QMessageBox.about( MainWindow, "", f"Nhiều hơn một thành phố với tên {city_name}. Thành phố sau đây có ID {city_id}. Vui lòng truy vấn bằng ID nếu không đúng." ) if city_id == None: QtWidgets.QMessageBox.about( MainWindow, "", "Sai tên thành phố hoặc không có dữ liệu về thành phố. Vui lòng truy vấn bằng ID." ) else: state, data = clientProgram.RequestWeatherDate7DaysOf(city_id) if state == clientProgram.State.BADCONNECTION or state == clientProgram.State.BADMESSAGE: QtWidgets.QMessageBox.about(MainWindow, "", "Lỗi kết nối tới server") elif state == clientProgram.State.FAILED: QtWidgets.QMessageBox.about(MainWindow, "", "Không tìm thấy thành phố.") elif state == clientProgram.State.SUCCEEDED: _translate = QtCore.QCoreApplication.translate city_name = data[0] self.city_box.setText(_translate("MainWindow", city_name)) self.city_id_box.setText(_translate("MainWindow", str(city_id))) self.viewCity(MainWindow, data)
self.day7_pic = QtWidgets.QLabel(MainWindow) self.day7_pic.setGeometry(QtCore.QRect(760, 210, 101, 81)) self.day7_pic.setText("") self.day7_pic.setScaledContents(True) self.day7_pic.setObjectName("day7_pic") self.temperature7 = QtWidgets.QLabel(MainWindow) self.temperature7.setGeometry(QtCore.QRect(760, 300, 101, 31)) font = QtGui.QFont() font.setFamily("Helvetica") font.setPointSize(14) font.setBold(True) font.setWeight(75) self.temperature7.setFont(font) self.temperature7.setStyleSheet("color: rgb(255, 103, 118)") self.temperature7.setAlignment(QtCore.Qt.AlignCenter) self.temperature7.setObjectName("temperature7") QtCore.QMetaObject.connectSlotsByName(MainWindow) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QWidget() ui = WeatherWindow() clientProgram = ClientProgram() ui.setupUI(MainWindow, clientProgram) MainWindow.show() sys.exit(app.exec_())
buttonLayout.addItem(layout3, 1, 0) buttonLayout.addItem(layout4, 1, 1) tmp = QtWidgets.QWidget(self) tmp.setFixedSize(320, 210) # tmp.setStyleSheet("background-color: rgb(60, 60, 180)") mainLayout = QVBoxLayout(tmp) mainLayout.addWidget(self.function_label) mainLayout.addItem(buttonLayout) self.setLayout(mainLayout) if __name__ == '__main__': from os import environ import sys def suppress_qt_warnings(): environ["QT_DEVICE_PIXEL_RATIO"] = "0" environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1" environ["QT_SCREEN_SCALE_FACTORS"] = "1" environ["QT_SCALE_FACTOR"] = "1" suppress_qt_warnings() app = QtWidgets.QApplication(sys.argv) demo = FunctionUI(None, ClientProgram()) demo.setupUI() demo.show() sys.exit(app.exec_())
def __init__(self): super().__init__() self.clientProgram = ClientProgram()
class ClientUI(QtWidgets.QWidget): def __init__(self): super().__init__() self.clientProgram = ClientProgram() def closeEvent(self, event: QtGui.QCloseEvent) -> None: close = QMessageBox.question(self, "Thoát", ("Kết nối vẫn còn hiệu lực. " if self.clientProgram.connected else "") + "Bạn chắc chắn muốn thoát?", QMessageBox.Yes | QMessageBox.No) if close == QMessageBox.Yes: self.Disconnect() self.close() event.accept() self.close() else: event.ignore() def Connect(self): ip = self.IP_box.text() port = int(self.port_box.text()) state = self.clientProgram.Connect(ip, port) # state = True if state: QMessageBox.about(self, "", "Kết nối đến server thành công") self.functionUI = FunctionUI(self, self.clientProgram) self.functionUI.setupUI() self.functionUI.show() self.hide() else: QMessageBox.about(self, "", "Thao tác thất bại") def Disconnect(self): self.clientProgram.Disconnect() def setupUI(self): self.setObjectName("MainWindow") self.setFixedSize(282, 214) _translate = QtCore.QCoreApplication.translate self.setWindowTitle(_translate("MainWindow", "Client")) # self.background_image = QtWidgets.QLabel(self) # self.background_image.setGeometry(QtCore.QRect(0, 0, 281, 211)) # self.background_image.setStyleSheet("background-color: rgb(238, 255, 238);") # self.background_image.setText("") # self.background_image.setScaledContents(True) # self.background_image.setObjectName("background_image") self.IP_box = QtWidgets.QLineEdit(self) self.IP_box.setGeometry(QtCore.QRect(40, 90, 201, 41)) font = QtGui.QFont() font.setFamily("Helvetica") font.setPointSize(12) self.IP_box.setFont(font) self.IP_box.setStyleSheet("background-color: rgb(246, 252, 255);") self.IP_box.setObjectName("IP_box") self.IP_box.setText(_translate("MainWindow", "127.0.0.1")) self.port_box = QtWidgets.QLineEdit(self) self.port_box.setGeometry(QtCore.QRect(40, 150, 101, 41)) font = QtGui.QFont() font.setFamily("Helvetica") font.setPointSize(12) self.port_box.setFont(font) self.port_box.setStyleSheet("background-color: rgb(246, 252, 255);") self.port_box.setObjectName("port_box") self.port_box.setText(_translate("MainWindow", " 6666")) self.connect_button = QtWidgets.QPushButton( self, clicked=lambda: self.Connect()) self.connect_button.setGeometry(QtCore.QRect(160, 150, 81, 41)) font = QtGui.QFont() font.setFamily("Helvetica") font.setPointSize(11) self.connect_button.setFont(font) self.connect_button.setStyleSheet( "background-color: rgb(224, 237, 255)") self.connect_button.setCheckable(False) self.connect_button.setChecked(False) self.connect_button.setObjectName("connect_button") self.connect_button.setText(_translate("MainWindow", "Kết nối")) self.WELCOME_label = QtWidgets.QLabel(self) self.WELCOME_label.setGeometry(QtCore.QRect(40, 20, 201, 51)) font = QtGui.QFont() font.setFamily("Helvetica") font.setPointSize(22) font.setBold(True) font.setWeight(75) self.WELCOME_label.setFont(font) self.WELCOME_label.setStyleSheet("color: rgb(255, 24, 128);") self.WELCOME_label.setAlignment(QtCore.Qt.AlignCenter) self.WELCOME_label.setObjectName("WELCOME_label") self.WELCOME_label.setText(_translate("MainWindow", "CLIENT")) QtCore.QMetaObject.connectSlotsByName(self)