编辑:解答感谢Furas,我现在正在运行的代码是:
import Tkinter as tk
import tkFileDialog
import string
from Read import getUser, getMessage
from Socket import openSocket, sendMessage
from Initialize import joinRoom
optionOneAmount = 0
def chatCode():
s = openSocket()
joinRoom(s)
readbuffer = ""
readbuffer = readbuffer + s.recv(1024)
temp = string.split(readbuffer, "\n")
readbuffer = temp.pop()
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 user + " typed :" + message
if "!voteOne" in message:
optionOneAmount += 1
break
if "!commands" in message:
sendMessage(s, "dit zijn mn commands lol")
break
app.after(100,chatCode)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.floater = FloatingWindow(self)
class FloatingWindow(tk.Toplevel):
def __init__(self, *args, **kwargs):
optionOne = "Option number one"
optionTwo = "Option number two"
optionThree = "Option number three"
optionOneAmount = 1
optionTwoAmount = 1
optionThreeAmount = 1
tk.Toplevel.__init__(self, *args, **kwargs)
self.overrideredirect(True)
self.configure(background='black')
self.grip = tk.Label(self,bg="gray16", bitmap="gray75")
self.grip.pack(side="left", fill="y")
self.one = tk.Label(self, text=optionOne+" has: "+str(optionOneAmount)+" votes", bg="black", fg="white", font="HouseSlant-Regular 30", anchor="w")
self.one.pack()
self.two = tk.Label(self, text=optionTwo+" has: "+str(optionTwoAmount)+" votes", bg="black", fg="white", font="HouseSlant-Regular 30", anchor="w")
self.two.pack()
self.three = tk.Label(self, text=optionThree+" has: "+str(optionThreeAmount)+" votes", bg="black", fg="white", font="HouseSlant-Regular 30", anchor="w")
self.three.pack()
self.geometry("+0+20")
self.attributes("-alpha", 0.7)
self.grip.bind("<ButtonPress-1>", self.StartMove)
self.grip.bind("<ButtonRelease-1>", self.StopMove)
self.grip.bind("<B1-Motion>", self.OnMotion)
def StartMove(self, event):
self.x = event.x
self.y = event.y
def StopMove(self, event):
self.x = None
self.y = None
def OnMotion(self, event):
deltax = event.x - self.x
deltay = event.y - self.y
x = self.winfo_x() + deltax
y = self.winfo_y() + deltay
self.geometry("+%s+%s" % (x, y))
app=App()
app.after(100, chatCode)
app.mainloop()
我在编程方面相对较新,我已经玩python大约一年了。我想要实现的是制作一个抽签式投票系统,并连接一个小的GUI。
这个想法是,抽搐聊天的人可以键入&#34;!voteOne&#34; &#34;!voteTwo&#34;或&#34;!voteThree&#34;,聊天机器人将如此注册投票:
if "!voteOne" in message:
optionOneAmount += 1
所以我一直在玩一个颤抖的聊天机器人教程,它工作得很好,非常有趣,并帮助我学习操纵聊天中的字符串/输入。
问题出在哪里,就是我希望有一个小GUI,使用该程序的流媒体可以在流上,每个人都可以看到每个选项有多少票。我尝试使用tkinter来获得一个漂亮的无边框/无框GUI窗口,我用这段代码调用它:
import Tkinter as tk
import tkFileDialog
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.floater = FloatingWindow(self)
class FloatingWindow(tk.Toplevel):
def __init__(self, *args, **kwargs):
optionOne = "Option number one"
optionTwo = "Option number two"
optionThree = "Option number three"
optionThreeAmount = 1
optionTwoAmount = 1
optionThreeAmount = 1
tk.Toplevel.__init__(self, *args, **kwargs)
self.overrideredirect(True)
self.configure(background='black')
self.grip = tk.Label(self,bg="gray16", bitmap="gray75")
self.grip.pack(side="left", fill="y")
self.one = tk.Label(self, text=optionOne+" has: "+str(optionOneAmount)+" votes", bg="black", fg="white", font="HouseSlant-Regular 30", anchor="w")
self.one.pack()
self.two = tk.Label(self, text=optionTwo+" has: "+str(optionTwoAmount)+" votes", bg="black", fg="white", font="HouseSlant-Regular 30", anchor="w")
self.two.pack()
self.three = tk.Label(self, text=optionThree+" has: "+str(optionThreeAmount)+" votes", bg="black", fg="white", font="HouseSlant-Regular 30", anchor="w")
self.three.pack()
self.geometry("+0+20")
self.attributes("-alpha", 0.7)
self.grip.bind("<ButtonPress-1>", self.StartMove)
self.grip.bind("<ButtonRelease-1>", self.StopMove)
self.grip.bind("<B1-Motion>", self.OnMotion)
def StartMove(self, event):
self.x = event.x
self.y = event.y
def StopMove(self, event):
self.x = None
self.y = None
def OnMotion(self, event):
deltax = event.x - self.x
deltay = event.y - self.y
x = self.winfo_x() + deltax
y = self.winfo_y() + deltay
self.geometry("+%s+%s" % (x, y))
app=App()
app.mainloop()
现在我有了其他代码,即读取聊天机器人的代码,我希望该代码更新tkinter代码中的optionOne,optionTwo和optionThree变量。
问题,据我所知,app.mainloop()运行一个无限循环,我的聊天机器人也使用无限循环,它们不能同时运行。
所以我的问题是,如何在tkinter代码中组合chatbot中的代码?
聊天机器人代码如下:
import string
from Read import getUser, getMessage
from Socket import openSocket, sendMessage
from Initialize import joinRoom
s = openSocket()
joinRoom(s)
readbuffer = ""
while True:
readbuffer = readbuffer + s.recv(1024)
temp = string.split(readbuffer, "\n")
readbuffer = temp.pop()
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 user + " typed :" + message
if "!voteOne" in message:
optionOneAmount += 1
break
(聊天机器人代码有更多标签,如设置,阅读,初始化和套接字,如果需要,我也可以发布它们,但我不认为它们现在相关吗?)
我怎样才能将这两个程序结合起来?感谢您花时间阅读,对不起,如果英语不好! (请耐心等待我,因为我只是想学习:))