我制作了一个简单的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()
Main Bot;
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
答案 0 :(得分:0)
由于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
对象是一个线程,它在启动时使用参数execfile
运行'GUI.py'
可调用(在本例中为函数)。
查看线程模块文档以获取更多信息:https://docs.python.org/3.3/library/threading.html