当我调用错误消息一(请参阅代码中的注释)时,该消息很快出现然后消失。但是,如果我调用错误消息二,它就会出现,并且只有在我单击“确定”按钮时才会消失。
如何修复它以使错误消息一像错误消息二一样工作?
try:
connection = pymysql.connect(host = 'localhost',
user = 'root',
db = 'Telephon Register',
cursorclass = pymysql.cursors.DictCursor)
cur = connection.cursor()
if number!= "":
cur.execute("SELECT Number FROM formen WHERE Telephonebook = " + self.number.text() )
result = cur.fetchone()
if len(result) == 0:
cur.execute("INSERT INTO formen VALUES(" + self.number.text())
connection.commit()
else:
print("The number " + number+ " already exists.")
else:
print("You have not typed a number!")
msg = QMessageBox() #EXCEPTION MESSAGE ONE
msg.setIcon(2)
msg.setText("Some Text")
msg.setInformativeText("Some informative text")
msg.setWindowTitle("Error")
msg.show()
connection.close() …Run Code Online (Sandbox Code Playgroud) 我想向 QMessagebox 添加一个自定义按钮,用于打开 matplotlib 窗口,以及一个“确定”按钮,供用户在想要关闭它时单击
我目前已经可以正常工作了,但我希望这两个按钮能够执行单独的操作而不是打开窗口。
我知道我可以创建一个具有所需结果的对话框窗口,但我想知道如何使用 QMessageBox。
import sys
from PyQt5 import QtCore, QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
msgbox = QtWidgets.QMessageBox()
msgbox.setWindowTitle("Information")
msgbox.setText('Test')
msgbox.addButton(QtWidgets.QMessageBox.Ok)
msgbox.addButton('View Graphs', QtWidgets.QMessageBox.YesRole)
bttn = msgbox.exec_()
if bttn:
print("Ok")
else:
print("View Graphs")
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
期望的结果:
确定按钮 - 关闭 QMessageBox
“查看图形”按钮 - 打开 matplotlib 窗口并保持 QMessageBox 打开,直到用户单击“确定”
我正在使用 PyQt5,并且创建了一个 QMessagebox,如果单击“是”按钮,我想打印一些内容。这是我的代码
self.messageBox = QMessageBox()
self.messageBox.setText("Are You Sure with Left Edge You Chosed?")
self.messageBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
if self.messageBox == QMessageBox.Yes:
self.confirmation = 1
print("Yess Clicked")
else:
self.confirmation = 0
self.messageBox.exec()
Run Code Online (Sandbox Code Playgroud) 我正在为我的屏幕创建一个错误消息框。我使用 QDialogButtonBox 作为按钮。现在我想用不同的颜色填充按钮。例如:“确定”--> 绿色“取消”--> 红色等。我可以更改所有按钮的背景,但不能单独更改。
有没有办法做到这一点?
提前致谢 !!!!
我尝试使用信号从线程接收字符串到我的主 GUI。一切正常,直到我想在 QMessageBox 中使用该字符串。打印出来没有问题,但是启动 QMessageBox 给了我几个错误(有些是关于 QPixmap 的,我什至不在 GUI 中使用。
这是我的代码的一个简短的工作示例:
import sys
import urllib2
import time
from PyQt4 import QtCore, QtGui
class DownloadThread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
time.sleep(3)
self.emit(QtCore.SIGNAL("threadDone(QString)"), 'test')
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.list_widget = QtGui.QListWidget()
self.button = QtGui.QPushButton("Start")
self.button.clicked.connect(self.start_download)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.button)
layout.addWidget(self.list_widget)
self.setLayout(layout)
self.downloader = DownloadThread()
self.connect(self.downloader, QtCore.SIGNAL("threadDone(QString)"), self.threadDone, QtCore.Qt.DirectConnection)
def start_download(self):
self.downloader.start()
def threadDone(self, info_message):
print info_message
QtGui.QMessageBox.information(self,
u"Information",
info_message
)
#self.show_info_message(info_message)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = …Run Code Online (Sandbox Code Playgroud) 我正在为使用Qt框架的考试做一些准备,我想知道如何以基本方式使用QInputDialog和QMessageBox(我的考试是手写编码)
Qt API在使用方面真的很难理解它对于我的项目来说很好,因为我可以以一种非常"hacky"的方式完成我想要的东西,我的关于这个主题的设置书很糟糕......
让我谈谈在这种情况下使用QInputDialog和QMessageBox的简洁方法:
#include <QApplication>
#include <QInputDialog>
#include <QDate>
#include <QMessageBox>
int computeAge(QDate id) {
int years = QDate::currentDate().year() - id.year();
int days = QDate::currentDate().daysTo(QDate
(QDate::currentDate().year(), id.month(), id.day()));
if(days > 0)
years--;
return years
}
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
/* I want my QInputDialog and MessageBox in here somewhere */
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
对于我的QInputDialog,我希望用户给出他们的出生日期(不要担心输入验证)我想使用QMessageBox来显示用户的年龄
我只是不明白在基本情况下需要进入QInputDialog和QMessageBox的参数是什么,因为那里似乎没有任何示例.
我怎么做到这一点?
qmessagebox ×6
pyqt ×4
python ×4
pyqt5 ×3
qt ×2
c++ ×1
dialog ×1
python-3.x ×1
qdate ×1
qt4 ×1