RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1338817
Accepted
arnold
arnold
Asked:2022-03-16 02:18:58 +0000 UTC2022-03-16 02:18:58 +0000 UTC 2022-03-16 02:18:58 +0000 UTC

工作 5 分钟后关闭程序

  • 772

以下情况:我需要以某种方式,使用注册表,在5工作几分钟后关闭程序并要求,例如,购买主要订阅。

我所做的最多是在一定数量的启动后实施关闭。

import winreg
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow
number, start_program = "", True


def software():
    with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
        with winreg.OpenKey(hkey, "SOFTWARE") as sub_key:
            try:
                i = 0
                while True:
                    path_value = winreg.EnumKey(sub_key, i)
                    print(f"{path_value}")
                    i += 1
            except OSError:
                print()
        winreg.CloseKey(sub_key)
    winreg.CloseKey(hkey)


def read_key_software():
    with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
        with winreg.OpenKey(hkey, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run") as sub_key:
            try:
                i = 0
                while True:
                    path_value = winreg.EnumValue(sub_key, i)
                    print(f"{path_value}")
                    i += 1
            except OSError:
                print("")
        winreg.CloseKey(sub_key)
    winreg.CloseKey(hkey)


def matherboard():
    name_board = "BaseBoardProduct"
    n_b = True
    with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
        with winreg.OpenKey(hkey, "HARDWARE\\DESCRIPTION\\System\\BIOS") as sub_key:
            try:
                i = 0
                while n_b:
                    path_value = winreg.EnumValue(sub_key, i)
                    if name_board == path_value[0]:
                        print(f"Matherboard: {path_value[1]}")
                        n_b = False
                    else:
                        i += 1
            except OSError:
                print("matherboard not found")
        winreg.CloseKey(sub_key)
    winreg.CloseKey(hkey)
    print("")


def processor():
    name_proc = "ProcessorNameString"
    n_p = True
    with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
        with winreg.OpenKey(hkey, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0") as sub_key:
            try:
                i = 0
                while n_p:
                    path_value = winreg.EnumValue(sub_key, i)
                    if name_proc == path_value[0]:
                        print(f"Proseccor: {path_value[1]}")
                        n_p = False
                    else:
                        i += 1
            except OSError:
                print("processor not found")
        winreg.CloseKey(sub_key)
    winreg.CloseKey(hkey)
    print("")


def SPLab():
    global number, start_program
    path = False
    with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as hkey:
        with winreg.OpenKey(hkey, "SOFTWARE") as sub_key:
            winreg.CreateKey(sub_key, "SPLab")
    winreg.CloseKey(hkey)
    with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as hkey:
        with winreg.OpenKey(hkey, "SOFTWARE\\SPLab", 0, winreg.KEY_READ) as sub_key:
            try:
                num = winreg.EnumValue(sub_key, 0)
                path = True
                start = num[1]
                if int(start) >= 10:
                    start_program = False
            except OSError:
                path = False
        winreg.CloseKey(sub_key)
        with winreg.OpenKey(hkey, "SOFTWARE\\SPLab", 0, winreg.KEY_WRITE) as sub_key:
            if path:
                start = int(start) + 1
            else:
                start = "1"
            winreg.SetValueEx(sub_key, "NamberOfStarts", 0, winreg.REG_SZ, str(start))
        winreg.CloseKey(sub_key)
    winreg.CloseKey(hkey)
    number = start
    print("")


class mainWin(QMainWindow):
    def __init__(self):
        super(mainWin, self).__init__()
        self.setWindowTitle("Program")

        if start_program:
            self.setGeometry(1000, 300, 580, 270)
            self.setStyleSheet("background : lightyellow;")
            self.b1 = QtWidgets.QPushButton(self)
            self.b1.setGeometry(20, 20, 250, 70)
            self.b1.setStyleSheet("background : lightgreen; font-size : 20px")
            self.b1.setText("software")
            self.b1.clicked.connect(software)
            self.b2 = QtWidgets.QPushButton(self)
            self.b2.setGeometry(300, 20, 250, 70)
            self.b2.setStyleSheet("background : lightgreen; font-size : 20px")
            self.b2.setText("read_key_software")
            self.b2.clicked.connect(read_key_software)
            self.b3 = QtWidgets.QPushButton(self)
            self.b3.setGeometry(20, 120, 250, 70)
            self.b3.setStyleSheet("background : lightgreen; font-size : 20px")
            self.b3.setText("matherboard")
            self.b3.clicked.connect(matherboard)
            self.b4 = QtWidgets.QPushButton(self)
            self.b4.setGeometry(300, 120, 250, 70)
            self.b4.setStyleSheet("background : lightgreen; font-size : 20px")
            self.b4.setText("processor")
            self.b4.clicked.connect(processor)
            self.l5 = QtWidgets.QLabel(self)
            self.l5.setGeometry(20, 210, 250, 50)
            self.l5.setText(f"Number of starts: {number}")
            self.l5.setStyleSheet("font-size : 20px")
        else:
            self.setGeometry(800, 400, 850, 300)
            self.setStyleSheet("background : white;")
            self.l1 = QtWidgets.QLabel(self)
            self.l1.setGeometry(25, 100, 1000, 70)
            self.l1.setText("Buy the full version of the program!")
            self.l1.setStyleSheet("font-size : 50px")


if __name__ == "__main__":
    SPLab()
    app = QApplication(sys.argv)
    mwin = mainWin()
    mwin.show()
    print("")
    sys.exit(app.exec_())
python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    S. Nick
    2022-03-16T04:23:30Z2022-03-16T04:23:30Z

    该类QTimer提供循环和一次性计时器。

    更多https://doc.qt.io/qt-5/qtimer.html

    在我的示例中,它将在 5 秒内工作,设置您需要的时间。

    import winreg
    import sys
    from PyQt5 import QtWidgets, QtCore
    from PyQt5.QtWidgets import QApplication, QMainWindow
    
    from PyQt5.Qt import *                                # +++
    
    number, start_program = "", True
    
    
    def software():
        with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
            with winreg.OpenKey(hkey, "SOFTWARE") as sub_key:
                try:
                    i = 0
                    while True:
                        path_value = winreg.EnumKey(sub_key, i)
                        print(f"{i} - {path_value}")
                        i += 1
                except OSError as e:
                    print(f'OSError: ------------- {e}')
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
    
    
    def read_key_software():
        with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
            with winreg.OpenKey(hkey, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run") as sub_key:
                try:
                    i = 0
                    while True:
                        path_value = winreg.EnumValue(sub_key, i)
                        print(f"{path_value}")
                        i += 1
                except OSError:
                    print("")
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
    
    
    def matherboard():
        name_board = "BaseBoardProduct"
        n_b = True
        with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
            with winreg.OpenKey(hkey, "HARDWARE\\DESCRIPTION\\System\\BIOS") as sub_key:
                try:
                    i = 0
                    while n_b:
                        path_value = winreg.EnumValue(sub_key, i)
                        if name_board == path_value[0]:
                            print(f"Matherboard: {path_value[1]}")
                            n_b = False
                        else:
                            i += 1
                except OSError:
                    print("matherboard not found")
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
        print("")
    
    
    def processor():
        name_proc = "ProcessorNameString"
        n_p = True
        with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
            with winreg.OpenKey(hkey, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0") as sub_key:
                try:
                    i = 0
                    while n_p:
                        path_value = winreg.EnumValue(sub_key, i)
                        if name_proc == path_value[0]:
                            print(f"Proseccor: {path_value[1]}")
                            n_p = False
                        else:
                            i += 1
                except OSError:
                    print("processor not found")
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
        print("")
    
    
    def SPLab():
        global number, start_program
        path = False
        
        with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as hkey:
            with winreg.OpenKey(hkey, "SOFTWARE") as sub_key:
                winreg.CreateKey(sub_key, "SPLab")
        winreg.CloseKey(hkey)
        
        with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as hkey:
            with winreg.OpenKey(hkey, "SOFTWARE\\SPLab", 0, winreg.KEY_READ) as sub_key:
                try:
                    num = winreg.EnumValue(sub_key, 0)
                    path = True
                    start = num[1]
                    if int(start) >= 10:
                        start_program = False
                except OSError:
                    path = False
            winreg.CloseKey(sub_key)
            with winreg.OpenKey(hkey, "SOFTWARE\\SPLab", 0, winreg.KEY_WRITE) as sub_key:
                if path:
                    start = int(start) + 1
                else:
                    start = "1"
                    
                winreg.SetValueEx(sub_key, "NamberOfStarts", 0, winreg.REG_SZ, str(start))
                
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
        number = start
        print("")
    
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setWindowTitle("Program")
            
            self.centralWidget = QWidget(self)                       # +++
            self.setCentralWidget(self.centralWidget)                # +++
            self.grid_layout = QGridLayout(self.centralWidget)       # +++
    
            if start_program:
                self.b1 = QtWidgets.QPushButton(self)
                self.grid_layout.addWidget(self.b1, 1, 1)            # +++
                self.b1.setText("software")
                self.b1.clicked.connect(software)
                
                self.b2 = QtWidgets.QPushButton(self)
                self.grid_layout.addWidget(self.b2, 1, 2)            # +++
                self.b2.setText("read_key_software")
                self.b2.clicked.connect(read_key_software)
                
                self.b3 = QtWidgets.QPushButton(self)
                self.grid_layout.addWidget(self.b3, 2, 1)            # +++
                self.b3.setText("matherboard")
                self.b3.clicked.connect(matherboard)
                
                self.b4 = QtWidgets.QPushButton(self)
                self.grid_layout.addWidget(self.b4, 2, 2)            # +++
                self.b4.setText("processor")
                self.b4.clicked.connect(processor)
                
                self.l5 = QtWidgets.QLabel(self)
                self.l5.setObjectName('l5')
                self.grid_layout.addWidget(self.l5, 3, 1, 1, 2)       # +++
                self.l5.setText(f"Number of starts: {number}")
    
    # !!! vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv            
                QtCore.QTimer.singleShot(1000 * 5,     self.end_program)
    #                                    1000 * 60 * 5  --> пять минут           
            else:
                self.end_program()
            
        def end_program(self):
            self.l1 = QtWidgets.QLabel(self)
            self.l1.setObjectName('l1')
            self.l1.setText("Buy the full version of the program!")
            
            self.setCentralWidget(self.l1)
    
    #  здесь, если вам надо, установить -------------------------->   '10'
    #                                                                 vvvv
    #  ...                                                            vvvv
    #  winreg.SetValueEx(sub_key, "NamberOfStarts", 0, winreg.REG_SZ, '10')        
    
    # !!! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    Stylesheet = '''
    QWidget {
        background : lightyellow;
    }
    QPushButton {
        background : lightgreen;  
        font-size : 20px;
        width: 250px;
        height: 70px;
    }
    #l1 {
        font-size : 50px;
    }
    #l5 {
        font-size : 20px;
    }
    '''
    
    
    
    if __name__ == "__main__":
        SPLab()
        app = QApplication(sys.argv)
        app.setStyleSheet(Stylesheet)
        mwin = MainWindow()
        mwin.show()
        print("start")
        sys.exit(app.exec_())
    

    在此处输入图像描述

    在此处输入图像描述


    更新:

    我注意到重新启动程序后再次工作。我可以以某种方式将时间绑定到注册表,以便它不能再启动第二次吗?

    我不太明白在哪里插入这一行,你能解释一下吗?

    import winreg
    import sys
    from PyQt5 import QtWidgets, QtCore
    from PyQt5.QtWidgets import QApplication, QMainWindow
    
    from PyQt5.Qt import *                                
    
    number, start_program = "", True
    
    
    def software():
        with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
            with winreg.OpenKey(hkey, "SOFTWARE") as sub_key:
                try:
                    i = 0
                    while True:
                        path_value = winreg.EnumKey(sub_key, i)
                        print(f"{i} - {path_value}")
                        i += 1
                except OSError as e:
                    print(f'OSError: ------------- {e}')
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
    
    
    def read_key_software():
        with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
            with winreg.OpenKey(hkey, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run") as sub_key:
                try:
                    i = 0
                    while True:
                        path_value = winreg.EnumValue(sub_key, i)
                        print(f"{path_value}")
                        i += 1
                except OSError:
                    print("")
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
    
    
    def matherboard():
        name_board = "BaseBoardProduct"
        n_b = True
        with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
            with winreg.OpenKey(hkey, "HARDWARE\\DESCRIPTION\\System\\BIOS") as sub_key:
                try:
                    i = 0
                    while n_b:
                        path_value = winreg.EnumValue(sub_key, i)
                        if name_board == path_value[0]:
                            print(f"Matherboard: {path_value[1]}")
                            n_b = False
                        else:
                            i += 1
                except OSError:
                    print("matherboard not found")
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
        print("")
    
    
    def processor():
        name_proc = "ProcessorNameString"
        n_p = True
        with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:
            with winreg.OpenKey(hkey, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0") as sub_key:
                try:
                    i = 0
                    while n_p:
                        path_value = winreg.EnumValue(sub_key, i)
                        if name_proc == path_value[0]:
                            print(f"Proseccor: {path_value[1]}")
                            n_p = False
                        else:
                            i += 1
                except OSError:
                    print("processor not found")
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
        print("")
    
    
    def SPLab(blocking=False):                                          # +++ blocking=False
        global number, start_program
        path = False
        
        with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as hkey:
            with winreg.OpenKey(hkey, "SOFTWARE") as sub_key:
                winreg.CreateKey(sub_key, "SPLab")
        winreg.CloseKey(hkey)
        
        with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as hkey:
            with winreg.OpenKey(hkey, "SOFTWARE\\SPLab", 0, winreg.KEY_READ) as sub_key:
                try:
                    num = winreg.EnumValue(sub_key, 0)
                    path = True
                    start = num[1]
                    if int(start) >= 10:
                        start_program = False
                except OSError:
                    path = False
            winreg.CloseKey(sub_key)
            with winreg.OpenKey(hkey, "SOFTWARE\\SPLab", 0, winreg.KEY_WRITE) as sub_key:
                if path:
                    start = int(start) + 1
                else:
                    start = "1"
    
    # +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv            
                if blocking: 
                    winreg.SetValueEx(sub_key, "NamberOfStarts", 0, winreg.REG_SZ, '10')
                else:
                    winreg.SetValueEx(sub_key, "NamberOfStarts", 0, winreg.REG_SZ, str(start))
    # +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^           
                
            winreg.CloseKey(sub_key)
        winreg.CloseKey(hkey)
        number = start
        print("")
    
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setWindowTitle("Program")
            
            self.centralWidget = QWidget(self)                   
            self.setCentralWidget(self.centralWidget)            
            self.grid_layout = QGridLayout(self.centralWidget)   
    
            if start_program:
                self.b1 = QtWidgets.QPushButton(self)
                self.grid_layout.addWidget(self.b1, 1, 1)        
                self.b1.setText("software")
                self.b1.clicked.connect(software)
                
                self.b2 = QtWidgets.QPushButton(self)
                self.grid_layout.addWidget(self.b2, 1, 2)       
                self.b2.setText("read_key_software")
                self.b2.clicked.connect(read_key_software)
                
                self.b3 = QtWidgets.QPushButton(self)
                self.grid_layout.addWidget(self.b3, 2, 1)      
                self.b3.setText("matherboard")
                self.b3.clicked.connect(matherboard)
                
                self.b4 = QtWidgets.QPushButton(self)
                self.grid_layout.addWidget(self.b4, 2, 2)      
                self.b4.setText("processor")
                self.b4.clicked.connect(processor)
                
                self.l5 = QtWidgets.QLabel(self)
                self.l5.setObjectName('l5')
                self.grid_layout.addWidget(self.l5, 3, 1, 1, 2)  
                self.l5.setText(f"Number of starts: {number}")
    
              
                QtCore.QTimer.singleShot(1000 * 5,     self.end_program)
    #                                    1000 * 60 * 5  --> пять минут           
            else:
                self.end_program()
            
        def end_program(self):
            self.l1 = QtWidgets.QLabel(self)
            self.l1.setObjectName('l1')
            self.l1.setText("Buy the full version of the program!")
            
            self.setCentralWidget(self.l1)
    
    # +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv            
           
            SPLab(blocking=True)                               # !!! <---- 
    
    # +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    Stylesheet = '''
    QWidget {
        background : lightyellow;
    }
    QPushButton {
        background : lightgreen;  
        font-size : 20px;
        width: 250px;
        height: 70px;
    }
    #l1 {
        font-size : 50px;
    }
    #l5 {
        font-size : 20px;
    }
    '''
    
    
    
    if __name__ == "__main__":
        SPLab()
        app = QApplication(sys.argv)
        app.setStyleSheet(Stylesheet)
        mwin = MainWindow()
        mwin.show()
        print("start")
        sys.exit(app.exec_())
    
    • 3

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5