Hi there,

got it. Note the root.distroy()-command.

-pekka-

----- CODE STARTS ----

from Tkinter import *
from ScrolledText import ScrolledText
import tkFont

class Message_box:
    # Graphical message box for printing unicode texts

    def __init__(self, myParent):
        self.myContainer1 = Frame(myParent)
        self.myContainer1.pack(side=TOP, expand=1, fill=BOTH)
        self.button1 = Button(self.myContainer1)
        self.button1["text"]= "Close"
        self.button1.pack(side=BOTTOM)
        self.button1.bind("<Button-1>", self.button1Click)
        self.font = tkFont.Font(family="Arial Unicode MS", size=8)
        self.text = ScrolledText(self.myContainer1, font=self.font,\
                         state=NORMAL, height=40, width=120, wrap=NONE)
        self.text.pack(side=TOP, expand=1, fill=BOTH)

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

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

    def enable_write(self):
        self.text.config(state=NORMAL)

    def disable_write(self):
        self.text.config(state=DISABLED)


if __name__ == '__main__': # first window root = Tk() print "blah1" root.title(' Message window') root.protocol("WM_DELETE_WINDOW", NONE) widget = Message_box(root) m = "blah2" widget.write("%s\n" % m) widget.disable_write() root.mainloop() root.destroy() print "blah3" # second window root = Tk() root.title(' Message window') root.protocol("WM_DELETE_WINDOW", NONE) widget = Message_box(root) m = "blah4" widget.write("%s\n" % m) widget.disable_write() root.mainloop() root.destroy() print "blah5"

----- CODE ENDS ----

Pekka Niiranen wrote:
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