本文介绍了在 PyQt PushButton 单击期间发送附加变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Python 和 PyQt 的新手,这是我的第一个应用程序.当前,当单击按钮时,toggleLED()
检查 self.LedOn
以决定是否打开 LED.
I'm new to Python and PyQt and this is my first application. Currently when a button is clicked, toggleLED()
checks self.LedOn
to decide whether to turn an LED on.
class Screen(QWidget):
def initUI(self):
self.LedOn = 0
btn = QPushButton('Off', self)
btn.setCheckable(True)
btn.clicked.connect(self.toggleLED)
def toggleLED(self, pressed):
source = self.sender()
if self.LedOn:
source.setText('Off')
source.LedOn = 0
self.serial.write('L')
else:
source.setText('On')
source.LedOn = 1
self.serial.write('H')
问题:现在如果我们想要控制多个 LED 并记住它们各自的切换状态,最佳做法是什么?
Problem: Now if we want to control multiple LEDs and remember their individual toggle state, what is the best practice?
我似乎也无法使用 btn.clicked.connect(self.toggleLED(btnNumber))
class Screen(QWidget):
def initUI(self):
btn1 = QPushButton('Off', self)
btn1.setCheckable(True)
btn1.LedOn= 0
btn1.clicked.connect(selftoggleLED(1))
btn2 = QPushButton('Off', self)
btn2.setCheckable(True)
btn2.LedOn= 0
btn2.clicked.connect(selftoggleLED(2))
def toggleLED(self, pressed, value):
source = self.sender()
if self.LedOn:
source.setText('Off')
self.serial.write(value)
self.serial.write('L')
else:
source.setText('On')
self.serial.write(value)
self.serial.write('H')
推荐答案
你可以使用partial
:
from functools import partial
btn1.clicked.connect(partial(self.toggleLED, 1))
这允许您将多个参数传递给一个函数.
This allows you to pass multiple arguments to a function.
这篇关于在 PyQt PushButton 单击期间发送附加变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!