要跟踪的两部分代码之间的差异原因

2024-04-26 23:36:05 发布

您现在位置:Python中文网/ 问答频道 /正文

不起作用的代码是这样的:(请考虑Note部分;Main()只用于创建systrayicon(由于tkinter的原因,它最初是用win32gui实现的,但在qt中,代码要少得多)。我知道混合使用这些工具箱/框架是不好的)。你知道吗

from tkinter import Tk,Menu,TOP,Frame,X,NO,BOTH,YES,BOTTOM
from PyQt4.QtGui import *
import sys

class Note():
    def __init__(self):
        self.root=Tk()
        print("Note has been created, but is not being displayed. Why? \n If Exit is clicked, it shows.")

class Main():
    def __init__(self):
        self.notes=[]
        self.app = QApplication(sys.argv)
        self.app.setQuitOnLastWindowClosed(False);

        self.trayIcon = QSystemTrayIcon(QIcon("J:\\python\\SimpleNotes.ico"), self.app)
        self.menu = QMenu()

        self.newWindow = self.menu.addAction("new Note")
        self.separator = self.menu.addSeparator()
        self.exitAction = self.menu.addAction("Exit")

        self.exitAction.triggered.connect(self.close)
        self.newWindow.triggered.connect(self.newNote)
        self.trayIcon.setContextMenu(self.menu)
        self.trayIcon.show()

        self.app.exec()

    def newNote(self):
        print("Create new note entry has been clicked")
        note=Note()
        #note.show() #because note is of Tk, it gots no show()
        self.notes.append(note)

    def close(self):
        self.trayIcon.hide()
        self.app.exit()
        print("Exit menu entry has been clicked")

Main()

代码是这样的:(我只替换了Note()部分,现在用Qt代替了tkinter,并让Note显示Qt的原因)

import sys
from PyQt4.QtGui import *


class Note(QMainWindow):
    def __init__(self):
        super(Note,self).__init__()
        self.w=QWidget()
        self.setWindowTitle("Note")
        self.setCentralWidget(self.w)

class Main():
    def __init__(self):
        self.notes=[]
        self.app = QApplication(sys.argv)
        self.app.setQuitOnLastWindowClosed(False);

        self.trayIcon = QSystemTrayIcon(QIcon("J:\\python\\SimpleNotes.ico"), self.app)
        self.menu = QMenu()

        self.newWindow = self.menu.addAction("new Note")
        self.separator = self.menu.addSeparator()
        self.exitAction = self.menu.addAction("Exit")

        self.exitAction.triggered.connect(self.close)
        self.newWindow.triggered.connect(self.newNote)
        self.trayIcon.setContextMenu(self.menu)
        self.trayIcon.show()

        self.app.exec()

    def newNote(self):
        print("Create new note entry has been clicked")
        note=Note()
        note.show()
        self.notes.append(note)

    def close(self):
        self.trayIcon.hide()
        self.app.exit()
        print("Exit menu entry has been clicked")

Main()

Tags: importselfappinitmaindefexitnote
1条回答
网友
1楼 · 发布于 2024-04-26 23:36:05

tkinter窗口不显示的原因是您没有调用Tk实例的mainloop方法。窗口仅在屏幕上绘制以响应事件,并且事件仅在事件循环运行时得到处理(而mainloop是事件循环的开始)

在你的问题中你写道:

I know that it is bad to mix those toolkits/frameworks

与其说它本身是坏的,不如说它本身是坏的。更像是不可能。不是真的,从字面上说,不可能,只是可能相当困难,更容易出错,比值得努力去做。你知道吗

两个(任何)GUI工具包都需要一个事件循环来运行,而且这两个事件循环彼此不兼容。即使您成功地将两者合并,一个工具箱中的窗口也无法与另一个工具箱中的窗口进行交互。你知道吗

相关问题 更多 >