473,724 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.myContaine r1 = Frame(parent)
self.myContaine r1.option_add(" *font",\
tkFont.Font(fam ily="Arial Unicode MS", size=8))
self.myContaine r1.pack()
self.text = ScrolledText()
self.text.pack( )
self.button1 = Button(self.myC ontainer1, text="Quit",\
command=self.bu tton1Click)
self.button1.pa ck(side=LEFT)
self.button1.bi nd("<Button-1>", self.button1Cli ck)

def button1Click(se lf, event):
self.myContaine r1.quit()

def write(self, s):
self.text.inser t(END, s)

root = Tk()
widget = Pyg_message_box (root)
sys.stdout = widget
a = codecs.open("d: \\test.txt", "r", "utf_16").readl ines()
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 2249
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.myContaine r1 = Frame(myParent)
self.myContaine r1.pack(side=TO P, expand=1, fill=BOTH)
self.button1 = Button(self.myC ontainer1)
self.button1["text"]= "Close"
self.button1.pa ck(side=BOTTOM)
self.button1.bi nd("<Button-1>", self.button1Cli ck)
self.font = tkFont.Font(fam ily="Arial Unicode MS", size=8)
self.text = ScrolledText(se lf.myContainer1 , font=self.font, \
state=NORMAL, height=40, width=120, wrap=NONE)
self.text.pack( side=TOP, expand=1, fill=BOTH)

def button1Click(se lf, event):
self.myContaine r1.quit()

def write(self,s):
self.text.inser t(END, s)

def enable_write(se lf):
self.text.confi g(state=NORMAL)

def disable_write(s elf):
self.text.confi g(state=DISABLE D)
if __name__ == '__main__':
# first window
root = Tk()
print "blah1"
root.title(' Message window')
root.protocol(" WM_DELETE_WINDO W", NONE)
widget = Message_box(roo t)
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_WINDO W", NONE)
widget = Message_box(roo t)
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.myContaine r1 = Frame(parent)
self.myContaine r1.option_add(" *font",\
tkFont.Font(fam ily="Arial Unicode MS", size=8))
self.myContaine r1.pack()
self.text = ScrolledText()
self.text.pack( )
self.button1 = Button(self.myC ontainer1, text="Quit",\
command=self.bu tton1Click)
self.button1.pa ck(side=LEFT)
self.button1.bi nd("<Button-1>", self.button1Cli ck)

def button1Click(se lf, event):
self.myContaine r1.quit()

def write(self, s):
self.text.inser t(END, s)

root = Tk()
widget = Pyg_message_box (root)
sys.stdout = widget
a = codecs.open("d: \\test.txt", "r", "utf_16").readl ines()
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
1599
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 alternatives will be greatly appreciated. <code> #!env python # # created: "15:19 20/01-2004" by Kim Petersen <kim@vindinggaard.dk>
7
3151
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 python source, test.py ------------------ from graphics import * def main():
44
4995
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 processed by this script, where the concerned tmp file is only actually written to when PythonWin is closed. In other words, after I run this script, one of the generated tmp files has a size of 0kB. I then close PythonWin and it is then written to.
8
3287
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 my pc. But, it is not getting installed and is failing by throwing the below errors. Should i need to configure / install any specific files for resolving this issue ?
0
8868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9388
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9160
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4764
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3209
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2612
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.