HELP: Tkinter idiom needed

Pekka Niiranen pekka.niiranen at
Sat Jan 1 09:17:54 EST 2005

Hi there,

after reading TkInter/thread -recipe:
I wondered if it was possible to avoid using threads
for the following problem:

I have script started from W2K console that normally
prints ascii messages to the screen. However, I have
command line "debug" -flag that might cause printing
of UTF-8 data to the screen. This fails occassionally
due to Console encoding, of course.

What I need is Tkinter window where some printouts
are directed when script is run in Debug -mode
(Redirection of stdout is out of question).

While testing, I have got this far already:

---script starts----

from Tkinter import *
from ScrolledText import ScrolledText
import os, sys, tkFont, codecs

class Pyg_message_box:
   def __init__(self, parent):
     self.myParent = parent
     self.myContainer1 = Frame(parent)
	tkFont.Font(family="Arial Unicode MS", size=8))
     self.text = ScrolledText()
     self.button1 = Button(self.myContainer1, text="Quit",\
     self.button1.bind("<Button-1>", self.button1Click)

   def button1Click(self, event):

   def write(self, s):
     self.text.insert(END, s)

root = Tk()
widget = Pyg_message_box(root)
sys.stdout = widget
a ="d:\\test.txt", "r", "utf_16").readlines()
for x in a:
   print x

---script ends----

My questions are:
- Can I open Tk -window without enclosing the whole script
   between "root=Tk()" and "root.mainloop()"?
- What is the idiom of opening Tk -window only when Debug -flag
   is encountered (the script stops running until window is closed)?

Something like:

if not my_debug == "ON":
	print "message" # prints to console
	# 1) open temporary ScrolledText() Tk -window
	# 2) Print stuff to window
	# 3) Ask user to close window

I would no like to always open Tkwindows just in case user runs script
with debug -flag on.


More information about the Python-list mailing list