Hi there,

after reading TkInter/thread -recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965
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)
    self.myContainer1.option_add("*font",\
        tkFont.Font(family="Arial Unicode MS", size=8))
    self.myContainer1.pack()
    self.text = ScrolledText()
    self.text.pack()
    self.button1 = Button(self.myContainer1, text="Quit",\
                     command=self.button1Click)
    self.button1.pack(side=LEFT)
    self.button1.bind("<Button-1>", self.button1Click)

  def button1Click(self, event):
     self.myContainer1.quit()

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

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

---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
else:
        # 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.

-pekka-
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to