我正在制作一个应用程序,我需要在启动时向用户显示一条弹出消息,告诉他们该程序的说明。有没有办法使弹出框显示在程序的开头?我已经准备好消息框功能,我只需要一种在开头显示它的方法
在来这里提问之前,我做了所有可能的研究,但是我找不到任何关于该主题的信息。我在Windows 10上使用Python 3.7
和PyQt5 5.13.0
class Ui_AutoClicker(object):
def setupUi(self, AutoClicker):
AutoClicker.setObjectName("AutoClicker")
AutoClicker.resize(586, 182)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icon.ico"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
AutoClicker.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(AutoClicker)
self.centralwidget.setObjectName("centralwidget")
self.push_start_end = QtWidgets.QPushButton(self.centralwidget)
self.push_start_end.setGeometry(QtCore.QRect(10, 120, 161, 51))
font = QtGui.QFont()
font.setPointSize(11)
self.push_start_end.setFont(font)
self.push_start_end.setObjectName("push_start_end")
self.label_delay = QtWidgets.QLabel(self.centralwidget)
self.label_delay.setGeometry(QtCore.QRect(10, 0, 191, 41))
font = QtGui.QFont()
font.setPointSize(12)
self.label_delay.setFont(font)
self.label_delay.setObjectName("label_delay")
self.input_delay = QtWidgets.QLineEdit(self.centralwidget)
self.input_delay.setGeometry(QtCore.QRect(10, 50, 291, 61))
font = QtGui.QFont()
font.setPointSize(12)
self.input_delay.setFont(font)
self.input_delay.setText("")
self.input_delay.setObjectName("input_delay")
self.label_click_count = QtWidgets.QLabel(self.centralwidget)
self.label_click_count.setGeometry(QtCore.QRect(320, 0, 261, 41))
font = QtGui.QFont()
font.setPointSize(12)
self.label_click_count.setFont(font)
self.label_click_count.setObjectName("label_click_count")
self.lcd_click_counter = QtWidgets.QLCDNumber(self.centralwidget)
self.lcd_click_counter.setGeometry(QtCore.QRect(320, 50, 261, 121))
self.lcd_click_counter.setProperty("intValue", 42069)
self.lcd_click_counter.setObjectName("lcd_click_counter")
self.lcd_click_counter.setProperty('digitCount', 10)
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(300, 0, 20, 251))
self.line.setFrameShape(QtWidgets.QFrame.VLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.push_close = QtWidgets.QPushButton(self.centralwidget)
self.push_close.setGeometry(QtCore.QRect(180, 120, 121, 51))
font = QtGui.QFont()
font.setPointSize(11)
self.push_close.setFont(font)
self.push_close.setObjectName("push_close")
AutoClicker.setCentralWidget(self.centralwidget)
self.keyboard = KController()
self.retranslateUi(AutoClicker)
QtCore.QMetaObject.connectSlotsByName(AutoClicker)
self.push_close.clicked.connect(self.close_app)
self.push_close.clicked.connect(QCoreApplication.instance().quit)
self.push_start_end.clicked.connect(self.start_clicker)
self.lcd_click_counter.setProperty('intValue', 1)
def retranslateUi(self, AutoClicker):
_translate = QtCore.QCoreApplication.translate
AutoClicker.setWindowTitle(_translate("AutoClicker", "Auto Clicker"))
self.push_start_end.setText(
_translate("AutoClicker", "Start/Stop (F4)"))
self.label_delay.setText(_translate("AutoClicker", "Delay in
seconds"))
self.label_click_count.setText(_translate(
"AutoClicker", "Number of clicks clicked"))
self.push_close.setText(_translate("AutoClicker", "Close (F6)"))
def show_initial_message(self):
msg = QMessageBox()
msg.setWindowTitle('Initial information')
msg.setText("Please use the 'Close (F6)' button to close the program."
"Closing it by pressing the red X button on the top will "
"leave the "
"autoclicker running until the key 'F6' is pressed.")
msg.setIcon(QMessageBox.Information)
msg.setStandardButtons(QMessageBox.Ok)
x = msg.exec_()
答案 0 :(得分:0)
如果要在显示主窗口之前先显示它,则将其放在类外的函数中,并在创建主窗口并显示它之前运行它。
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
# --- classes ---
class MyApp(QWidget):
pass
# --- functions ---
def show_message():
msg = QMessageBox()
msg.setWindowTitle('Initial information')
msg.setText("Please use the 'Close (F6)' button to close the program.\n\n"
"Closing it by pressing the red X button on the top will "
"leave the autoclicker running until the key 'F6' is pressed.")
msg.setIcon(QMessageBox.Information)
msg.setStandardButtons(QMessageBox.Ok)
msg.exec()
# --- main ---
app = QApplication([])
show_message()
my_app = MyApp()
my_app.show()
app.exec()
最终在__init__
之前的self.show()
中运行它
如果要在显示主窗口后显示它,请在__init__
之后在self.show()
中运行它
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
# --- classes ---
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.show()
self.show_message()
def show_message(self):
msg = QMessageBox()
msg.setWindowTitle('Initial information')
msg.setText("Please use the 'Close (F6)' button to close the program.\n\n"
"Closing it by pressing the red X button on the top will "
"leave the autoclicker running until the key 'F6' is pressed.")
msg.setIcon(QMessageBox.Information)
msg.setStandardButtons(QMessageBox.Ok)
msg.exec()
# --- functions ---
# --- main ---
app = QApplication([])
my_app = MyApp()
app.exec()
如果要延迟显示,则可以使用QTimer
。
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtCore import QTimer
# --- classes ---
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.show()
timer = QTimer(self)
timer.timeout.connect(self.show_message)
timer.setSingleShot(True)
timer.start(3000) # 3s
def show_message(self):
msg = QMessageBox()
msg.setWindowTitle('Initial information')
msg.setText("Please use the 'Close (F6)' button to close the program.\n\n"
"Closing it by pressing the red X button on the top will "
"leave the autoclicker running until the key 'F6' is pressed.")
msg.setIcon(QMessageBox.Information)
msg.setStandardButtons(QMessageBox.Ok)
msg.exec()
# --- main ---
app = QApplication([])
my_app = MyApp()
app.exec()
答案 1 :(得分:0)
尝试:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_AutoClicker(object):
def setupUi(self, AutoClicker):
AutoClicker.setObjectName("AutoClicker")
AutoClicker.resize(586, 182)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icon.ico"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
AutoClicker.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(AutoClicker)
self.centralwidget.setObjectName("centralwidget")
self.push_start_end = QtWidgets.QPushButton(self.centralwidget)
self.push_start_end.setGeometry(QtCore.QRect(10, 120, 161, 51))
font = QtGui.QFont()
font.setPointSize(11)
self.push_start_end.setFont(font)
self.push_start_end.setObjectName("push_start_end")
self.label_delay = QtWidgets.QLabel(self.centralwidget)
self.label_delay.setGeometry(QtCore.QRect(10, 0, 191, 41))
font = QtGui.QFont()
font.setPointSize(12)
self.label_delay.setFont(font)
self.label_delay.setObjectName("label_delay")
self.input_delay = QtWidgets.QLineEdit(self.centralwidget)
self.input_delay.setGeometry(QtCore.QRect(10, 50, 291, 61))
font = QtGui.QFont()
font.setPointSize(12)
self.input_delay.setFont(font)
self.input_delay.setText("")
self.input_delay.setObjectName("input_delay")
self.label_click_count = QtWidgets.QLabel(self.centralwidget)
self.label_click_count.setGeometry(QtCore.QRect(320, 0, 261, 41))
font = QtGui.QFont()
font.setPointSize(12)
self.label_click_count.setFont(font)
self.label_click_count.setObjectName("label_click_count")
self.lcd_click_counter = QtWidgets.QLCDNumber(self.centralwidget)
self.lcd_click_counter.setGeometry(QtCore.QRect(320, 50, 261, 121))
self.lcd_click_counter.setProperty("intValue", 42069)
self.lcd_click_counter.setObjectName("lcd_click_counter")
self.lcd_click_counter.setProperty('digitCount', 10)
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(300, 0, 20, 251))
self.line.setFrameShape(QtWidgets.QFrame.VLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.push_close = QtWidgets.QPushButton(self.centralwidget)
self.push_close.setGeometry(QtCore.QRect(180, 120, 121, 51))
font = QtGui.QFont()
font.setPointSize(11)
self.push_close.setFont(font)
self.push_close.setObjectName("push_close")
AutoClicker.setCentralWidget(self.centralwidget)
# ? KController self.keyboard = KController()
self.retranslateUi(AutoClicker)
QtCore.QMetaObject.connectSlotsByName(AutoClicker)
# ? close_app self.push_close.clicked.connect(self.close_app)
self.push_close.clicked.connect(QtCore.QCoreApplication.instance().quit) # QtCore.
# ? start_clicker self.push_start_end.clicked.connect(self.start_clicker)
self.lcd_click_counter.setProperty('intValue', 1)
def retranslateUi(self, AutoClicker):
_translate = QtCore.QCoreApplication.translate
AutoClicker.setWindowTitle(_translate("AutoClicker", "Auto Clicker"))
self.push_start_end.setText(
_translate("AutoClicker", "Start/Stop (F4)"))
self.label_delay.setText(_translate("AutoClicker", "Delay in seconds"))
self.label_click_count.setText(_translate(
"AutoClicker", "Number of clicks clicked"))
self.push_close.setText(_translate("AutoClicker", "Close (F6)"))
def show_initial_message(self):
msg = QtWidgets.QMessageBox()
msg.setWindowTitle('Initial information')
msg.setText("Please use the 'Close (F6)' button to close the program."
"Closing it by pressing the red X button on the top will "
"leave the "
"autoclicker running until the key 'F6' is pressed.")
msg.setIcon(QtWidgets.QMessageBox.Information)
msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
x = msg.exec_()
class Window(QtWidgets.QMainWindow, Ui_AutoClicker):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication([])
application = Window()
application.show_initial_message() # +++
application.show()
sys.exit(app.exec_())