首页 > 解决方案 > 如何在 PyQt5 中获取返回值?

问题描述

如何获取返回值?附上我的代码。我的意图:想要检查 QListWidget,如果它是空的,则会弹出一个消息框并询问用户的选项。问题:在开始阶段,会显示一个消息框弹出窗口,(我不希望在开始阶段出现),如果QListwidget为空,则消息框弹出窗口将根据需要显示,如果我们选择“是”或“否”按钮,第一个文件中不会返回/打印任何内容。如何解决?

主要的

import sys
from PyQt5.QtWidgets import QWidget,QApplication,QListWidget,QVBoxLayout,QLineEdit
from msgbox_secondfile import *

class MsgBox(QWidget):
    def __init__(self):
        super(). __init__()
        self.setWindowTitle("Msgbox")

        self.textbox = QLineEdit()
        self.listbox = QListWidget()
        self.listbox.addItems(["item001","item002","Gold Fish"])
        self.vbox = QVBoxLayout(self)
        self.vbox.addWidget(self.textbox)
        self.vbox.addWidget(self.listbox)

        self.getdetails_1 = Msgbox_002(self.textbox, self.listbox)
        self.textbox.textChanged.connect(self.getdetails_1.func_textbox_textchanged)

        # Here,I need a retun value from  Method "func_create_newitem, I try the follwing code
        # This code make a 2 problems,
        # first problem : At beggning itself, msg box will displayed, I dont want at beggning
        # second: after click the "yes" or "No" button from msg box, nothing will print here

        new = Msgbox_002.func_create_newitem(self)
        print("retun value print :",new)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainwindow = MsgBox()
    mainwindow.show()
    sys.exit(app.exec_())

第二

import sys
from PyQt5.QtWidgets import  QMessageBox,QWidget
from PyQt5.QtCore import Qt

class Msgbox_002(QWidget) :
    def __init__(self, tb,lb):
        super().__init__()
        self.textbox = tb
        self.listbox = lb


    def func_textbox_textchanged(self):
        self.item_contains = self.listbox.findItems(self.textbox.text(), Qt.MatchContains)

        if len(self.item_contains) == 0 :
            self.func_create_newitem()


    def func_create_newitem(self):
        self.msgBox = QMessageBox()
        self.msgBox.setIcon(QMessageBox.Information)
        self.msgBox.setText("Item Not found.... Create a new One" )
        self.msgBox.setWindowTitle("QMessageBox Example")
        self.msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        self.returnValue = self.msgBox.exec_()
        

        if self.msgBox.clickedButton() is self.msgBox.button(QMessageBox.Yes):
            print("Yes button clicked")
            self.ss = " ok button cliked from return"
            return self.ss
        if self.msgBox.clickedButton() is self.msgBox.button(QMessageBox.No):
            # print("No Button Clicked")
            self.ss = "cancle button clicked from return"
            return self.ss

标签: pythonpyqtpyqt5

解决方案


首先,不需要在新类中进行验证,调用 func_create_newitem 也是合乎逻辑的,因为正如您指出的,当找不到项目时必须调用该方法。

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication, QListWidget, QVBoxLayout, QLineEdit

from msgbox_secondfile import MessageBox


class Widget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Msgbox")

        self.textbox = QLineEdit()
        self.listbox = QListWidget()
        self.listbox.addItems(["item001", "item002", "Gold Fish"])

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.textbox)
        vbox.addWidget(self.listbox)

        self.msgbox = MessageBox()

        self.textbox.textChanged.connect(self.handle_text_changed)

    def handle_text_changed(self):
        text = self.textbox.text()
        if not text:
            return
        items = self.listbox.findItems(text, Qt.MatchContains)
        if not items:
            res = self.msgbox.execute()
            print(res)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainwindow = Widget()
    mainwindow.show()
    sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import QMessageBox


class MessageBox(QMessageBox):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setIcon(QMessageBox.Information)
        self.setText("Item Not found.... Create a new One")
        self.setWindowTitle("QMessageBox Example")
        self.setStandardButtons(QMessageBox.Yes | QMessageBox.No)

    def execute(self):
        self.exec_()
        if self.clickedButton() is self.button(QMessageBox.Yes):
            return " ok button cliked from return"
        elif self.clickedButton() is self.button(QMessageBox.No):
            return "cancel button clicked from return"
        return "not button clicked"

推荐阅读