我有一个程序在主线程中有一个PyQt中的gui。它与光电探测器通信并在另一个线程中获取功率读数,该线程向主线程发送信号以更新gui的功率值。 现在我想用一个电机自动对准我的光纤,从光电探测器获得反馈。
所以我创建了一个控制电机的类,但我必须以某种方式将光电探测器读数传递给该类。首先,我试图访问父母的权力变量,但它没有工作。
然后我在我的gui中创建了一个方法来返回变量的值并尝试从电机类中访问它。我遇到了一个问题,即我不能先使用父方法而不使用__init__
。有没有办法绕过它?我无法再次调用gui __init__
,我只想在子类中使用其中一种方法。
如果有其他方法可以做到这一点,我也会感到高兴。
PS:我想我不能给子类提供光检测器对象,因为它在另一个线程中,对吧?
- Edit-- gui代码是:
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
self.PDvalue = 0 #initial PD value
self.PDState = 0 #control the PD state (on-off)
self.PDport = self.dialog.pm100d.itemText(self.dialog.pm100d.currentIndex()) #gets pot info
def __init__(self):
... #a lot of other stuff
self.nano = AlgoNanoMax.NanoMax('COM12') #creates the motor object
self.nano_maxX.clicked.connect(self.NanoMaximizeX) #connect its fun to a buttom
self.actionConnect_PM100D.triggered.connect(self.ActionConnect_PM100D) #PD buttom
def NanoMaximizeX(self):
self.nano.maximize_nano_x() #uses motor object function
def ActionConnect_PM100D(self):
if self.PDState == 0: #check if PD is on
self.PD = PDThread(self.PDState, self.PDport) #creates thread
self.PD.valueupdate.connect(self.PDHandler) #signal connect
self.PD.dialogSignal.connect(self.PDdialog) #create error dialog
self.threads = []
self.threads.append(self.PD)
self.PD.start() #start thread
else:
self.PDState = 0
self.PD.state = 0 #stop thread
self.startpd.setText('Start PD') #change buttom name
def PDHandler(self, value):
self.PDvalue = value #slot to get pow from thread
def ReturnPow(self):
return self.PDvalue #return pow (I tried to use this to pass to the motor class)
def PDdialog(self):
self.dialog.set_instrument('PM100D') #I have a dialog that says error and asks you to type the right port
if self.dialog.exec_() == QtGui.QDialog.Accepted: #if Ok buttom try again
ret = self.dialog.pm100d.itemText(self.dialog.pm100d.currentIndex()) #new port
self.PD.port = str(ret)
self.PD.flagWhile = False #change PD stop loop condition to try again
else: #pressed cancel, so it gives up
self.PD.photodetector.__del__() #delete objects
self.PD.terminate() #stop thread
self.PD.quit()
现在是PD类,它位于另一个线程中但与gui在同一个文件中:
class PDThread(QtCore.QThread):
valueupdate = QtCore.pyqtSignal(float) #creating signals
dialogSignal = QtCore.pyqtSignal() #signal in case of error
state = 1 #used to stop thread
def __init__(self, state, port):
QtCore.QThread.__init__(self)
self.photodetector = PM100D() #creates the PD object
self.port = port
def run(self):
while True:
self.flagWhile = True #used to leave while
try:
self.photodetector.connect(self.port) #try to connect
except:
self.dialogSignal.emit() #emit error signal
while self.flagWhile == True:
time.sleep(0.5) #wait here until user press something in the dialog, which is in another thread
else:
break #leave loop when connected
window.PDState = 1 #change state of main gui buttom (change functionality to turn off if pressed again)
window.startpd.setText('Stop PD') #change buttom label
while self.state == 1:
time.sleep(0.016)
value = self.photodetector.get_pow() #get PD pow
self.valueupdate.emit(value) #emit it
AlgoNanoMax文件:
import gui
from NanoMax import Nano
class NanoMax(gui.MyApp): #inheriting parent
def __init__(self, mcontroller_port):
self.mcontroller = Nano(mcontroller_port) #mcontroller is the communication to the motor
def maximize_nano_x(self, step=0.001, spiral_number=3):
''' Alignment procedure with the nano motor X'''
print 'Optimizing X'
power = super(NanoMax, self).ReturnPow() #here I try to read from the photodetector
xpos = self.mcontroller.initial_position_x
position = []
position = [[power, xpos]]
xsign = 1
self.mcontroller.move_relative(self.mcontroller.xaxis, (-1) * spiral_number * step)
print 'X nano move: '+ str((-1) * spiral_number * step * 1000) + ' micrometers'
time.sleep(4)
power = super(NanoMax, self).ReturnPow()
xpos += (-1) * spiral_number * step
position.append([power, xpos])
for _ in xrange(2*spiral_number):
self.mcontroller.move_relative(self.mcontroller.xaxis, xsign * step)
print 'X nano move: '+ str(xsign * step * 1000) + ' micrometers'
time.sleep(5)
power = super(NanoMax, self).ReturnPow()
xpos += xsign * step
position.append([power, xpos])
pospower = [position[i][0] for i in xrange(len(position))]
optimalpoint = pospower.index(max(pospower))
x_shift = (-1) * (xpos - position[optimalpoint][1])
print 'Maximum power: ' + str(max(pospower)) + ' dBm'
print 'Current power: ' + str(super(NanoMax, self).ReturnPow()) + ' dBm'
self.mcontroller.move_relative(self.mcontroller.xaxis, x_shift)
答案 0 :(得分:1)
__init__
和NanoMax
的{{1}}应调用MyApp
以确保为所有级别进行初始化(如果这是Python 2,则无法使用no-arg super().__init__()
,因此分别为super
和super(NanoMax, self).__init__()
。这假定super(MyApp, self).__init__()
已正确编写了新式类,并正确使用PyQT
本身;你在其他地方使用super
,所以至少前者是真的。在所有类中适当地使用super
将确保所有级别super
- 编辑一次,同时手动列出超级类不能在某些继承模式中工作,或者可能调用某些__init__
个多次或根本没有。
如果有多个级别可能会带参数,您也应该接受__init__
/ *args
并将它们转发到**kwargs
调用,以便将参数转发到需要的位置去。
结合这两者,您的代码应如下所示:
super().__init__
注意:如果超重类可能在其class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
super(MyApp, self).__init__(*args, **kwargs)
... rest of __init__ ...
class PDThread(QtCore.QThread):
def __init__(self, state, port, *args, **kwargs):
super(PDThread, self).__init__(*args, **kwargs)
...
class NanoMax(gui.MyApp): #inheriting parent
def __init__(self, mcontroller_port, *args, **kwargs):
super(NanoMax, self).__init__(*args, **kwargs)
self.mcontroller = Nano(mcontroller_port) #mcontroller is the communication to the motor
中调用的方法超载,并且您的重载取决于您自己__init__
中设置的状态,则需要设置之前的状态,而不是__init__
之后。合作多重继承可能是一种痛苦。另请注意,对于除最低级别类之外的任何内容,使用位置参数可能会因多重继承而变得难看,因此按关键字传递所有参数可能是有意义的,并且只接受并转发super().__init__(...)
,而不是**kwargs
,因此,如果继承层次结构略有变化,人们就不会以破坏的方式传递位置参数。
答案 1 :(得分:0)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
self.PDvalue = 0 #initial PD value
self.PDState = 0 #control the PD state (on-off)
在上面的代码中,它在函数外部设置一个变量。要在类中执行此操作,请不要将self关键字放在其前面。这样你就可以进入类定义
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
PDvalue = 0 #initial PD value
PDState = 0 #control the PD state (on-off)
并在超级行
power = super(NanoMax, self).PDvalue
例如:
>>> class Hi:
H = 5
def __init__(self):
self.g = 6
>>> class Bye(Hi):
def H(self):
print(super(Bye, self).H)
>>> e = Bye()
>>> e.H()
5
>>>