Qt防止本程序多次启动

利用WindowsAPI,在main.cpp中添加以下代码:

#include <windows.h>


// 注:这里的 L"fortest_abc123"可以替换为其它名称,我只是举个例子
bool checkOne()
{
    //  创建互斥量
    HANDLE m_hMutex  =  CreateMutex(NULL, FALSE,  L"fortest_abc123" );
    //  检查错误代码
    if  (GetLastError()  ==  ERROR_ALREADY_EXISTS)  {
        //  如果已有互斥量存在则释放句柄并复位互斥量
        CloseHandle(m_hMutex);
        m_hMutex  =  NULL;
        //  程序退出
        return  false;
    }
    else
        return true;
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //防止程序开启两次
    if(!checkOne()) {
        return 0;
    }
    MainWindow w;
    w.show();

    return a.exec();
}

最近学习python用pygame做了一个小游戏,在拓展游戏设置和网络对战功能时,用pyqt5,Qt Designer设计了游戏设置和网络界面,发现第一次启动时能正常调用界面,但关闭之后不能再次打开,尝试过直接调用和新开线程调用,均不能解决问题,求解!? #窗口文件代码 #窗口界面代码 ``` class Ui_dialog(object): def setupUi(self, dialog): dialog.setObjectName("dialog") dialog.setWindowModality(QtCore.Qt.NonModal) dialog.resize(480, 320) self.buttonBox = QtWidgets.QDialogButtonBox(dialog) self.buttonBox.setGeometry(QtCore.QRect(240, 270, 221, 41)) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok) self.buttonBox.setObjectName("buttonBox") self.verticalLayoutWidget = QtWidgets.QWidget(dialog) self.verticalLayoutWidget.setGeometry(QtCore.QRect(9, 9, 461, 251)) self.verticalLayoutWidget.setObjectName("verticalLayoutWidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setObjectName("verticalLayout") self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget) self.label_2.setObjectName("label_2") self.verticalLayout.addWidget(self.label_2) self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.radioButton_3 = QtWidgets.QRadioButton(self.verticalLayoutWidget) self.radioButton_3.setEnabled(True) self.radioButton_3.setTabletTracking(False) self.radioButton_3.setChecked(True) self.radioButton_3.setObjectName("radioButton_3") self.buttonGroup = QtWidgets.QButtonGroup(dialog) self.buttonGroup.setObjectName("buttonGroup") self.buttonGroup.addButton(self.radioButton_3) self.horizontalLayout.addWidget(self.radioButton_3) self.radioButton = QtWidgets.QRadioButton(self.verticalLayoutWidget) self.radioButton.setObjectName("radioButton") self.buttonGroup.addButton(self.radioButton) self.horizontalLayout.addWidget(self.radioButton) self.verticalLayout.addLayout(self.horizontalLayout) self.label = QtWidgets.QLabel(self.verticalLayoutWidget) self.label.setObjectName("label") self.verticalLayout.addWidget(self.label) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.radioButton_4 = QtWidgets.QRadioButton(self.verticalLayoutWidget) self.radioButton_4.setChecked(True) self.radioButton_4.setObjectName("radioButton_4") self.buttonGroup_2 = QtWidgets.QButtonGroup(dialog) self.buttonGroup_2.setObjectName("buttonGroup_2") self.buttonGroup_2.addButton(self.radioButton_4) self.horizontalLayout_2.addWidget(self.radioButton_4) self.radioButton_5 = QtWidgets.QRadioButton(self.verticalLayoutWidget) self.radioButton_5.setObjectName("radioButton_5") self.buttonGroup_2.addButton(self.radioButton_5) self.horizontalLayout_2.addWidget(self.radioButton_5) self.radioButton_10 = QtWidgets.QRadioButton(self.verticalLayoutWidget) self.radioButton_10.setObjectName("radioButton_10") self.buttonGroup_2.addButton(self.radioButton_10) self.horizontalLayout_2.addWidget(self.radioButton_10) self.radioButton_11 = QtWidgets.QRadioButton(self.verticalLayoutWidget) self.radioButton_11.setObjectName("radioButton_11") self.buttonGroup_2.addButton(self.radioButton_11) self.horizontalLayout_2.addWidget(self.radioButton_11) self.radioButton_6 = QtWidgets.QRadioButton(self.verticalLayoutWidget) self.radioButton_6.setObjectName("radioButton_6") self.buttonGroup_2.addButton(self.radioButton_6) self.horizontalLayout_2.addWidget(self.radioButton_6) self.verticalLayout.addLayout(self.horizontalLayout_2) self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget) self.label_3.setObjectName("label_3") self.verticalLayout.addWidget(self.label_3) self.horizontalSlider = QtWidgets.QSlider(self.verticalLayoutWidget) self.horizontalSlider.setMaximum(10) self.horizontalSlider.setProperty("value", 5) self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal) self.horizontalSlider.setTickInterval(0) self.horizontalSlider.setObjectName("horizontalSlider") self.verticalLayout.addWidget(self.horizontalSlider) self.label_4 = QtWidgets.QLabel(self.verticalLayoutWidget) self.label_4.setObjectName("label_4") self.verticalLayout.addWidget(self.label_4) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.radioButton_7 = QtWidgets.QRadioButton(self.verticalLayoutWidget) self.radioButton_7.setObjectName("radioButton_7") self.buttonGroup_3 = QtWidgets.QButtonGroup(dialog) self.buttonGroup_3.setObjectName("buttonGroup_3") self.buttonGroup_3.addButton(self.radioButton_7) self.horizontalLayout_3.addWidget(self.radioButton_7) self.radioButton_8 = QtWidgets.QRadioButton(self.verticalLayoutWidget) self.radioButton_8.setChecked(True) self.radioButton_8.setObjectName("radioButton_8") self.buttonGroup_3.addButton(self.radioButton_8) self.horizontalLayout_3.addWidget(self.radioButton_8) self.verticalLayout.addLayout(self.horizontalLayout_3) self.retranslateUi(dialog) self.buttonBox.accepted.connect(dialog.accept) self.buttonBox.rejected.connect(dialog.reject) self.radioButton_5.clicked.connect(self.gamedj) QtCore.QMetaObject.connectSlotsByName(dialog) dialog.setTabOrder(self.radioButton_3, self.radioButton) dialog.setTabOrder(self.radioButton, self.radioButton_4) dialog.setTabOrder(self.radioButton_4, self.radioButton_5) dialog.setTabOrder(self.radioButton_5, self.radioButton_10) dialog.setTabOrder(self.radioButton_10, self.radioButton_11) dialog.setTabOrder(self.radioButton_11, self.radioButton_6) dialog.setTabOrder(self.radioButton_6, self.horizontalSlider) dialog.setTabOrder(self.horizontalSlider, self.radioButton_7) dialog.setTabOrder(self.radioButton_7, self.radioButton_8) def retranslateUi(self, dialog): _translate = QtCore.QCoreApplication.translate dialog.setWindowTitle(_translate("dialog", "游戏设置")) self.label_2.setText(_translate("dialog", "游戏模式选择:")) self.radioButton_3.setText(_translate("dialog", "普通阶")) self.radioButton.setText(_translate("dialog", "可变阶")) self.label.setText(_translate("dialog", "游戏阶数(最高阶):")) self.radioButton_4.setText(_translate("dialog", "4")) self.radioButton_5.setText(_translate("dialog", "5")) self.radioButton_10.setText(_translate("dialog", "6")) self.radioButton_11.setText(_translate("dialog", "7")) self.radioButton_6.setText(_translate("dialog", "8")) self.label_3.setText(_translate("dialog", "游戏速度选择:")) self.label_4.setText(_translate("dialog", "道具模式选择:")) self.radioButton_7.setText(_translate("dialog", "开启")) self.radioButton_8.setText(_translate("dialog", "关闭")) #逻辑执行类 class MyMainWindow(QDialog,Ui_dialog): def __init__(self, parent = None): super(MyMainWindow, self).__init__(parent) self.setupUi(self) # 连接槽函数 #self.radioButton_5.clicked.connect(self.gamedj) #定义新增槽函数ok def gamedj(self): print(“o'k” ) #设置内容测试 #定义调用函数 def setting_ui(): app = QApplication(sys.argv) win = MyMainWindow() win.show() app.exec() #新开线程 class setui_Thread(threading.Thread): def __init__(self): super(setui_Thread, self).__init__() def run(self): currentTreadname = threading.currentThread() #测试 print("set ui running in", currentTreadname) #测试 setting_ui() setui = setui_Thread() setui.setDaemon(daemonic=True) #调用线程或函数代码片 #检测鼠标点击是否在游戏设置 if ksyx3.textRect.collidepoint(mouse_x,mouse_y): if not ai_settings.game_set: ai_settings.game_set = True #setui.run() setui.start() ``` 请各位大神答疑,谢谢!
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页