如何在 PyQt5 中获取 return 值?
How to get a return value in PyQt5?
如何获取return值?附上我的代码。我的意图:想要检查 QListWidget,如果它是空的,将弹出消息框并询问用户的选项。 问题: 在开始阶段,会显示一个消息框弹出窗口,(我不希望在开始阶段出现),如果QListwidget 为空,则显示消息框弹出窗口根据需要,如果我们 select“是”或“否”按钮,则第一个文件中不会 return/print。如何解决?
主要
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
首先,没有必要在新的 class 中进行验证,调用 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"
如何获取return值?附上我的代码。我的意图:想要检查 QListWidget,如果它是空的,将弹出消息框并询问用户的选项。 问题: 在开始阶段,会显示一个消息框弹出窗口,(我不希望在开始阶段出现),如果QListwidget 为空,则显示消息框弹出窗口根据需要,如果我们 select“是”或“否”按钮,则第一个文件中不会 return/print。如何解决?
主要
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
首先,没有必要在新的 class 中进行验证,调用 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"