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, 50) info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;") id_label = QLabel("员工ID:" + result[0], self) id_label.move(70, 100) id_label.setStyleSheet("font-size: 18px; color: black;") name_label = QLabel("姓名:" + str(result[1]), self) name_label.move(70, 150) name_label.setStyleSheet("font-size: 18px; color: black;") six_label = QLabel("性别:" + result[2], self) six_label.move(70, 200) six_label.setStyleSheet("font-size: 18px; color: black;") sfz_label = QLabel("身份证:" + str(result[3]), self) sfz_label.move(70, 250) sfz_label.setStyleSheet("font-size: 18px; color: black;") tel_label = QLabel("电话:" + result[4], self) tel_label.move(70, 300) tel_label.setStyleSheet("font-size: 18px; color: black;") # 创建返回按钮 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)在员工信息界面右边设置数字显示框,接收显示def get_data(): ser = serial.Serial('COM3', 9600) data = ser.readline() if data: # 将字符串按空格分割,并取第三个元素作为数值 value_str = data.decode().strip().split()[1] value = float(value_str) ser.close() return value
时间: 2023-12-14 19:40:12 浏览: 122
你可以在 initUI() 方法中添加一个 QLabel 控件,用来显示数字。然后在 get_data() 方法中读取串口数据并返回,将返回的数值设置给 QLabel 控件即可。
以下是修改后的代码示例:
```
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, 50)
info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;")
id_label = QLabel("员工ID:" + result[0], self)
id_label.move(70, 100)
id_label.setStyleSheet("font-size: 18px; color: black;")
name_label = QLabel("姓名:" + str(result[1]), self)
name_label.move(70, 150)
name_label.setStyleSheet("font-size: 18px; color: black;")
six_label = QLabel("性别:" + result[2], self)
six_label.move(70, 200)
six_label.setStyleSheet("font-size: 18px; color: black;")
sfz_label = QLabel("身份证:" + str(result[3]), self)
sfz_label.move(70, 250)
sfz_label.setStyleSheet("font-size: 18px; color: black;")
tel_label = QLabel("电话:" + result[4], self)
tel_label.move(70, 300)
tel_label.setStyleSheet("font-size: 18px; color: black;")
# 创建数字显示框
self.value_label = QLabel(self)
self.value_label.setGeometry(500, 100, 200, 50)
self.value_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;")
# 创建返回按钮
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)
def get_data(self):
ser = serial.Serial('COM3', 9600)
data = ser.readline()
if data:
# 将字符串按空格分割,并取第三个元素作为数值
value_str = data.decode().strip().split()[1]
value = float(value_str)
ser.close()
# 将数值设置给 QLabel 控件
self.value_label.setText("数值:" + str(value))
```
阅读全文