python - Threading with Twisted with Tkinter -
i working on battleship game project (for learning purposes) uses tkinter ui and, because want program able run on 2 computers multiplayer, twisted data transfer. first time using twisted have used tkinter many times.
i know both twisted , tkinter run in loops not possible have these running in same thread. found out there's 2 ways around this: tksupport
, running twisted's reactor
in separate thread.
however, tried import tksupport
twisted.internet
said didn't exist. checked twisted folder in site-packages sure , indeed not there, twisted docs claim is. assuming because running python 3.5 , tksupport
hasn't been ported on yet, if not case, please let me know.
also, solution threading, discovered there's controversy on putting twisted's reactor in it's own thread. is ok put reactor
in own thread, and, if so, precautions should take?
thanks.
i took @ source code twisted.internet.tksupport , appears has not been ported support python 3. culprit line importing tkinter modules:
import tksimpledialog, tkmessagebox
in python 3 equivalent be:
from tkinter import simpledialog, messagebox
to around until twisted supports tkinter python 3, should able create own tksupport module:
# tksupport.py tkinter import simpledialog, messagebox twisted.internet import task _task = none def install(widget, ms=10, reactor=none): """install tkinter.tk() object reactor.""" installtkfunctions() global _task _task = task.loopingcall(widget.update) _task.start(ms / 1000.0, false) def uninstall(): """remove root tk widget reactor. call before destroy()ing root widget. """ global _task _task.stop() _task = none def installtkfunctions(): import twisted.python.util twisted.python.util.getpassword = getpassword def getpassword(prompt = '', confirm = 0): while 1: try1 = simpledialog.askstring('password dialog', prompt, show='*') if not confirm: return try1 try2 = simpledialog.askstring('password dialog', 'confirm password', show='*') if try1 == try2: return try1 else: messagebox.showerror('password mismatch', 'passwords did not match, starting over') __all__ = ["install", "uninstall"]
and following modified version of twisted's tkinter example do:
import tkinter tk twisted.internet import reactor import tksupport root = tk.tk() # install reactor support tksupport.install(root) # @ point build tk app usual using root object, # , start program "reactor.run()", , stop # "reactor.stop()".
Comments
Post a Comment