class EmployeeInfoWindow(QWidget): def init(self, user_id): super().init() self.user_id = user_id self.initUI() def initUI(self): # 连接数据库,获取员工信息 conn = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', database='jj_tset') cursor = conn.cursor() cursor.execute("SELECT * FROM employee_table WHERE user_id='%s'" % self.user_id) result = cursor.fetchone() conn.close() # 创建用于显示员工信息的控件 info_label = QLabel("员工信息", self) info_label.move(100, 100) name_label = QLabel("姓名:" + result[1], self) name_label.move(100, 150) age_label = QLabel("年龄:" + str(result[2]), self) age_label.move(100, 200) # TODO: 创建其他员工信息控件 # 设置窗口大小和标题 self.setFixedSize(800, 500) self.setWindowTitle('员工信息') class LoginWindow(QWidget): def init(self): super().init() self.initUI() def initUI(self): self.setFixedSize(800, 500) self.setWindowTitle('鸡爪称重系统') # 创建一个按钮 button = QPushButton('开始称重', self) button.move(100, 100) button.setGeometry(300, 200, 200, 50) button.clicked.connect(self.showInputDialog) def showInputDialog(self): # 创建一个输入框,获取员工ID text, okPressed = QInputDialog.getText(self, "输入员工ID", "输入员工ID:") if okPressed and text != '': # 连接数据库,检查员工ID是否存在 conn = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', database='jj_tset') cursor = conn.cursor() cursor.execute("SELECT * FROM employee_table WHERE user_id='%s'" % text) result = cursor.fetchone() conn.close() if result: # 员工ID存在,跳转到下一个界面,显示员工信息 print('输入的ID是:', text) employee_info_window = EmployeeInfoWindow(text) employee_info_window.show() self.hide() else: # 员工ID不存在,弹出提示框,提示重新输入 QMessageBox.warning(self, '错误', '员工ID不存在,请重新输入!') if name == 'main': app = QApplication(sys.argv) loginWindow = LoginWindow() loginWindow.show() sys.exit(app.exec_())怎么更改为跳转界面
时间: 2023-12-14 07:39:34 浏览: 78
可以使用QStackedWidget来实现界面跳转,具体实现步骤如下:
1. 在LoginWindow类中添加一个QStackedWidget控件,用于显示不同的界面。
2. 在showInputDialog方法中,如果员工ID存在,就将QStackedWidget控件切换到EmployeeInfoWindow界面。
3. 在EmployeeInfoWindow中添加返回按钮,用于返回到LoginWindow界面。
下面是代码示例:
```
class EmployeeInfoWindow(QWidget):
def __init__(self, user_id, stacked_widget):
super().__init__()
self.user_id = user_id
self.stacked_widget = stacked_widget
self.initUI()
def initUI(self):
# 连接数据库,获取员工信息
conn = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', database='jj_tset')
cursor = conn.cursor()
cursor.execute("SELECT * FROM employee_table WHERE user_id='%s'" % self.user_id)
result = cursor.fetchone()
conn.close()
# 创建用于显示员工信息的控件
info_label = QLabel("员工信息", self)
info_label.move(100, 100)
name_label = QLabel("姓名:" + result[1], self)
name_label.move(100, 150)
age_label = QLabel("年龄:" + str(result[2]), self)
age_label.move(100, 200)
# 创建返回按钮
back_button = QPushButton('返回', self)
back_button.setGeometry(600, 400, 100, 50)
back_button.clicked.connect(self.backToLoginWindow)
# 设置窗口大小和标题
self.setFixedSize(800, 500)
self.setWindowTitle('员工信息')
def backToLoginWindow(self):
self.stacked_widget.setCurrentIndex(0)
class LoginWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setFixedSize(800, 500)
self.setWindowTitle('鸡爪称重系统')
# 创建一个QStackedWidget控件
self.stacked_widget = QStackedWidget(self)
self.stacked_widget.setGeometry(0, 0, 800, 500)
# 创建Login界面
login_widget = QWidget()
self.stacked_widget.addWidget(login_widget)
# 在Login界面上创建一个按钮
button = QPushButton('开始称重', login_widget)
button.move(100, 100)
button.setGeometry(300, 200, 200, 50)
button.clicked.connect(self.showInputDialog)
def showInputDialog(self):
# 创建一个输入框,获取员工ID
text, okPressed = QInputDialog.getText(self, "输入员工ID", "输入员工ID:")
if okPressed and text != '':
# 连接数据库,检查员工ID是否存在
conn = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', database='jj_tset')
cursor = conn.cursor()
cursor.execute("SELECT * FROM employee_table WHERE user_id='%s'" % text)
result = cursor.fetchone()
conn.close()
if result:
# 员工ID存在,切换到EmployeeInfoWindow界面,显示员工信息
employee_info_window = EmployeeInfoWindow(text, self.stacked_widget)
self.stacked_widget.addWidget(employee_info_window)
self.stacked_widget.setCurrentIndex(1)
else:
# 员工ID不存在,弹出提示框,提示重新输入
QMessageBox.warning(self, '错误', '员工ID不存在,请重新输入!')
if __name__ == '__main__':
app = QApplication(sys.argv)
loginWindow = LoginWindow()
loginWindow.show()
sys.exit(app.exec_())
```
阅读全文