# 创建数字显示框和外框 value_widget = QWidget(self) value_widget.setGeometry(500, 100, 200, 100) self.lcd_1 = QLCDNumber(value_widget) self.lcd_1.setSegmentStyle(QLCDNumber.Flat) self.lcd_1.setDigitCount(10) self.lcd_1.display(0.123456789) hbox_layout = QHBoxLayout(value_widget) hbox_layout.addWidget(self.lcd_1) hbox_layout.setContentsMargins(0, 0, 0, 0) hbox_layout.setSpacing(0) value_widget.setStyleSheet(''' background-color: #eee; border-radius: 10px; border: 2px solid 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(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)) def update_lcd(self): value = self.get_data self.lcd_1.display(value)报错TypeError: arguments did not match any overloaded call: display(self, str: str): argument 1 has unexpected type 'method' display(self, num: float): argument 1 has unexpected type 'method' display(self, num: int): argument 1 has unexpected type 'method'
时间: 2023-12-10 10:03:05 浏览: 208
在 update_lcd 函数中,你调用了 get_data 函数,但是没有加上括号,导致返回的是函数本身而不是函数返回值。你需要修改 update_lcd 函数中的代码,将 get_data 后面的括号加上来,以获取正确的数值。修改后的代码如下:
```
def update_lcd(self):
value = self.get_data()
self.lcd_1.display(value)
```
相关问题
def startWeighing(self): next_widget = QWidget() self.stacked_widget.addWidget(next_widget) # 添加一个QLabel控件用于显示信息 self.info_label = QLabel("员工信息", next_widget) self.info_label.move(100, 50) self.info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;") self.id_label = QLabel("员工ID:", next_widget) self.id_label.move(70, 100) self.id_label.setStyleSheet("font-size: 18px; color: black;") self.name_label = QLabel("姓名:", next_widget) self.name_label.move(70, 150) self.name_label.setStyleSheet("font-size: 18px; color: black;") # 创建数字显示框和外框 value_widget = QWidget(next_widget) value_widget.setGeometry(500, 100, 200, 100) # self.stacked_widget.addWidget(value_widget) self.lcd_1 = QLCDNumber(value_widget) self.lcd_1.setSegmentStyle(QLCDNumber.Flat) self.lcd_1.setDigitCount(10) self.lcd_1.display('0 ') hbox_layout = QHBoxLayout(value_widget) hbox_layout.addWidget(self.lcd_1) hbox_layout.setContentsMargins(0, 0, 0, 0) hbox_layout.setSpacing(0) # 切换到下一个界面 self.stacked_widget.setCurrentWidget(next_widget)怎么将获取的到串口数据更新到数字框内
可以在串口数据接收函数中使用以下代码将获取的到串口数据更新到数字框内:
```
def update_lcd(self, value):
self.lcd_1.display(value)
```
然后在串口数据接收函数中调用该方法即可:
```
def serial_data_received(self):
data = self.serial.readAll().data().decode()
# 处理获取到的数据
self.update_lcd(data)
```
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.startWeighing) def startWeighing(self): # 创建一个新的QWidget info_widget = QWidget(self.stacked_widget) # 将info_widget添加到QStackedWidget中 self.stacked_widget.addWidget(info_widget) # 切换到新的QWidget self.stacked_widget.setCurrentWidget(info_widget) self.info_label = QLabel("员工信息", self) self.info_label.move(100, 50) self.info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;") self.id_label = QLabel("员工ID:", self) self.id_label.move(70, 100) self.id_label.setStyleSheet("font-size: 18px; color: black;") self.name_label = QLabel("姓名:", self) self.name_label.move(70, 150) self.name_label.setStyleSheet("font-size: 18px; color: black;") self.six_label = QLabel("性别:", self) self.six_label.move(70, 200) self.six_label.setStyleSheet("font-size: 18px; color: black;") self.sfz_label = QLabel("身份证:", self) self.sfz_label.move(70, 250) self.sfz_label.setStyleSheet("font-size: 18px; color: black;") self.tel_label = QLabel("电话:", self) self.tel_label.move(70, 300) self.tel_label.setStyleSheet("font-size: 18px; color: black;") self.setFixedSize(800, 500) self.setWindowTitle('员工信息') # 创建数字显示框和外框 value_widget = QWidget(self) value_widget.setGeometry(500, 100, 200, 100) self.lcd_1 = QLCDNumber(value_widget) self.lcd_1.setSegmentStyle(QLCDNumber.Flat) self.lcd_1.setDigitCount(10) self.lcd_1.display('0 KG') # self.thread = DataThread() # self.thread.valueUpdated.connect(self.lcd_1.display) # self.thread.start() hbox_layout = QHBoxLayout(value_widget) hbox_layout.addWidget(self.lcd_1) hbox_layout.setContentsMargins(0, 0, 0, 0) hbox_layout.setSpacing(0) value_widget.setStyleSheet(''' background-color: #eee; border-radius: 10px; border: 2px solid 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)没有显示label
根据代码,我发现在 `startWeighing` 函数中,所有的 QLabel 都是使用 self 作为 parent 创建的,但是在代码中,self 指的是 LoginWindow,而不是 info_widget。所以,应该将所有的 QLabel 的 parent 改为 info_widget,才能正确显示。修改后的代码如下:
```
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.startWeighing)
def startWeighing(self):
# 创建一个新的QWidget
info_widget = QWidget(self.stacked_widget)
# 将info_widget添加到QStackedWidget中
self.stacked_widget.addWidget(info_widget)
# 切换到新的QWidget
self.stacked_widget.setCurrentWidget(info_widget)
# 创建员工信息
self.info_label = QLabel("员工信息", info_widget)
self.info_label.move(100, 50)
self.info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;")
self.id_label = QLabel("员工ID:", info_widget)
self.id_label.move(70, 100)
self.id_label.setStyleSheet("font-size: 18px; color: black;")
self.name_label = QLabel("姓名:", info_widget)
self.name_label.move(70, 150)
self.name_label.setStyleSheet("font-size: 18px; color: black;")
self.six_label = QLabel("性别:", info_widget)
self.six_label.move(70, 200)
self.six_label.setStyleSheet("font-size: 18px; color: black;")
self.sfz_label = QLabel("身份证:", info_widget)
self.sfz_label.move(70, 250)
self.sfz_label.setStyleSheet("font-size: 18px; color: black;")
self.tel_label = QLabel("电话:", info_widget)
self.tel_label.move(70, 300)
self.tel_label.setStyleSheet("font-size: 18px; color: black;")
self.setFixedSize(800, 500)
self.setWindowTitle('员工信息')
# 创建数字显示框和外框
value_widget = QWidget(info_widget)
value_widget.setGeometry(500, 100, 200, 100)
self.lcd_1 = QLCDNumber(value_widget)
self.lcd_1.setSegmentStyle(QLCDNumber.Flat)
self.lcd_1.setDigitCount(10)
self.lcd_1.display('0 KG')
hbox_layout = QHBoxLayout(value_widget)
hbox_layout.addWidget(self.lcd_1)
hbox_layout.setContentsMargins(0, 0, 0, 0)
hbox_layout.setSpacing(0)
value_widget.setStyleSheet('''
background-color: #eee;
border-radius: 10px;
border: 2px solid black;
''')
# 创建返回按钮
back_button = QPushButton('返回', info_widget)
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)
```
这样就能正确显示所有的 QLabel 了。
阅读全文