Threading With Twisted With Tkinter
I am currently working on a Battleship game project (for learning purposes) that uses tkinter for the UI and, because I want this program to be able to run on two computers for mul
Solution 1:
I took a look at the source code for twisted.internet.tksupport and it appears it has not been ported to support Python 3. The culprit is the line importing the tkinter modules:
import tkSimpleDialog, tkMessageBox
In Python 3 the equivalent would be:
from tkinter import simpledialog, messagebox
To get around this until Twisted supports Tkinter for Python 3, you should be able create your own tksupport module:
# tksupport.py
from tkinter import simpledialog, messagebox
from twisted.internet import task
_task = None
def install(widget, ms=10, reactor=None):
"""Install a Tkinter.Tk() object into the reactor."""
installTkFunctions()
global _task
_task = task.LoopingCall(widget.update)
_task.start(ms / 1000.0, False)
def uninstall():
"""Remove the root Tk widget from the reactor.
Call this before destroy()ing the 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 a slightly modified version of Twisted's Tkinter example you would do:
import tkinter as tk
from twisted.internet import reactor
import tksupport
root = tk.Tk()
# Install the Reactor support
tksupport.install(root)
# at this point build Tk app as usual using the root object,
# and start the program with "reactor.run()", and stop it
# with "reactor.stop()".
Solution 2:
I currently have twisted 17.9.0 and python 3.6. In reference to the answer above, tksupport for python 3 is now available with twisted, so no need to create your own tksupport module.
Post a Comment for "Threading With Twisted With Tkinter"