I am retired and trying to get to grips with Python (as you can probably tell from the code!) and am trying to program a simple game using a laptop and tablet (both running Python 3.3.5 under Windows 8).
My problem in a nutshell is this:
I have two simple working programs (below): one waits for a button click and changes the screen colour, the other waits for a message from a comms client and changes the screen colour.
What's the simplest way to combine these into one ongoing program, so we are waiting for either screen or comms activity which may come in any order?
I've tried various ways I've discovered on this site and others around threading and waiting, but none seem ideal. I would appreciate any help!
# PROGRAM 1 - wait for user to click button
from tkinter import *
win1 = Tk()
win1.attributes('-fullscreen', True)
def turnorange():
win1.configure(bg='orange')
def turnwhite():
win1.configure(bg='white')
btn1 = Button( win1 , text='Click for orange' , command=turnorange)
btn1.place(x=100,y=100)
btn2 = Button( win1 , text='Click for white' , command=turnwhite)
btn2.place(x=250,y=100)
btn_end = Button( win1 , text='Close' , command=exit)
btn_end.place(x=200,y=200)
win1.mainloop()
# PROGRAM 2 - wait for message from client
from tkinter import *
import socket
win1 = Tk()
win1.attributes('-fullscreen', True)
def changecolour(col):
win1.configure(bg=col)
win1.update()
def startup():
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.bind((socket.gethostname(), 8089))
ss.listen(5)
while True:
conn, address = ss.accept()
colour = conn.recv(64).decode()
if len(colour) > 0:
changecolour(colour)
data = 'whatever'
conn.send(data.encode())
btn = Button( win1 , text='Click to listen' , command=startup)
btn.place(x=100,y=100)
win1.mainloop()