我的python脚本关闭触摸板和电源按钮为什么?

时间:2015-02-28 08:07:55

标签: python linux

我在Manjaro linux上使用KDE。我在python中有这个脚本来关闭触摸板:

#!/usr/bin/python
import sys

from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QCoreApplication
from subprocess import call

class Example(QWidget):

def __init__(self):
    super().__init__()

    self.initUI()


def initUI(self):               

    qbtn = QPushButton('On', self)
    qbtn.clicked.connect(self.handleButtonOn)
    qbtn.resize(qbtn.sizeHint())
    qbtn.move(25, 50)  

    qbtn = QPushButton('Off', self)
    qbtn.clicked.connect(self.handleButtonOff)
    qbtn.resize(qbtn.sizeHint())
    qbtn.move(125, 50) 

    self.setGeometry(300, 300, 250, 100)
    self.setWindowTitle('Touchpad On/Off')    
    self.show()

def handleButtonOn(self, event):
    print ('On')
    call(["synclient", "touchpadoff=0"])
    call(["notify-send", "Your touchpad is set to ON"])
    self.destroy()

def handleButtonOff(self, event):
    print ('Off')
    call(["synclient", "touchpadoff=1"])
    call(["notify-send", "Your touchpad is set to OFF"])
    self.destroy()

if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) 

它的工作几乎完美(关闭我的触控板),但是当我启动脚本时,它会关闭我的电源按钮,因此我无法通过此按钮关闭计算机。 Yakuake也有问题,不能写在这个终端。最后,我启动了一些其他终端,例如" konsole"并通过shutdown命令关闭计算机。 我是python中的新手。如何使这项工作好。我需要关掉我的触控板,我使用外接鼠标。

1 个答案:

答案 0 :(得分:0)

我无法通过电源按钮重现您的问题,但我发现self.destroy()会导致您的脚本冻结在某些已损坏且未响应的SIGINT状态。用self.close()替换self.destroy()使它在我的机器上运行。

请尝试用self.close()替换self.destroy()。