【发布时间】:2021-01-16 23:35:03
【问题描述】:
我在 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
class messagebox():
def __init__(self,parent):
self.parent=parent
self.parent_width=self.parent.geometry().width()
self.parent_height=self.parent.geometry().height()
#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 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,454,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,0,454,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)
messbox=messagebox(w)
def run():
h=messbox.showinfo('hello','how r u?')
print(h)
#Thread(target=run).start()
b.clicked.connect(run)
w.setWindowTitle("PyQt Dialog demo")
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
window()
但唯一的问题是当我从一个线程调用消息框方法(如messbox.showinfo())时,它说无法创建连接,因为孩子在新线程中。我已经浏览了这个网站上关于工作线程的各种示例。但是我无法正确理解工作线程。在这里我注释掉了“线程部分”并通过按钮单击调用。谁能让我明白吗?谢谢你!!!
【问题讨论】:
标签: python python-3.x pyqt pyqt5 signals-slots