简体   繁体   English

用PyQt4实例化QWidget的需求

[英]Need held with instancing a QWidget with PyQt4

This code works: 此代码有效:

from PyQt4 import QtGui
import sys

app=QtGui.QApplication(sys.argv)


window1=QtGui.QWidget()
window1.show()

window2=QtGui.QWidget()
window2.show()

But this doesnt: 但这不是:

from PyQt4.QtGui import *
import sys

class window(QMainWindow):
    def __init__(self):
        super(window, self).__init__()
        self.w=QWidget()
        self.w.show()

app=QApplication(sys.argv)
window()
window()

How can i create 2 windows by instancing the class window? 如何通过实例化类窗口来创建2个窗口? I don't get it with Qt, with Tkinter it is very easy to figure out... 我不了解Qt,使用Tkinter可以很容易地发现...

EDIT: The question above is meant for creating some windows by clicking on a button in the systray. 编辑:上面的问题旨在通过单击系统托盘中的按钮来创建一些窗口。 As you can see when executing the code below, it works, but there is just one window shown at any time, eg if i clicked twice the context menu of the systray icon to create two windows. 正如您在执行下面的代码时所看到的那样,它可以工作,但是在任何时候都只显示一个窗口,例如,如果我两次单击systray图标的上下文菜单来创建两个窗口。 I don't see where it comes from... 我不知道它来自哪里...

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.app = QApplication(sys.argv)

        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")
        self.note=Note()
        self.note.show()

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

main()

EDIT2: Got it! EDIT2:明白了! The problem can be solved by coding it this way: 可以通过以下方式解决此问题:

class main():
    def __init__(self):
        self.notes=[]
        ...

    def newNote(self):
        note=Note()
        note.show()
        self.notes.append(note)

Though still I don't know why now it works, or even no window occurs if you delete the line "self.notes.append(note)". 虽然我仍然不知道为什么现在可以正常工作,但是如果删除“ self.notes.append(note)”行,甚至没有窗口出现。 But hah, it works! 但是,它有效!

Try something like this: 尝试这样的事情:

from PyQt4.QtGui import *
import sys

class window(QMainWindow):
    def __init__(self):
        super(window, self).__init__()
        self.w=QWidget()
        self.setCentralWidget(self.w)

app=QApplication(sys.argv)
w1 = window()
w1.show()
w2 = window()
w2.show()
app.exec()

You need to show the top-level containers, not the internal widgets. 您需要显示顶级容器,而不是内部窗口小部件。 And you probably wanted that widget to show inside your main window and not as an independent window. 而且您可能希望该小部件显示在主窗口中,而不是作为独立窗口显示。

The problem with your updated code is that when the click "Add note" a second time, you replace the member self.note with a new window. 更新代码的问题在于,当第二次单击“添加注释”时,将成员self.note替换为新窗口。 The previous window is therefore not referenced anywhere after the call, and gets destroyed. 因此,在调用之后的任何地方都不会引用前一个窗口,它会被销毁。
If you want to keep multiple windows open, you need to keep a handle on all of them as long as you need them. 如果要保持打开多个窗口,则需要在所有窗口上都保持一个句柄。

Warning: I don't actually know python, so the naive use of a list might not be a good idea - I don't know. 警告:我实际上并不了解python,因此单纯使用列表可能不是一个好主意-我不知道。

Try this: 尝试这个:

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

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

The setQuitOnLastWindowClosed part is necessary in your use case, otherwise app.exec would exit once you've closed all the note windows, and your app would exit at that point - doesn't look like that's what you want to happen. setQuitOnLastWindowClosed部分是你的使用情况必须的,否则app.exec将退出一旦你关闭了所有的音符窗口,和你的应用程序将退出在这一点-看起来并不像这就是你要发生什么。

Note: that's not a usual use of QMainWindow . 注意:这不是QMainWindow的通常用法。 That's a "heavy" class, usually use as the sole main window of a "complete" GUI app, with a menu, toolbar(s), status bar, etc. Using a simple QWidget with a QTextEdit and maybe a couple of buttons sounds better for this use case. 那是一个“沉重的”类,通常用作“完整” GUI应用程序的唯一主窗口,带有菜单,工具栏,状态栏等。将简单的QWidgetQTextEdit一起使用,也许还可以听到几个按钮的声音对于这个用例来说更好。 In fact you could probably get away with deriving your Note from QTextEdit and implementing just a context menu. 实际上,您可能会从QTextEdit导出Note并仅实现上下文菜单而逃脱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM