gpt4 book ai didi

python-3.x - PyQt5 弹出窗口

转载 作者:行者123 更新时间:2023-12-01 19:01:07 25 4
gpt4 key购买 nike

我正在尝试使用 PyQt5 构建一个应用程序,当双击 QListWidget 中的项目时,该应用程序会“弹出”辅助窗口。

这是代码:

import sys
from PyQt5.QtWidgets import QWidget, QListWidget, QListWidgetItem, QLabel, QPushButton, QApplication


class exampleWidget(QWidget):
def __init__(self):
super().__init__()

self.initUI()

def initUI(self):
listWidget = QListWidget(self)
listWidget.itemDoubleClicked.connect(self.buildExamplePopup)

names = ["Jack", "Chris", "Joey", "Kim", "Duncan"]

for n in names:
QListWidgetItem(n, listWidget)

self.setGeometry(100, 100, 100, 100)
self.show()

@staticmethod
def buildExamplePopup(item):
name = item.text()
exPopup = examplePopup(name)
exPopup.setGeometry(100, 200, 100, 100)
exPopup.show()


class examplePopup(QWidget):
def __init__(self, name):
super().__init__()

self.name = name

self.initUI()

def initUI(self):
lblName = QLabel(self.name, self)

if __name__ == "__main__":
app = QApplication(sys.argv)
ex = exampleWidget()
sys.exit(app.exec_())

我希望在双击列表框中的其中一个名称时弹出第二个窗口,但我一生都无法让 examplePopup 小部件在屏幕上绘制。预先感谢您的帮助。

最佳答案

弹出窗口不会显示,因为您没有保留对它的引用,因此一旦 buildExamplePopup 返回,它就会被垃圾收集。

您可以像这样轻松解决问题:

    def buildExamplePopup(self, item):
name = item.text()
self.exPopup = examplePopup(name)
self.exPopup.setGeometry(100, 200, 100, 100)
self.exPopup.show()

关于python-3.x - PyQt5 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35810346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com