问题描述
从我之前的问题中,我知道如何在主应用程序的线程内部将子级连接到父级。 但是我的消息框应该在按下按钮时返回值,并等待父级直到子级关闭。
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 (将#修改为@)