简体   繁体   English

如何为python程序中的函数进行开/关切换?

[英]How to make an on/off switch for a function in a python program?

So I have this python program for CSGO hacks that has esp, aimbot, wallhacks and more!所以我有这个用于 CSGO hacks 的 python 程序,它有 esp、aimbot、wallhacks 等等! Lets take the triggerbot code for example...让我们以 triggerbot 代码为例......

#imports
import pymem
from pymem import process
import os
import sys
import time
import win32
from win32 import win32api
import win32process
import keyboard
import threading
from threading import Thread
import offsets
from offsets import *

def trigger():
    while True:
        localplayer = pm.read_int(client + dwLocalPlayer)
        crosshairid = pm.read_int(localplayer + m_iCrosshairId)
        getteam = pm.read_int(client + dwEntityList + (crosshairid - 1)*0x10)
        localteam = pm.read_int(localplayer + m_iTeamNum)
        crosshairteam = pm.read_int(getteam + m_iTeamNum)

        if crosshairid>0 and crosshairid<32 and localteam != crosshairteam:
            pm.write_int(client + dwForceAttack, 6)

Now I want to be able to give the user an option to turn it on and off via tkinter switch, but I am not able to figure out on how to make it turn off completely once it is turned on.现在我希望能够为用户提供通过 tkinter 开关打开和关闭它的选项,但是我无法弄清楚如何在打开后完全关闭它。

I have tried somethings which I don't want to as they were stupid and also I searched on google couldn't find much.我尝试了一些我不想做的事情,因为它们很愚蠢,而且我在谷歌上搜索也找不到太多东西。

Please help!请帮忙!

Take a look at this example:看看这个例子:

from tkinter import *

root = Tk()

def run():
    global rep
    if var.get() == 1:
        print('Hey')
        rep = root.after(1000,run) #run the function every 2 second, if checked.
    else:
        root.after_cancel(rep) #cancel if the checkbutton is unchecked.

def step():
    print('This is being printed in between the other loop')

var = IntVar()
b1 = Checkbutton(root,text='Loop',command=run,variable=var)
b1.pack()

b2 = Button(root,text='Seperate function',command=step)
b2.pack()

root.mainloop()

after() method takes two arguments mainly: after()方法主要接受两个参数:

  • ms - time to be run the function ms - 运行函数的时间
  • func - the function to run after the given ms is finished. func - 在给定的 ms 完成后运行的函数。
  • after_cancel() method takes the variable name of after() only. after_cancel()方法只采用after_cancel()的变量名。

Perhaps also keep in mind, you can use root.update() and root.update_idletasks() with while loops, but its not efficient either.也许还请记住,您可以将root.update()root.update_idletasks()while循环一起使用,但效率也不高。

Hope this helped you understand better, do let me know if any doubts or errors.希望这能帮助您更好地理解,如果有任何疑问或错误,请告诉我。

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM