簡體   English   中英

如何為python程序中的函數進行開/關切換?

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

所以我有這個用於 CSGO hacks 的 python 程序,它有 esp、aimbot、wallhacks 等等! 讓我們以 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)

現在我希望能夠為用戶提供通過 tkinter 開關打開和關閉它的選項,但是我無法弄清楚如何在打開后完全關閉它。

我嘗試了一些我不想做的事情,因為它們很愚蠢,而且我在谷歌上搜索也找不到太多東西。

請幫忙!

看看這個例子:

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()方法主要接受兩個參數:

  • ms - 運行函數的時間
  • func - 在給定的 ms 完成后運行的函數。
  • after_cancel()方法只采用after_cancel()的變量名。

也許還請記住,您可以將root.update()root.update_idletasks()while循環一起使用,但效率也不高。

希望這能幫助您更好地理解,如果有任何疑問或錯誤,請告訴我。

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM