2016-12-25 4 views
0

Ich versuche, eine Tastaturverknüpfung zu machen, um die Anwendung zu beenden, nachdem eine kritische Nachricht erscheint. Ich möchte, dass der Benutzer die Tastenkombination drückt, dann mit einer kritischen Nachricht aufgefordert wird und nach dem Klicken auf "Ja" wird das Programm beendet. Ich habe es schon eine Weile versucht und konnte es nicht zur Arbeit bringen. hier ist das, was ich habePython pyqt4 Tastenkürzel

hier ist mein Code

import sys 
import webbrowser 
import random 
import time 
import os 
import subprocess 
from PyQt4.QtCore import QSize, QTimer 
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPainter, QPixmap, QMessageBox, \ 
    QAction, QKeySequence 


def CloseSC(self): 
    msg = QMessageBox() 
    msg.setIcon(QMessageBox.Critical) 
    msg.setText("This is a message box") 
    msg.setInformativeText("This is additional information") 
    msg.setWindowTitle("MessageBox demo") 
    msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) 


class MainWindow(QMainWindow,): 
    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     self.setGeometry(50, 50, 400, 450) 
     self.setFixedSize(400, 450) 
     self.startUIWindow() 

     self.actionExit = QAction(('E&xit'), self) 
     self.actionExit.setShortcut(QKeySequence("Ctrl+Q")) 
     self.actionExit.triggered.connect(CloseSC) 

Antwort

1

Sie Aktion hinzufügen müssen mit {your widget}.addAction({your action})

Widget Dies ist meine Lösung:

import sys 
from PyQt4.QtGui import QMainWindow, QMessageBox, QAction, QKeySequence, QApplication 


class MainWindow(QMainWindow): 
    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     self.setGeometry(50, 50, 400, 450) 
     self.setFixedSize(400, 450) 
     self.actionExit = QAction(('E&xit'), self) 
     self.actionExit.setShortcut(QKeySequence("Ctrl+Q")) 
     self.addAction(self.actionExit) 
     self.actionExit.triggered.connect(self.CloseSC) 

    def CloseSC(self): 
     msg = QMessageBox(self) 
     msg.setIcon(QMessageBox.Critical) 
     msg.setText("This is a message box") 
     msg.setInformativeText("This is additional information") 
     msg.setWindowTitle("MessageBox demo") 
     msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) 
     if msg.exec_() == QMessageBox.Ok: 
      self.close() 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    w = MainWindow() 
    w.show() 
    sys.exit(app.exec_()) 

enter image description here

Nach Strg + Q

enter image description here