簡體   English   中英

執行文件后繼續執行代碼-Python

[英]Continuing code after executing a File - Python

我制作了一個簡單的GUI,可以在運行Twitch TV IRC Bot時啟動。 但是,直到我關閉GUI之后,主機器人才繼續運行。 我將如何使腳本與GUI同時運行? 這是GUI:

##--GUI--##
def whitelist_wipe():
execfile('command_whitelist_wipe.py')

def modpack():
modpack = modpack_input.get()
return



root = Tk()
modpack_input = StringVar()

root.title('Geekster_Bot')
root.geometry('450x450+500+300')

WhitelistButton = Button(root, text = 'Clear Whitelist!', command + whitelist_wipe).pack()

SubmitButton = Button(root, text = 'Change Modpack', command = modpack).pack()

ModpackName = Entry(root, textvariable=modpack_input).pack()

root.mainloop()

這是從主Bot scrpit啟動GUI的部分:

try:
    execfile('GUI.py')

except:
    print('Error loading GUI')

在GUI打開的情況下如何繼續代碼?

編輯

GUI.py;

##--GUI--##
def whitelist_wipe():
    execfile('command_whitelist_wipe.py')

def modpack_var():
    modpack_input = modpack_user.get()



root = Tk()
modpack_user = StringVar()

root.title('Geekster_Bot')
root.geometry('350x100+500+300')

Label1 = Label(root, text = 'Geekster_Bot Controls!').pack()

WhitelistButton = Button(root, text = 'Clear Whitelist!', command =whitelist_wipe).pack(side = LEFT)


SubmitButton = Button(root, text = 'Change Modpack', command = modpack_var).pack()

ModpackName = Entry(root, textvariable=modpack_user).pack()

root.mainloop()

主機器人

try:
    #Create thread
    gui_thread = threading.Thread( target = execfile, args = ('GUI.py',) )
    #Start thread
    gui_thread.start()
    #Code to run in the main thread

except:
    print('Error loading GUI')

def message(msg): #function for sending messages to the IRC chat
            global queue
            queue = queue + 1
            print queue
            if queue < 20: #ensures does not send >20 msgs per 30 seconds.
                twitch = ('OUTPUT ON ' + channel + ' :' + msg + '\r\n')
                irc.send('PRIVMSG ' + channel + ' :' + msg + '\r\n')
                print (twitch)
            else:
                print 'Message deleted'

def socialtimer(): #function for announcing social every 3 minutes
            global ntimer
            z = open(r'E:\Geekster_Bot\Twitter.txt')
            SOCIAL = z.read()
            message (SOCIAL)
            print 'Social Timers Started!'
            ntimer = threading.Timer(1200,socialtimer)
            ntimer.start()

def queuetimer(): #function for resetting the queue every 30 seconds
            global queue
            print 'queue reset'
            queue = 0
            threading.Timer(30,queuetimer).start()

def modpack_var():
            modpack_input = modpack_user.get()

##--Main Bot--##

#General variables
newsmsg = 'whitelist'
modpack = modpack_input

由於gui本身在循環中運行,因此它會阻塞您啟動它的線程,直到您停止循環(關閉gui)。 您需要在與主線程不同的線程中啟動gui。

您可以使用線程模塊執行此操作:

import threading

try:
    #Create thread
    gui_thread = threading.Thread( target = execfile, args = ('GUI.py',) )
    #Start thread
    gui_thread.start()
    #Code to run in the main thread

except:
    print('Error loading GUI')

在此代碼中, gui_thread對象是一個線程,該線程在啟動時使用參數'GUI.py'運行可調用的execfile (在這種情況下為函數)。 檢查線程模塊文檔以獲取更多信息: https : //docs.python.org/3.3/library/threading.html

暫無
暫無

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

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