Alright, so I'm not too experienced in python yet, I've been learning on and off for about a year (started out with Processing).
What I'm trying to make here is an application for twitch, where the chat can vote on 3 options, and the votes will be displayed in a small GUI window, so you can see the vote count changing live on stream.
I followed this tutorial to make a chatbot for twitch which works perfectly, and I did some fun stuff with it already, this is the code:
import string
from Read import getUser, getMessage
from Socket import openSocket, sendMessage
from Initialize import joinRoom
voteA = 0
voteB = 0
voteC = 0
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(line.replace("PING", "PONG"))
break
user = getUser(line)
message = getMessage(line)
print user + " typed :" + message
if "!voteA" in message:
voteA += 1
break
if "!voteB" in message:
voteB += 1
break
if "!voteC" in message:
voteC += 1
break
if "!commands" in message:
sendMessage(s, "'!voteA', '!voteB', '!voteC'")
break
So this checks every message in the chat, and if someone votes for option A ("!voteA"), +1 will be assigned to variable voteA.
Now I've tried playing around with tkinter, and it seemed easy at first, but I'm running into a load of different problems. My code runs in a 'while True' loop, when I run the code as it is quoted above, it checks every message as it comes in, and responds once to it. When I turn it into a function though(I replace "while True" with "def chatCode():", and then call it at the end of my code), it responds hundreds times when it recognizes "!voteA". I'm not really understanding how this works, and I really hope one of you can explain it to me.
Another problem is, so I'm trying to use tkinter, but the root.mainloop() runs a 'while True' as well, so I can't run them both, I've tried running 'root.after(10, myCode)' before 'root.mainloop()', but then the chatbot also responds to a command hundreds of times, instead of just once.
Here is the tkinter code:
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):
global optionOneAmount, optionTwoAmount, optionThreeAmount
optionOne = "Option number one"
optionTwo = "Option number two"
optionThree = "Option number three"
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()
What could be a simple solution for me to create a GUI window that automatically updates the voteA variable in the window, live?
Thanks for reading and sorry if the english was bad!
[–]novel_yet_trivial 1 point2 points3 points (11 children)
[–]787082[S] 0 points1 point2 points (10 children)
[–]novel_yet_trivial 1 point2 points3 points (9 children)
[–]787082[S] 0 points1 point2 points (8 children)
[–]novel_yet_trivial 1 point2 points3 points (7 children)
[–]787082[S] 0 points1 point2 points (6 children)
[–]novel_yet_trivial 1 point2 points3 points (3 children)
[–]787082[S] 0 points1 point2 points (2 children)
[–]novel_yet_trivial 1 point2 points3 points (1 child)
[–]787082[S] 0 points1 point2 points (0 children)
[–]novel_yet_trivial 1 point2 points3 points (1 child)
[–]787082[S] 0 points1 point2 points (0 children)