472,805 Members | 820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

HELP: Tkinter idiom needed

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=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-
Jul 18 '05 #1
1 2187
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/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=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-

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kim Petersen | last post by:
I've worked on this table object a bit too long - and seem to have stared too long at the code. Can someone see where it goes wrong in the insertrow function? Also optimization hints and...
7
by: John Slimick | last post by:
I want to do a little Tkinter in my 1 credit python practicum, but I am having problems getting everything installed correctly. A sample of the problem is below: ------------------- The...
44
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file...
8
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.