我试图制作一个抽搐的程序,例如,通过输入命令("!voteA"),抽搐聊天可以投票,而聊天机器人代码将计算投票,并且在GUI窗口中显示它们,我已经相当远了,但是我的GUI窗口没有打开,我认为它与self.after(10,self.get_votes)行有关,任何人都可以帮忙我出去了吗?
这里是代码:(它有更多标签,如阅读,套接字,初始化,设置,但我不认为他们现在相关,如果他们是我也可以发布它们)
import Tkinter as tk
from Read import getUser, getMessage
from Socket import openSocket, sendMessage
from Initialize import joinRoom
from collections import OrderedDict
class App(tk.Frame): #Toplevel is a frame with a specific use. Unless there is another window, you want a Frame.
def __init__(self):
options = [
"voteA",
"voteB",
"voteC"]
self.s = openSocket()
joinRoom(self.s)
self.readbuffer = ""
tk.Frame.__init__(self)
self['background']='black'
self.pack()
self.master.geometry("+200+200")
# self.master.attributes("-alpha", 0.7)
self.labels = OrderedDict()
for option in options:
label = tk.Label(
self,
bg="black",
fg="white",
font="HouseSlant-Regular 30",
anchor="w")
label.pack()
self.labels[option] = [0, label]
self.update_text()
self.after(10, self.get_votes)
def update_text(self):
for name, data in self.labels.items():
count, label = data
label['text'] = "{} has {} votes".format(name, count)
def get_votes(self):
self.after(10, self.get_votes)
self.readbuffer = self.readbuffer + self.s.recv(1024)
temp = self.readbuffer.split('\n')
self.readbuffer = temp.pop() #save the last (possibly incomplete) line for later
if self.readbuffer == "":
pass #no further messages in the queue
#you should add a sleep time here for the sake of your hot NIC
for line in temp:
print(line)
if "PING" in line:
s.send("PONG :tmi.twitch.tv\r\n".encode())
break
user = getUser(line)
message = getMessage(line)
print "{} typed: {}".format(user, message)
if "!commands" in message:
sendMessage(self.s, " ".join(["!"+option for option in self.labels]))
break
for option in self.labels:
if "!"+option in message:
self.labels[option][0] += 1
print self.labels[option][0]
self.update_text()
app=App()
app.mainloop()
运行代码时会发生什么,聊天机器人的工作原理是什么;如果我输入"!命令"或"!voteA"它会很好地响应,但GUI窗口不会打开。我可以在我的Dock(我是一个mac用户)中看到它试图打开一个窗口,但它冻结然后我必须强行退出它。它虽然没有在编译器中给出错误。有没有人知道我搞砸了什么?