From michael.odonnell at uam.es Sat Mar 1 13:05:10 2014 From: michael.odonnell at uam.es (Michael O'Donnell) Date: Sat, 1 Mar 2014 13:05:10 +0100 Subject: [Tkinter-discuss] Tracebacks In-Reply-To: <3766A980-82BA-4AA0-9F1F-5822C79AF1E1@passcal.nmt.edu> References: <3766A980-82BA-4AA0-9F1F-5822C79AF1E1@passcal.nmt.edu> Message-ID: Hi Bob, When my program starts up, I run: outstr=codecs.open(logfile, "w", 'utf-8') sys.stderr = outstr sys.stdout = outstr That should do what you want. The stream will close when the program closes. Takes stderr AND normal print, but you could drop the line for stdout mick On 28 February 2014 16:33, Bob Greschke wrote: > This isn't quite a Tkinter thing, but why can't some clever person (I > couldn't do it) make it possible for you to put something like this > > #! /usr/bin/python -t "/home/me/error.txt",stderr > > at the beginning of a Python program and have the interpreter direct all > of the traceback messages to the file error.txt, and stderr? I've got a > 43,000 line Tkinter/via X11 program and stupid users that won't tell me > when it crashes -- if they even notice. Right now we create an xterm on > their computer then execute and ssh to the server where the program > resides. The errors come out in that xterm, but if the users quit they are > gone. It seems like the interpreter would be the smartest place to put > something like this since it controls everything, and so you don't have to > try and guess in which section of code you misspelled a variable name and > put it in a try-except. > > Isn't this reasonable? There are all kinds of questions about this > problem, but never any really good solutions. I like this one. :) I just > wouldn't have any idea how to do it, or if it's even possible. > > Bob > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: From memilanuk at gmail.com Tue Mar 11 03:55:40 2014 From: memilanuk at gmail.com (memilanuk) Date: Mon, 10 Mar 2014 19:55:40 -0700 Subject: [Tkinter-discuss] tkRAD - XML widget building for Tkinter In-Reply-To: <1392643749304-5047808.post@n6.nabble.com> References: <1392643749304-5047808.post@n6.nabble.com> Message-ID: Just saw this here... looks pretty interesting! From memilanuk at gmail.com Tue Mar 11 04:27:18 2014 From: memilanuk at gmail.com (memilanuk) Date: Mon, 10 Mar 2014 20:27:18 -0700 Subject: [Tkinter-discuss] importing tk.W? Message-ID: So... this is kind of twisting my brain a bit. Not sure I understand how/why this works the way it does. Some help or explanation would be appreciated. On the one hand, I keep reading about the evils/perils of wildcard imports and recommendations for named imports. On the other hand, it seems like nearly every tkinter example in books, guides, tutorials that I see uses a wildcard import. So I was trying to follow along some code examples from a recent book, and also trying to use python3 as much as practical. Given that the original book code was python 2.x and I had to change 'Tkinter' to 'tkinter', I figured why not change to using a named import, i.e. import tkinter as tk rather than from Tkinter import * Other than having to import a 'tk.' to widget names, I figured I should be good to go. Unfortunately not. The book had code like this: from tkinter import * root = Tk() Label(root, text="Username").grid(row=0, sticky=W) While what I was coming up with was more like this: import tkinter as tk root = tk.Tk() tk.Label(root, text="Username").grid(row=0, sticky='W') The difference being that unless I enclosed the W attribute for the option 'sticky=' in quotes, I got an error 'Unresolved reference 'W''. I asked online, and found that apparently 'W' is imported along with 'Label', 'Entry', 'Button', 'Tk', etc. at the top level of tkinter. It appears I can use either a wildcard import, enclose it in quotes, or use explicit reference e.g. tk.W. My question is this: How / why does this make any sense, that an attribute (W) to an option (sticky=) would be imported at the same level as widgets themselves? Is it some artifact left over from tcl, or just a bad idea that never went away? Between having to explicitly reference every widget already (tk.Label) and having to be extra specific with options like sticky... I'm starting to see why everyone uses wildcard imports when it comes to tkinter! Thanks, Monte From greg.ewing at canterbury.ac.nz Tue Mar 11 05:48:29 2014 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Tue, 11 Mar 2014 17:48:29 +1300 Subject: [Tkinter-discuss] importing tk.W? In-Reply-To: References: Message-ID: <531E959D.30207@canterbury.ac.nz> memilanuk wrote: > My question is this: How / why does this make any sense, that an > attribute (W) to an option (sticky=) would be imported at the same level > as widgets themselves? Tkinter is a *very* old library, written before the best practices for this kind of thing were sorted out. If it were being designed today, things would probably have been done differently. Anyway, there's no great problem if you avoid * imports and prefix everything with tk. -- Greg From michael.odonnell at uam.es Tue Mar 11 07:55:51 2014 From: michael.odonnell at uam.es (Michael O'Donnell) Date: Tue, 11 Mar 2014 07:55:51 +0100 Subject: [Tkinter-discuss] importing tk.W? In-Reply-To: References: Message-ID: Hi Monte, When you import all from tkinter, you get a number of named constants as well as the classes (constants being variables with a fixed value). So, don't think of them as attributes, think of them as variables, which tkinter defines like: tk.W='w' tk.BOTH='both' etc. Mick On 11 March 2014 04:27, memilanuk wrote: > So... this is kind of twisting my brain a bit. Not sure I understand > how/why this works the way it does. Some help or explanation would be > appreciated. > > On the one hand, I keep reading about the evils/perils of wildcard imports > and recommendations for named imports. On the other hand, it seems like > nearly every tkinter example in books, guides, tutorials that I see uses a > wildcard import. > > So I was trying to follow along some code examples from a recent book, and > also trying to use python3 as much as practical. Given that the original > book code was python 2.x and I had to change 'Tkinter' to 'tkinter', I > figured why not change to using a named import, i.e. > > import tkinter as tk > > rather than > > from Tkinter import * > > Other than having to import a 'tk.' to widget names, I figured I should be > good to go. Unfortunately not. The book had code like this: > > from tkinter import * > root = Tk() > Label(root, text="Username").grid(row=0, sticky=W) > > While what I was coming up with was more like this: > > import tkinter as tk > root = tk.Tk() > tk.Label(root, text="Username").grid(row=0, sticky='W') > > > The difference being that unless I enclosed the W attribute for the option > 'sticky=' in quotes, I got an error 'Unresolved reference 'W''. > > I asked online, and found that apparently 'W' is imported along with > 'Label', 'Entry', 'Button', 'Tk', etc. at the top level of tkinter. It > appears I can use either a wildcard import, enclose it in quotes, or use > explicit reference e.g. tk.W. > > My question is this: How / why does this make any sense, that an attribute > (W) to an option (sticky=) would be imported at the same level as widgets > themselves? Is it some artifact left over from tcl, or just a bad idea that > never went away? Between having to explicitly reference every widget > already (tk.Label) and having to be extra specific with options like > sticky... I'm starting to see why everyone uses wildcard imports when it > comes to tkinter! > > Thanks, > > Monte > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss From deepakts08 at gmail.com Wed Mar 12 18:08:50 2014 From: deepakts08 at gmail.com (deepak) Date: Wed, 12 Mar 2014 10:08:50 -0700 (PDT) Subject: [Tkinter-discuss] how to get values form the custom widget module to the parent frame widget in Tkinter? Message-ID: <1394644130523-5050211.post@n6.nabble.com> I am ne to python GUi, I have built a cust frame by inheriting the Frame , the code is as follows : from Tkinter import * from logging import exception import tkFileDialog import pickle import os.path STATION_NAME, TARGET_NAME, HOST_NAME, TARGET_IP, HOST_IP, EXCEL_FILE = range(6) class NAddStation(Frame): def __init__(self, parent=None): Frame.__init__(self, parent) self.pack(side=TOP, expand=YES, fill=BOTH) self.tempVar = [] # Label Area self.labels = ["Station Name :", "Target Name :", "Host Name :", "Target's IP :", "Host's IP :", "Excel File :"] self.REMOVE =1 self.ADD = 2 self.UPDATE = 3 # Entry Area self.Entry_Target_Name = None self.Entry_Host_Name = None self.Entry_Target_IP = None self.Entry_Host_IP = None self.Entry_Station_Name = None self.Entry_File_Path = None #Misc self.File_Browse_Button = None self.Add_Button = None self.Cancel_Button = None self.createWindow() def openFileBrowser(self): filepath = tkFileDialog.askopenfilename() self.Entry_File_Path.insert(0, filepath) def validate(self): print("Adding Data....") self.Store_Station() def Remove_Station(self,key): if os.path.isfile('Station-pickle'): dbfile = open('Station-pickle', 'rb') db = pickle.load(dbfile) dbfile.close() db.__delitem__(key) dbfile = open('Station-pickle', 'wb') pickle.dump(db, dbfile) dbfile.close() def returnStationName(self): return self.tempVar[0].get() def Store_Station(self): fl={} rc={} fl['SN']= self.tempVar[0].get() fl['HN']= self.tempVar[1].get() fl['HP']= self.tempVar[2].get() fl['TN']= self.tempVar[3].get() fl['TP']= self.tempVar[4].get() fl['XL']= self.tempVar[5].get() station_name = self.tempVar[0].get() rc[station_name]= fl if not os.path.isfile('Station-pickle'): print ("Creating File") dbfile = open('Station-pickle', 'wb') # use binary mode files in 3.X pickle.dump(rc, dbfile) # data is bytes, not str else: print ("Appending File") # -------- dbfile = open('Station-pickle', 'rb') db = pickle.load(dbfile) dbfile.close() db[station_name] = fl dbfile = open('Station-pickle', 'wb') pickle.dump(db, dbfile) dbfile.close() dbfile.close() def Load_Station(self, key_value): dbfile = open('Station-pickle', 'rb') # use binary mode files in 3.X db = pickle.load(dbfile) for key in db: if key == key_value: print("STATION_NAME :" + db[key]['SN']) print("HOST_NAME :" + db[key]['HN']) print("HOST_IP :" + db[key]['HP']) print("TARGET_NAME :" + db[key]['TN']) print("TARGENT_IP :" + db[key]['TP']) print("FILE_NAME :" + db[key]['XL']) return ("STATION_NAME :" + db[key]['SN'] +"\n" + "HOST_NAME :" + db[key]['HN'] + "\n" + "HOST_IP :" + db[key]['HP'] + "\n" + "TARGET_NAME :" + db[key]['TN'] + "\n" + "TARGENT_IP :" + db[key]['TP'] + "\n" + "FILE_NAME :" + db[key]['XL']) print "No Station with Name " + key_value dbfile.close() def createWindow(self): # Station Entry Label(self, text=self.labels[STATION_NAME]).grid(row=0, column=0) self.Entry_Station_Name = Entry(self, width=20) self.tempVar.append(self.Entry_Station_Name) self.Entry_Station_Name.grid(row=0, column=1) # Host Entry Label(self, text=self.labels[HOST_NAME]).grid(row=1, column=0) self.Entry_Host_Name = Entry(self, width=20) self.tempVar.append(self.Entry_Host_Name) self.Entry_Host_Name.grid(row=1, column=1) Label(self, text=self.labels[HOST_IP]).grid(row=1, column=2) self.Entry_Host_IP = Entry(self, width=20) self.tempVar.append(self.Entry_Host_IP) self.Entry_Host_IP.grid(row=1, column=3) # Target Entry Label(self, text=self.labels[TARGET_NAME]).grid(row=2, column=0) self.Entry_Target_Name = Entry(self, width=20) self.tempVar.append(self.Entry_Target_Name) self.Entry_Target_Name.grid(row=2, column=1) Label(self, text=self.labels[TARGET_IP]).grid(row=2, column=2) self.Entry_Target_IP = Entry(self, width=20) self.tempVar.append(self.Entry_Target_IP) self.Entry_Target_IP.grid(row=2, column=3) #Browser Button Label(self, text=self.labels[EXCEL_FILE]).grid(row=3, column=0) self.Entry_File_Path = Entry(self, width=50) self.tempVar.append(self.Entry_File_Path) self.Entry_File_Path.grid(row=3, column=1) self.File_Browse_Button = Button(self, text="Browse", command=self.openFileBrowser, width=10,justify=CENTER).grid(row=3, column=3) Button(self, text="Add", command=(lambda : self.validate()), width=10,justify=CENTER).grid(row=4, column=1) Button(self, text="Cancel", command= self.winfo_toplevel().destroy, width=10,justify=CENTER).grid(row=4, column=2) self.Entry_Station_Name.focus() **Now I want to use the above mentioned Custom Frame in Main app , when i ckick on AddStation from menu file the custome Frame pops up , I can add the data but when i click on ok , i want the data to apear on the textArea , represented by Self.Entity2 in the following code:** from Tkconstants import LEFT, BOTTOM, X, TOP, RIGHT, ACTIVE, Y from Tkinter import Tk, Frame, Menu, END, Label, YES, BOTH, Entry, StringVar, Toplevel import Tkinter import sys from make_db_pickle import * class SimpleApp(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.pack(expand=YES, fill=BOTH) self.master.title('GUI') self.initialize(parent) self.TargetName = None self.HostName = None self.eHostNameEntry =None self.sbar = None self.AddWidget = None def CancelButtonPressed(self, widget): # print "Cancel Button Pressed" widget.destroy() def AddEntryToListBox(self,entry): self.entry1.insert(END, entry) def enter(event,entry,widget): event.OkButtonPressed(entry,widget) def OkButtonPressed(self,entry,widget): print "Ok bution pressed" self.AddEntryToListBox(str(entry.get()).upper()) widget.destroy() def runCommand(self, selection): # redefine me lower self.entry2.insert('1.0',"The following Station was selected " + selection) self.entry3.insert('1.0',"The following Station was selected " + selection) self.entry4.insert('1.0',"The following Station was selected " + selection) print('You selected:', selection) def handleList(self, event): index = self.entry1.curselection() # on list double-click label = self.entry1.get(index) # fetch selection text self.runCommand(label) # and call action here def initialize(self, parent): menubar = Menu(self.parent) self.parent.config(menu=menubar) self.sbar = Tkinter.Scrollbar(self) fileMenu = Menu(menubar, tearoff = False) fileMenu.add_command(label="Add Station", command=self.AddNewStation) fileMenu.add_command(label="Delete Station", command=self.onExit) fileMenu.add_separator() fileMenu.add_command(label="Exit", command=self.onExit) menubar.add_cascade(label="File", menu=fileMenu) self.grid() self.entry1 = Tkinter.Listbox(self, height=24, yscrollcommand=1, bg="white") # -------------------------- self.sbar = Tkinter.Scrollbar(self) self.sbar.config(command=self.entry1.yview) # xlink sbar and list self.entry1.config(yscrollcommand=self.sbar.set) # move one moves other self.entry1.pack(side=LEFT, expand=YES, fill=BOTH) # list clipped first # pos = 0 #list.config(selectmode=SINGLE, setgrid=1) # select,resize modes self.entry1.bind('', self.handleList) # set event handler # -------------------------- # for i in acts: # self.entry1.insert(END, i) # self.entry1 = Tkinter.Text(self) self.entry1.grid(column=0, row=0, sticky='EW') self.entry2 = Tkinter.Text(self) self.entry2.grid(column=1, row=0, sticky='EW', padx=1, pady=1) self.entry3 = Tkinter.Text(self) self.entry3.grid(column=0, row=1, sticky='EW', padx=1, pady=1) self.entry4 = Tkinter.Text(self) self.entry4.grid(column=1, row=1, sticky='EW', padx=1, pady=1) def onExit(self): sys.exit() def AddNewStation(self): n = Toplevel(root) n.title("Add Station") self.AddWidget = NAddStation(n) n.transient() def AddStation(self): AddStationDialogBox = Toplevel(root) #Tkinter.Tk() AddStationDialogBox.title('Add Station') frame = Frame(AddStationDialogBox) #frame for target entry frame.pack(padx=10, pady=10,side=TOP,fill=X) bottomframe = Frame(AddStationDialogBox) #froame for host entry bottomframe.pack(padx=10, pady=10,side=TOP,fill=X) bottomframe2 = Frame(AddStationDialogBox) #frame for button bottomframe2.pack(side=TOP) AddStationDialogBox.transient() lTargentName = Label(frame, text="Target Name :") lTargentName.pack(side=LEFT) TargetName = StringVar() eTargetEntryField = Entry(frame,width=20) eTargetEntryField.pack(side=RIGHT,fill=X) eTargetEntryField.focus() lHostName = Label(bottomframe, text="Host Name :") lHostName.pack(side=LEFT) HostName = StringVar() eHostNameEntry = Entry(bottomframe,width=20) eHostNameEntry.pack(side=RIGHT,fill=X) okbutton = Tkinter.Button(bottomframe2, text="OK",command=(lambda : self.OkButtonPressed(eTargetEntryField, AddStationDialogBox))) okbutton.pack(side=LEFT, padx=10, pady=15) cancelbutton = Tkinter.Button(bottomframe2, text="CANCEL", command=(lambda : self.CancelButtonPressed(AddStationDialogBox))) cancelbutton.pack(side=LEFT, padx=10, pady=15) AddStationDialogBox.focus() AddStationDialogBox.bind('',(lambda event : self.enter(eTargetEntryField,AddStationDialogBox))) AddStationDialogBox.mainloop() if __name__ == "__main__": root = Tk() app = SimpleApp(root) app.mainloop() -- View this message in context: http://python.6.x6.nabble.com/how-to-get-values-form-the-custom-widget-module-to-the-parent-frame-widget-in-Tkinter-tp5050211.html Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. From kalman_g at verizon.net Sat Mar 15 02:20:23 2014 From: kalman_g at verizon.net (GKalman) Date: Fri, 14 Mar 2014 18:20:23 -0700 (PDT) Subject: [Tkinter-discuss] Need help using Tkinter Widget lift() and lower() methods with Place geometry Manager Message-ID: <1394846423164-5050511.post@n6.nabble.com> """ I tried a simple case: Frame with two Canvas widgets. I drag (i.e. move) Canvas #1. When it overlaps with Canvas #2 neither the lift() nor the lower() methods work. Why? """ from Tkinter import * class myObject(): #-------------------------------- def __init__(self,root): self.root = root #place a Frame on the root: self.f = Frame(self.root, bg="yellow", width=600, height=400) self.f.pack() #place a Canvas on the Frame: self.c =Canvas(self.f, bg="cyan",width=100,height=50) #NW-vtx of Canvas: self.xNW=10 self.yNW=10 self.c.place(x=self.xNW,y=self.yNW) ##event-generators: self.c.bind('', self.startMoveWindow) self.c.bind('', self.MoveWindow) self.c2=Canvas(self.f,bg="red",width=100,height=100) self.c2.place(x=300,y=200) self.c2.lower(self.c) #----------------------------------------------- def startMoveWindow(self, event): ## at start: record current root coordinates self.xo, self.yo = event.x_root, event.y_root #-------------------------------------- def MoveWindow(self, event): self.root.update_idletasks() ## use root coordinates for offset of Widget (canvas) coordinates self.xNW += event.x_root - self.xo self.yNW+= event.y_root - self.yo ## update coordinates self.xo, self.yo= event.x_root, event.y_root ## Move & redraw Widget (canvas) self.c.place_configure(x=self.xNW, y=self.yNW) #================================== root=Tk() x = myObject(root) root.mainloop() -- View this message in context: http://python.6.x6.nabble.com/Need-help-using-Tkinter-Widget-lift-and-lower-methods-with-Place-geometry-Manager-tp5050511.html Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. From michael.odonnell at uam.es Sat Mar 15 08:55:04 2014 From: michael.odonnell at uam.es (Michael O'Donnell) Date: Sat, 15 Mar 2014 08:55:04 +0100 Subject: [Tkinter-discuss] Need help using Tkinter Widget lift() and lower() methods with Place geometry Manager In-Reply-To: <1394846423164-5050511.post@n6.nabble.com> References: <1394846423164-5050511.post@n6.nabble.com> Message-ID: Dear GKalman, According to the documentation, lift and lower affect the toplevel that contains the canvas, they do not change stacking order of widgets within a toplevel. Maybe the better alternative is to call a place_forget() on the lower canvas and then place it again, which should put it above. I have never used place() and am not sure of its abilities to do what you want. Another possibility is to use a Canvas instead of your enclosing frame, and then use creatr_window to place your canvases within the enclosing canvas, and use canvas methods: move, tag_raise and tag_lower raise and lower your canvases relative to each other. Just a suggestion, Mick On 15 March 2014 02:20, GKalman wrote: > """ > I tried a simple case: > Frame with two Canvas widgets. I drag (i.e. move) Canvas #1. When it > overlaps with Canvas #2 neither the lift() nor the lower() methods work. > Why? > """ > from Tkinter import * > > class myObject(): > #-------------------------------- > def __init__(self,root): > self.root = root > > #place a Frame on the root: > self.f = Frame(self.root, bg="yellow", width=600, height=400) > self.f.pack() > > #place a Canvas on the Frame: > self.c =Canvas(self.f, bg="cyan",width=100,height=50) > #NW-vtx of Canvas: > self.xNW=10 > self.yNW=10 > self.c.place(x=self.xNW,y=self.yNW) > > ##event-generators: > self.c.bind('', self.startMoveWindow) > self.c.bind('', self.MoveWindow) > > self.c2=Canvas(self.f,bg="red",width=100,height=100) > self.c2.place(x=300,y=200) > self.c2.lower(self.c) > > #----------------------------------------------- > def startMoveWindow(self, event): > ## at start: record current root coordinates > self.xo, self.yo = event.x_root, event.y_root > > #-------------------------------------- > def MoveWindow(self, event): > self.root.update_idletasks() > > ## use root coordinates for offset of Widget (canvas) coordinates > self.xNW += event.x_root - self.xo > self.yNW+= event.y_root - self.yo > ## update coordinates > self.xo, self.yo= event.x_root, event.y_root > > ## Move & redraw Widget (canvas) > self.c.place_configure(x=self.xNW, y=self.yNW) > > #================================== > root=Tk() > x = myObject(root) > root.mainloop() > > > > -- > View this message in context: http://python.6.x6.nabble.com/Need-help-using-Tkinter-Widget-lift-and-lower-methods-with-Place-geometry-Manager-tp5050511.html > Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss From msa01 at bitflipper.ca Sat Mar 22 03:26:01 2014 From: msa01 at bitflipper.ca (Cam Farnell) Date: Fri, 21 Mar 2014 23:26:01 -0300 Subject: [Tkinter-discuss] OT: Unicode In-Reply-To: <03EB34F3-F69D-4A29-BBAB-EC1A27AD4229@passcal.nmt.edu> References: <3766A980-82BA-4AA0-9F1F-5822C79AF1E1@passcal.nmt.edu> <5310BADA.6020301@bitflipper.ca> <6E01C189-AA5E-4F6F-A303-2C635608ADBB@passcal.nmt.edu> <03EB34F3-F69D-4A29-BBAB-EC1A27AD4229@passcal.nmt.edu> Message-ID: <532CF4B9.1020708@bitflipper.ca> Technically this is a Python question, not a Tkinter question, but it's in the context of a Tkinter application so I don't feel *too* guilty about posting it here. OK. I've got at Tkinter application (running with Python 2.7.2 on Ubuntu 12.04.4 LTS) that needs to handle French accented characters. And it does handle accented characters just fine. I can type an accented character into an Entry and it shows up correctly. I can display it on a Text. I can cPickle it to disk and read it back. For example, if I enter e-circumflex (in at Tkinter Entry) and then print it using repr I get: u\'EA' If I look in the cPickled file there are 0xEA's where the e-circumflex characters are. So far so good. The problem comes when I need to read into my Tkinter application a file which has accented characters and which was prepared using a text editor like, for example, gedit. The file to be read also has 0xEA's to represent e-circumflex. However, when I read such a file the resulting string then contains u'\cd\xaa' where the e-circumflexes belong. I don't know who is doing the unwanted conversion or how to make it go away. I've tried reading in binary mode, I've tried opening the file using: F = codecs.open('temp.txt', encoding='latin-1') I've tried putting: # -*- coding: latin-1 -* as the second line of my program. I've tried reading Python/unicode documentation till my eyes went blurry. All to no avail. There is probably some really simple solution to this, but so far I've failed to find. it. Thus, if anyone out there in Tkinter land knows the simple solution or could point me to a good source of information I would greatly appreciate it. Thanks Cam Farnell From michael.odonnell at uam.es Sat Mar 22 08:52:49 2014 From: michael.odonnell at uam.es (Michael O'Donnell) Date: Sat, 22 Mar 2014 08:52:49 +0100 Subject: [Tkinter-discuss] OT: Unicode In-Reply-To: <532CF4B9.1020708@bitflipper.ca> References: <3766A980-82BA-4AA0-9F1F-5822C79AF1E1@passcal.nmt.edu> <5310BADA.6020301@bitflipper.ca> <6E01C189-AA5E-4F6F-A303-2C635608ADBB@passcal.nmt.edu> <03EB34F3-F69D-4A29-BBAB-EC1A27AD4229@passcal.nmt.edu> <532CF4B9.1020708@bitflipper.ca> Message-ID: Dear Cam, Python 3 is so much better at dealing with unicode than Python 2. But, that said. Your file is in an encoding that is not latin-1 (which is basically an anglo encoding, no good if your text has inflections/accents). Solution: 1. Open your text file in a browser 2. If the file displays ok in the browser, see what encoding the browser used to decode the file: there is usually a "Encoding" option in the menu somewhere, e.g. in Chrome, under the View menu. Assume for this example that it is iso-8859-1 3. Change your file opening to: F = codecs.open('temp.txt', encoding=iso-8859-1') That should fix it. you can read from the file directly as a unicode string. Mick On 22 March 2014 03:26, Cam Farnell wrote: > Technically this is a Python question, not a Tkinter question, but it's in > the context of a Tkinter application so I don't feel *too* guilty about > posting it here. > > OK. I've got at Tkinter application (running with Python 2.7.2 on Ubuntu > 12.04.4 LTS) that needs to handle French accented characters. And it does > handle accented characters just fine. I can type an accented character into > an Entry and it shows up correctly. I can display it on a Text. I can > cPickle it to disk and read it back. For example, if I enter e-circumflex > (in at Tkinter Entry) and then print it using repr I get: > > u\'EA' > > If I look in the cPickled file there are 0xEA's where the e-circumflex > characters are. So far so good. > > The problem comes when I need to read into my Tkinter application a file > which has accented characters and which was prepared using a text editor > like, for example, gedit. The file to be read also has 0xEA's to represent > e-circumflex. However, when I read such a file the resulting string then > contains u'\cd\xaa' where the e-circumflexes belong. I don't know who is > doing the unwanted conversion or how to make it go away. I've tried reading > in binary mode, I've tried opening the file using: > > F = codecs.open('temp.txt', encoding='latin-1') > > I've tried putting: > > # -*- coding: latin-1 -* > > as the second line of my program. I've tried reading Python/unicode > documentation till my eyes went blurry. All to no avail. > > There is probably some really simple solution to this, but so far I've > failed to find. it. > > Thus, if anyone out there in Tkinter land knows the simple solution or could > point me to a good source of information I would greatly appreciate it. > > Thanks > > Cam Farnell > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss From bob at mellowood.ca Sat Mar 22 17:40:31 2014 From: bob at mellowood.ca (Bob van der Poel) Date: Sat, 22 Mar 2014 09:40:31 -0700 Subject: [Tkinter-discuss] OT: Unicode In-Reply-To: References: <3766A980-82BA-4AA0-9F1F-5822C79AF1E1@passcal.nmt.edu> <5310BADA.6020301@bitflipper.ca> <6E01C189-AA5E-4F6F-A303-2C635608ADBB@passcal.nmt.edu> <03EB34F3-F69D-4A29-BBAB-EC1A27AD4229@passcal.nmt.edu> <532CF4B9.1020708@bitflipper.ca> Message-ID: You might also have luck with encoding='cp1252'. This is the "standard" set for windows characters. On Sat, Mar 22, 2014 at 12:52 AM, Michael O'Donnell wrote: > Dear Cam, > > Python 3 is so much better at dealing with unicode than > Python 2. > > But, that said. Your file is in an encoding > that is not latin-1 (which is basically an anglo > encoding, no good if your text has inflections/accents). > > Solution: > > 1. Open your text file in a browser > > 2. If the file displays ok in the browser, > see what encoding the browser used > to decode the file: there is usually a "Encoding" > option in the menu somewhere, e.g. in Chrome, > under the View menu. > > Assume for this example that it is iso-8859-1 > > 3. Change your file opening to: > > F = codecs.open('temp.txt', encoding=iso-8859-1') > > That should fix it. you can read from the file > directly as a unicode string. > > Mick > > On 22 March 2014 03:26, Cam Farnell wrote: >> Technically this is a Python question, not a Tkinter question, but it's in >> the context of a Tkinter application so I don't feel *too* guilty about >> posting it here. >> >> OK. I've got at Tkinter application (running with Python 2.7.2 on Ubuntu >> 12.04.4 LTS) that needs to handle French accented characters. And it does >> handle accented characters just fine. I can type an accented character into >> an Entry and it shows up correctly. I can display it on a Text. I can >> cPickle it to disk and read it back. For example, if I enter e-circumflex >> (in at Tkinter Entry) and then print it using repr I get: >> >> u\'EA' >> >> If I look in the cPickled file there are 0xEA's where the e-circumflex >> characters are. So far so good. >> >> The problem comes when I need to read into my Tkinter application a file >> which has accented characters and which was prepared using a text editor >> like, for example, gedit. The file to be read also has 0xEA's to represent >> e-circumflex. However, when I read such a file the resulting string then >> contains u'\cd\xaa' where the e-circumflexes belong. I don't know who is >> doing the unwanted conversion or how to make it go away. I've tried reading >> in binary mode, I've tried opening the file using: >> >> F = codecs.open('temp.txt', encoding='latin-1') >> >> I've tried putting: >> >> # -*- coding: latin-1 -* >> >> as the second line of my program. I've tried reading Python/unicode >> documentation till my eyes went blurry. All to no avail. >> >> There is probably some really simple solution to this, but so far I've >> failed to find. it. >> >> Thus, if anyone out there in Tkinter land knows the simple solution or could >> point me to a good source of information I would greatly appreciate it. >> >> Thanks >> >> Cam Farnell >> >> _______________________________________________ >> Tkinter-discuss mailing list >> Tkinter-discuss at python.org >> https://mail.python.org/mailman/listinfo/tkinter-discuss > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss -- **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: bob at mellowood.ca WWW: http://www.mellowood.ca