微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自定义消息框在pyqt5中不返回任何值

如何解决自定义消息框在pyqt5中不返回任何值

从我之前的问题中,我知道如何在主应用程序的线程内部将子级连接到父级。 但是我的消息框应该在按下按钮时返回值,并等待父级直到子级关闭

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import  QApplication,QWidget,QPushButton,QDialog,qframe,QLabel,QTextEdit
from threading import Thread
import threading
import time


class messageBox(QObject):
    speak_word = pyqtSignal(str)
    def __init__(self,parent):
        super().__init__()
        self.parent=parent
        #self.speak_word = pyqtSignal(str)
        
        self.parent_width=self.parent.geometry().width()
        self.parent_height=self.parent.geometry().height()
        self.speak_word.connect(self.sayHelloFromMainThread)
    #This method will flash the messageBox dialog when we click on parent
    def flash(self,event):
                    QTimer.singleShot(50,lambda:self.toolbar.setStyleSheet("background-color: blue;\n"
        "border-top-left-radius: 20px;\n"
        "border-top-right-radius: 20px;\n"
        "") )
                    QTimer.singleShot(120,lambda:self.toolbar.setStyleSheet("background-color: red;\n"
        "border-top-left-radius: 20px;\n"
        "border-top-right-radius: 20px;\n"
        "") )

    def sayHelloFromMainThread(self):
         self.showinfo('hello','how r u?')
    def hellor(self):
        self.speak_word.emit('hi')
    def showinfo(self,title="ShowInfo",msg="Hello,There!"):
        self.value=None
        self.pop = QDialog()
        self.pop.setGeometry(500,200,454,165)
        self.msg=msg
        self.pop.mousepressEvent=self.click
        self.pop.mouseMoveEvent=self.move
        self.frame = qframe(self.pop)
        self.frame.setStyleSheet("background-color: #1b1b1b;\n"
            "border-bottom-left-radius: 20px;\n"
            "border-bottom-right-radius: 20px;\n"
            "")
        self.frame.setGeometry(0,30,134)
        self.toolbar = qframe(self.pop)
        self.toolbar.setStyleSheet("background-color:red;\n"
            "border-top-left-radius: 20px;\n"
            "border-top-right-radius: 20px;\n"
            "")
        self.toolbar.setGeometry(0,30)
        #This makes the window to frameless
        self.pop.setwindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint)
        self.pop.setAttribute(Qt.WA_TranslucentBackground,True)
        #self.cover will Cover a frame to its parent entirely
        self.cover=qframe(self.parent)
        self.cover.resize(self.parent_width,self.parent_height)
        self.cover.setStyleSheet("background-color:transparent;")
        #you can see the frame by setting background to a color
        self.cover.show()
        self.cover.mousepressEvent=self.flash
        b1 = QPushButton("Ok",self.frame)
        b1.setStyleSheet('''QPushButton {background-color: red;
        border-style: outset;
        border-width: 2px;
        border-radius: 10px;
        border-color: beige;
        font: bold 14px;
        min-width: 60px;
        min-height: 25px;
        }''''''QPushButton:pressed{background-color: green;
        border-style: inset;}''')
        b1.move(350,100)
        b1.clicked.connect(self.on_close)
        self.pop.setwindowTitle("Dialog")
        self.pop.setwindowModality(Qt.WindowModal)
        self.pop.exec_()
        return self.value
    def on_close(self):
        self.value=True
        self.cover.deleteLater()
        self.pop.close()
    # move and click  methods are used to drag the dialog
    def move(self,event):
            if event.buttons() ==Qt.LeftButton:
                    deltax = event.x()- self.xp
                    deltay = event.y() - self.yp
                    x = self.pop.x() + deltax
                    y = self.pop.y() + deltay
                    width,height=int(self.pop.geometry().width()),int(self.pop.geometry().height())
                    self.pop.setGeometry(int(x),int(y),width,height)
    def click(self,event):
           if event.buttons() == Qt.LeftButton:
               self.xp=event.x()
               self.yp=event.y()


def window():
       app = QApplication(sys.argv)
       w = QWidget()
       w.setGeometry(100,100,400,400)
       b = QPushButton(w)
       b.setText("Hello World!")
       b.move(50,50)
       ui = messageBox(w)
       def dowork():
            h=ui.hellor()
            print(h)
       def run():
               Thread(target=dowork).start()
       b.clicked.connect(run)
       w.setwindowTitle("PyQt Dialog demo")
       w.show()
       sys.exit(app.exec_())
if __name__ == '__main__':
   window()

没有从线程调用它使父等待,但我不知道如何通过按钮单击来返回值,假设是true或false。 任何帮助将不胜感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。