Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > HELP: Tkinter idiom needed

Reply
Thread Tools

HELP: Tkinter idiom needed

 
 
Pekka Niiranen
Guest
Posts: n/a
 
      01-01-2005
Hi there,

after reading TkInter/thread -recipe:
http://aspn.activestate.com/ASPN/Coo...n/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=)
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-
 
Reply With Quote
 
 
 
 
Pekka Niiranen
Guest
Posts: n/a
 
      01-03-2005
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=
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/Coo...n/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=)
> 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-

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dictionary idiom needed Brandon Python 6 12-11-2008 09:32 PM
from Tkinter import *,win = Tk() "from Tkinter import *" Pierre Dagenais Python 0 08-03-2008 10:33 PM
Help needed: try/except -idiom vs. C-style coding pekka niiranen Python 1 10-11-2004 02:14 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-23-2004 02:31 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-20-2004 12:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57