QSystemTrayIcon使python崩溃

时间:2018-05-30 13:15:08

标签: python qt5 pyside

QSystemTrayIcon使应用程序崩溃。我在控制台中没有出错。 Windows说AppHangB1。但是,有时它会工作几次,有时在第一次显示上下文菜单时会崩溃。

编辑:

这是我的代码分解为最重要的事情:

class SystemTrayIcon(QSystemTrayIcon):

def __init__(self, icon, parent=None):
    QSystemTrayIcon.__init__(self, icon, parent)
    menu = QMenu()
    showAction = menu.addAction("Fenster anzeigen")
    exitAction = menu.addAction("Beenden")
    menu.setStyleSheet("QMenu{background-color:white; margin:2px; "
                       "font: 75 10pt Trebuchet MS;} "
                       "QMenu::item:selected{background-color:#8e0000;}")
    self.setContextMenu(menu)
    self.setToolTip("KOSE")
    exitAction.triggered.connect(self.exit)
    showAction.triggered.connect(self.show_window)

def exit(self):
    sys.exit()

def show_window(self):
    if (loading_screen_window.windowOpacity() == 0.0):
        loading_screen_window.fadeIN()

class CancelWindow(QWidget):
def __init__(self):
    super(CancelWindow, self).__init__()
    loader = QUiLoader()
    file = QFile("cancel.ui")
    file.open(QFile.ReadOnly)
    global cancel_screen
    cancel_screen = loader.load(file, self)
    file.close()
    self.initUI()

def initUI(self):
    #some init stuff like window pos, button- function connects...

def fadeIN(self):
    #window fade in animation

def fadeOUT(self, exit):
    #fade out animation

def ja(self):
    #button func
    self.fadeOUT(True)

def nein(self):
    #button func
    self.fadeOUT(False)
    kunden_screen.lbl_opacity.hide()
    kunden_screen.setEnabled(True)

def exit_app(self):
    QCoreApplication.exit() 

class LoadingScreen(QWidget):
def __init__(self):
    super(LoadingScreen, self).__init__()
    loader = QUiLoader()
    file = QFile("loading_screen.ui")
    file.open(QFile.ReadOnly)
    global loading_screen
    loading_screen = loader.load(file, self)
    file.close()
    self.initUI()

    #Config Thread
    self.config_thread = config()
    self.config_thread.finished.connect(self.config_finished)

    #Config Thread starten
    self.config_thread.start()

def initUI(self):
    #window init

def fadeIN(self):
    #fade animation

def fadeOUT(self):
    #fade animation

def config_finished(self):
    #function thats called when config thread is finished

class KundenSelect(QWidget):
connected = 0
cursor = None
conn_database = None

def __init__(self):
    super(KundenSelect, self).__init__()
    loader = QUiLoader()
    file = QFile("kunden.ui")
    file.open(QFile.ReadOnly)
    global kunden_screen
    kunden_screen = loader.load(file, self)
    file.close()
    self.initUI()

def initUI(self):      
    #window init

class config(QThread):  
def run(self):
    #config thread, reading files, database connection ...

app = QApplication(sys.argv)

global loading_screen_window
loading_screen_window = LoadingScreen()

global kunden_screen_window
kunden_screen_window = KundenSelect()
kunden_screen.lbl_opacity.hide()
kunden_screen_window.hide()

global cancel_screen_window
cancel_screen_window = CancelWindow()
cancel_screen_window.hide()

trayIcon = SystemTrayIcon(QIcon("icon.png"), parent=app)
trayIcon.show()

sys.exit(app.exec_())  

我用谷歌搜索了它,但没有找到任何解决方案。我正在使用PySide2和Qt5。

由于

1 个答案:

答案 0 :(得分:0)

此代码似乎正在改变信号/插槽语法

class SystemTrayIcon(QSystemTrayIcon):

    def __init__(self, icon, parent=None):
        QSystemTrayIcon.__init__(self, icon, parent)
        menu = QMenu()
        showAction = menu.addAction("Fenster anzeigen")
        exitAction = menu.addAction("Beenden")
        menu.setStyleSheet("QMenu{background-color:white; margin:2px; "
                           "font: 75 10pt Trebuchet MS;} "
                           "QMenu::item:selected{background-color:#8e0000;}")
        self.setContextMenu(menu)
        self.setToolTip("KOSE")
        exitAction.triggered.connect(self.exit)
        showAction.triggered.connect(self.show_window)

    def exit(self):
        sys.exit()

    def show_window(self):
        print("xxxxxx")
        # if (loading_screen_window.windowOpacity() == 0.0):
        # loading_screen_window.fadeIN()

app = QApplication(sys.argv)

trayIcon = SystemTrayIcon(QIcon("icon.png"), parent=app)
trayIcon.show()

sys.exit(app.exec_())

使用Pyside(不是Pyside2),但我不认为这会有所不同。