首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在运行脚本的正常函数时运行tKinter?

如何在运行脚本的正常函数时运行tKinter?
EN

Stack Overflow用户
提问于 2022-03-15 10:16:30
回答 1查看 77关注 0票数 0

因此,我制作了一个脚本,它检测屏幕中的像素颜色,当它检测到正确的窗口时,将按键发送到选定的窗口。我做了一个热键来启动和停止这个探测像素的小循环,然后我决定让它成为一个exe,并配置热键,我开始使用tKinter,并在其中设置了一个窗口和一个标签,直到我发现脚本甚至不能正常工作,或者除了显示窗口之外(甚至按下热键)。然后,我移动root.mainloop(),将所有脚本代码放在root = tk.Tk()root.mainloop()之间,没有任何函数。任何人都有任何解决方案使脚本仍然可以使用UI。另外,也许是一种读取热键的方法,即使窗口没有焦点(尽管我认为它无论如何都会工作,但万一tKinter可能不允许)。

按照要求,下面是打开窗口但脚本不能正常工作的前面的代码

代码语言:javascript
复制
from pyautogui import *
import pyautogui
import time
import random
import keyboard
import win32api, win32con
import win32com.client
from pywinauto import win32defines 
from pywinauto.application import Application
import tkinter as tk


root = tk.Tk()
root.title('Fisher Version 1.0')
canvas = tk.Canvas(root, width=150, height=200)
canvas.grid(columnspan=1)

hotkeyLabel = tk.Label(root, text="Key for start/stop")
hotkeyLabel.grid(columnspan=1, column=0, row=0)

root.mainloop()

app = Application().connect(title_re="FiveM")
win = app.window(title_re = "FiveM")

#GREEN ZONE X:  959 Y:  337 RGB: (106, 150,   1)
#G ZONE region=(40,23,300,30)
run = False
#keyRod = ['rodKey']
#keyFood = ['foodKey']
#keyDrink = ['drinkKey']
#keyHot = ['hotKey']





def autofisher():
    while True:
        if pyautogui.locateOnScreen('pressg.png', region=(305,23,30,30), grayscale=False, confidence=0.7) !=None: #detects if g prompt shows up
            #shell.SendKeys("g")
            win.send_keystrokes('g')
            #print("YEssssS ITS HERE") #test for detecting fish prompt
            time.sleep(1)
        
        r, g, b = pyautogui.pixel(959, 337) #detects the green zone
        a, c, d = pyautogui.pixel(959, 340) 
        if 100 <= r <= 110 and 145 <= g <= 155 and 0 <= b <= 5: #detects if in green is showing
            #print("hi", r, g, b)
            #print("ijhabd", a, c, d)
            #win.send_keystrokes('e')
            for y in range(338, 350, 1): #detects if line in range
            #for x in range(954, 964):
                j, k, l = pyautogui.pixel(959, y)
                if 0 <= k <= 50 and 0 <= k <= 50:
                    #print("cauight") #test for detecting the black
                    #shell.SendKeys("e")
                    win.send_keystrokes('e')

while True:           
    if keyboard.is_pressed('z'):
        print("kijanbdsjokn")
        run = not run
    if keyboard.is_pressed('k'):
        #win.send_keystrokes(keyBind)
        print("keyBind")

while run == True:
    autofisher()

        
while True:
    time.sleep(60)
    # thing to run
    q, w, e = pyautogui.pixel(209, 1042) #detects the hunger   
    if 55 <= q <= 70 and 55 <= w <= 70 and 55 <= e <= 70:
        #press 3 for food
        win.send_keystrokes('2')
        time.sleep(10)
        #press 1 for fishing rod
        #send_keystrokes('1')
    u, i, o = pyautogui.pixel(287, 1056) #detects the thirst
    if 55 <= u <= 70 and 55 <= i <= 70 and 55 <= o <= 70:
        #send_keystrokes('2')
        #press 2 for water
        time.sleep(10)
        win.send_keystrokes('3')
        #press 1 for fishing rod

这是修改过的

代码语言:javascript
复制
from cgitb import text
from pyautogui import *
import pyautogui
import time
import random
import keyboard
import win32api, win32con
import win32com.client
from pywinauto import win32defines 
from pywinauto.application import Application
import tkinter as tk


root = tk.Tk()
root.title('Fisher Version 1.0')
canvas = tk.Canvas(root, width=150, height=200)
canvas.grid(columnspan=1)

hotkeyLabel = tk.Label(root, text="Key for start/stop")
hotkeyLabel.grid(columnspan=1, column=0, row=0)

app = Application().connect(title_re="FiveM")
win = app.window(title_re = "FiveM")

#GREEN ZONE X:  959 Y:  337 RGB: (106, 150,   1)
#G ZONE region=(40,23,300,30)
run = False
#keyRod = ['rodKey']
#keyFood = ['foodKey']
#keyDrink = ['drinkKey']
#keyHot = ['hotKey']





def autofisher():
    while True:
        if pyautogui.locateOnScreen('pressg.png', region=(305,23,30,30), grayscale=False, confidence=0.7) !=None: #detects if g prompt shows up
            #shell.SendKeys("g")
            win.send_keystrokes('g')
            #print("YEssssS ITS HERE") #test for detecting fish prompt
            time.sleep(1)
        
        r, g, b = pyautogui.pixel(959, 337) #detects the green zone
        a, c, d = pyautogui.pixel(959, 340) 
        if 100 <= r <= 110 and 145 <= g <= 155 and 0 <= b <= 5: #detects if in green is showing
            #print("hi", r, g, b)
            #print("ijhabd", a, c, d)
            #win.send_keystrokes('e')
            for y in range(338, 350, 1): #detects if line in range
            #for x in range(954, 964):
                j, k, l = pyautogui.pixel(959, y)
                if 0 <= k <= 50 and 0 <= k <= 50:
                    #print("cauight") #test for detecting the black
                    #shell.SendKeys("e")
                    win.send_keystrokes('e')

while True:           
    if keyboard.is_pressed('z'):
        print("kijanbdsjokn")
        run = not run
    if keyboard.is_pressed('k'):
        #win.send_keystrokes(keyBind)
        print("keyBind")

while run == True:
    autofisher()

        
while True:
    time.sleep(60)
    # thing to run
    q, w, e = pyautogui.pixel(209, 1042) #detects the hunger   
    if 55 <= q <= 70 and 55 <= w <= 70 and 55 <= e <= 70:
        #press 3 for food
        win.send_keystrokes('2')
        time.sleep(10)
        #press 1 for fishing rod
        #send_keystrokes('1')
    u, i, o = pyautogui.pixel(287, 1056) #detects the thirst
    if 55 <= u <= 70 and 55 <= i <= 70 and 55 <= o <= 70:
        #send_keystrokes('2')
        #press 2 for water
        time.sleep(10)
        win.send_keystrokes('3')
        #press 1 for fishing rod

root.mainloop()
EN

回答 1

Stack Overflow用户

发布于 2022-03-15 11:20:40

告诉我这是否有效:

代码语言:javascript
复制
import pyautogui
import time
import keyboard
from pywinauto.application import Application
import tkinter as tk
import threading
root = tk.Tk()
root.title('Fisher Version 1.0')
canvas = tk.Canvas(root, width=150, height=200)
canvas.grid(columnspan=1)

hotkeyLabel = tk.Label(root, text="Key for start/stop")
hotkeyLabel.grid(columnspan=1, column=0, row=0)

app = Application().connect(title_re="FiveM")
win = app.window(title_re="FiveM")

# GREEN ZONE X:  959 Y:  337 RGB: (106, 150,   1)
# G ZONE region=(40,23,300,30)
run = True


# keyRod = ['rodKey']
# keyFood = ['foodKey']
# keyDrink = ['drinkKey']
# keyHot = ['hotKey']


def autofisher():
    while True:
        if pyautogui.locateOnScreen('pressg.png', region=(305, 23, 30, 30), grayscale=False,
                                    confidence=0.7) != None:  # detects if g prompt shows up
            # shell.SendKeys("g")
            win.send_keystrokes('g')
            # print("YEssssS ITS HERE") #test for detecting fish prompt
            time.sleep(1)

        r, g, b = pyautogui.pixel(959, 337)  # detects the green zone
        a, c, d = pyautogui.pixel(959, 340)
        if 100 <= r <= 110 and 145 <= g <= 155 and 0 <= b <= 5:  # detects if in green is showing
            # print("hi", r, g, b)
            # print("ijhabd", a, c, d)
            # win.send_keystrokes('e')
            for y in range(338, 350, 1):  # detects if line in range
                # for x in range(954, 964):
                j, k, l = pyautogui.pixel(959, y)
                if 0 <= k <= 50 and 0 <= k <= 50:
                    # print("cauight") #test for detecting the black
                    # shell.SendKeys("e")
                    win.send_keystrokes('e')

def keyboard_press():
    global run
    while True:
        if keyboard.is_pressed('z'):
            print("kijanbdsjokn")
            run = not run
        if keyboard.is_pressed('k'):
            # win.send_keystrokes(keyBind)
            print("keyBind")

def runFunction():
    while run == True:
        autofisher()

def main_thing():
    while True:
        time.sleep(60)
        # thing to run
        q, w, e = pyautogui.pixel(209, 1042)  # detects the hunger
        if 55 <= q <= 70 and 55 <= w <= 70 and 55 <= e <= 70:
            # press 3 for food
            win.send_keystrokes('2')
            time.sleep(10)
            # press 1 for fishing rod
            # send_keystrokes('1')
        u, i, o = pyautogui.pixel(287, 1056)  # detects the thirst
        if 55 <= u <= 70 and 55 <= i <= 70 and 55 <= o <= 70:
            # send_keystrokes('2')
            # press 2 for water
            time.sleep(10)
            win.send_keystrokes('3')
            # press 1 for fishing rod

def starts():
    autofisherT = threading.Thread(target=autofisher)
    keyboardT = threading.Thread(target=keyboard_press)
    runT = threading.Thread(target=runFunction)
    mainThingT = threading.Thread(target=main_thing)

    autofisherT.start()
    keyboardT.start()
    runT.start()
    mainThingT.start()

root.mainloop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71480470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档