[Tutor] educational
i wanted to use a .jpeg image file on a label in a small program that am building in Python, i use Window 7 professional OS, and Python 3.4here are my codes:- import sys from tkinter import * #Window mGui=Tk() mGui.geometry("1000x500") mGui.title("Kamusi") #Functions #Variables ment=StringVar() #Images image = Image.open("logo.jpg") photo = ImageTk.PhotoImage(image) #Widgets Logo=Label(mGui,image=photo,width=510,height=150) Logo.grid(row=0,column=3) when i run those codes i get this error message:- AttributeError: type object 'Image' has no attribute 'open' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] educational
Welcome, Michael! MICHAEL BASHAGI writes: > when i run those codes i get this error message:- When showing an error, please show the entire traceback; it usually contains information useful for diagnosing the problem. > AttributeError: type object 'Image' has no attribute 'open' In this case, I'm fairly sure the line producing this error is:: image = Image.open("logo.jpg") And Python is correct, the ‘Image’ type has no ‘open’ attribute. What leads you to think that would work? If there is some article online telling you to use that, it's incorrect; please help us to correct that. It's best not to guess what attributes are in a type (otherwise known as the “API” for the type). Instead, consult the documentation. For Tkinter, that is http://www.python.org/topics/tkinter/>. -- \“Most people, I think, don't even know what a rootkit is, so | `\ why should they care about it?” —Thomas Hesse, Sony BMG, 2006 | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] educational
Ben Finney writes: > It's best not to guess what attributes are in a type (otherwise known as > the “API” for the type). Instead, consult the documentation. For > Tkinter, that is http://www.python.org/topics/tkinter/>. My apologies; there's a broken link in the documentation. Use this instead http://docs.python.org/3/library/tkinter.html>. -- \ “I busted a mirror and got seven years bad luck, but my lawyer | `\thinks he can get me five.” —Steven Wright | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] educational
Ben Finney wrote: > Welcome, Michael! > > MICHAEL BASHAGI writes: > >> when i run those codes i get this error message:- > > When showing an error, please show the entire traceback; it usually > contains information useful for diagnosing the problem. > >> AttributeError: type object 'Image' has no attribute 'open' > > In this case, I'm fairly sure the line producing this error is:: > > image = Image.open("logo.jpg") > > And Python is correct, the ‘Image’ type has no ‘open’ attribute. What > leads you to think that would work? If there is some article online > telling you to use that, it's incorrect; please help us to correct that. There are a few things around called `Image`. The code the OP is trying to adapt probably uses the Image from the PIL: import tkinter from PIL import ImageTk from PIL import Image mGui = tkinter.Tk() image = Image.open("logo.jpg") photo = ImageTk.PhotoImage(image=image) Logo = tkinter.Label(mGui, image=photo) Logo.grid(row=0, column=3) mGui.mainloop() This can be simplified: import tkinter from PIL import ImageTk mGui = tkinter.Tk() photo = ImageTk.PhotoImage(file="logo.jpg") Logo = tkinter.Label(mGui, image=photo) Logo.grid(row=0, column=3) mGui.mainloop() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] educational
On 09/03/14 10:37, Peter Otten wrote: In this case, I'm fairly sure the line producing this error is:: image = Image.open("logo.jpg") And Python is correct, the ‘Image’ type has no ‘open’ attribute. What There are a few things around called `Image`. The code the OP is trying to adapt probably uses the Image from the PIL: Which may lead to another issue since the OP is using Python 3.4. Is PIL available for any Python 3.x yet? And especially for 3.4? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] educational
On 09/03/2014 17:06, Alan Gauld wrote: On 09/03/14 10:37, Peter Otten wrote: In this case, I'm fairly sure the line producing this error is:: image = Image.open("logo.jpg") And Python is correct, the ‘Image’ type has no ‘open’ attribute. What There are a few things around called `Image`. The code the OP is trying to adapt probably uses the Image from the PIL: Which may lead to another issue since the OP is using Python 3.4. Is PIL available for any Python 3.x yet? And especially for 3.4? Pillow does seem to (haven't used it myself): http://pillow.readthedocs.org/en/latest/installation.html TJG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] educational
Peter Otten <__pete...@web.de> writes: > Ben Finney wrote: > > And Python is correct, the ‘Image’ type has no ‘open’ attribute. What > > leads you to think that would work? If there is some article online > > telling you to use that, it's incorrect; please help us to correct that. > > There are a few things around called `Image`. The code the OP is trying to > adapt probably uses the Image from the PIL: Then I'm further confirmed in my view that ‘from tkinter import *’ is dreadful practice, especially for a system we recommend to newcomers. If it were:: > import tkinter > from PIL import ImageTk > from PIL import Image then it would be clear which “Image” is being used where. Now all I need is for the Tkinter-using community to change itself to fix this confusing practice. I won't hold my breath. -- \ Rommel: “Don't move, or I'll turn the key on this can of Spam!” | `\ —The Goon Show, _Rommel's Treasure_ | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] educational
On 09/03/14 19:56, Ben Finney wrote: Then I'm further confirmed in my view that ‘from tkinter import *’ is dreadful practice, especially for a system we recommend to newcomers. Its always dreadful practice for production code regardless of the module. Its OK for playing at the >>> prompt but not much more. I usually import Tkinter as import tkinter as tk Now all I need is for the Tkinter-using community to change itself to fix this confusing practice. I won't hold my breath. I don't find everyone in the Tkinter community using from tkinter import *, quite a few use normal practice. But historically I agree it's been an unfortunate paradigm, Even the IDLE code uses import * :-( -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] educational
On 09/03/2014 21:35, Alan Gauld wrote: On 09/03/14 19:56, Ben Finney wrote: Then I'm further confirmed in my view that ‘from tkinter import *’ is dreadful practice, especially for a system we recommend to newcomers. Its always dreadful practice for production code regardless of the module. Its OK for playing at the >>> prompt but not much more. I usually import Tkinter as import tkinter as tk Now all I need is for the Tkinter-using community to change itself to fix this confusing practice. I won't hold my breath. I don't find everyone in the Tkinter community using from tkinter import *, quite a few use normal practice. But historically I agree it's been an unfortunate paradigm, Even the IDLE code uses import * :-( I vaguely recall reading somewhere that IDLE is moving towards 'ímport tkinter as tk', a process which would certainly be helped by the extremely sensible PEP 434 http://legacy.python.org/dev/peps/pep-0434/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] educational
Alan Gauld writes: > On 09/03/14 19:56, Ben Finney wrote: > > Now all I need is for the Tkinter-using community to change itself > > to fix this confusing practice [‘from tkinter import *’]. I won't > > hold my breath. > > I don't find everyone in the Tkinter community using from tkinter > import *, quite a few use normal practice. Then they are not following the advice of the documentation http://docs.python.org/3/library/tkinter.html>, which explicitly recommends ‘from tkinter import *’. It's also prevalent in third-party http://effbot.org/tkinterbook/> http://www.tkdocs.com/> documents which are referenced from the official documentation. This one http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/> does at least get it right. If ‘import tkinter as tk’ were to become the norm in official and third-party documentation, I'd agree the problem would be solved at that point. -- \ “One bad programmer can easily create two new jobs a year. | `\ Hiring more bad programmers will just increase our perceived | _o__) need for them.” —David Lorge Parnas, 1999-03 | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] improvements on a renaming script
Hello all, A bit of background, I had some slides scanned and a 3-character slice of the file name indicates what roll of film it was. This is recorded in a tab-separated file called fileNames.tab. Its content looks something like: p01 200511_autumn_leaves p02 200603_apple_plum_cherry_blossoms The original file names looked like: 1p01_abc_0001.jpg 1p02_abc_0005.jpg The renamed files are: 200511_autumn_leaves_-_001.jpeg 200603_apple_plum_cherry_blossoms_-_005.jpeg The script below works and has done what I wanted, but I have a few questions: - In the get_long_names() function, the for/if thing is reading the whole fileNames.tab file every time, isn't it? In reality, the file was only a few dozen lines long, so I suppose it doesn't matter, but is there a better way to do this? - Really, I wanted to create a new sequence number at the end of each file name, but I thought this would be difficult. In order for it to count from 01 to whatever the last file is per set p01, p02, etc, it would have to be aware of the set name and how many files are in it. So I settled for getting the last 3 digits of the original file name using splitext(). The strings were unique, so it worked out. However, I can see this being useful in other places, so I was wondering if there is a good way to do this. Is there a term or phrase I can search on? - I'd be interested to read any other comments on the code. I'm new to python and I have only a bit of computer science study, quite some time ago. #!/usr/bin/env python3 import os import csv # get longnames from fileNames.tab def get_long_name(glnAbbrev): with open( os.path.join(os.path.expanduser('~'),'temp2','fileNames.tab') ) as filenames: filenamesdata = csv.reader(filenames, delimiter='\t') for row in filenamesdata: if row[0] == glnAbbrev: return row[1] # find shortname from slice in picture filename def get_slice(fn): threeColSlice = fn[1:4] return threeColSlice # get 3-digit sequence number from basename def get_bn_seq(fn): seq = os.path.splitext(fn)[0][-3:] return seq # directory locations indir = os.path.join(os.path.expanduser('~'),'temp4') outdir = os.path.join(os.path.expanduser('~'),'temp5') # rename for f in os.listdir(indir): if f.endswith(".jpg"): os.rename( os.path.join(indir,f),os.path.join( outdir, get_long_name(get_slice(f))+"_-_"+get_bn_seq(f)+".jpeg") ) exit() Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] improvements on a renaming script
On 3/9/2014 3:22 PM, street.swee...@mailworks.org wrote: Hello all, A bit of background, I had some slides scanned and a 3-character slice of the file name indicates what roll of film it was. This is recorded in a tab-separated file called fileNames.tab. Its content looks something like: p01 200511_autumn_leaves p02 200603_apple_plum_cherry_blossoms The original file names looked like: 1p01_abc_0001.jpg 1p02_abc_0005.jpg The renamed files are: 200511_autumn_leaves_-_001.jpeg 200603_apple_plum_cherry_blossoms_-_005.jpeg The script below works and has done what I wanted, but I have a few questions: - In the get_long_names() function, the for/if thing is reading the whole fileNames.tab file every time, isn't it? In reality, the file was only a few dozen lines long, so I suppose it doesn't matter, but is there a better way to do this? The "usual" way is to create a dictionary with row[0] contents as keys and row[1] contents as values. Do this once per run. Then lookup each glnAbbrev in the dictionary and return the corresponding value. - Really, I wanted to create a new sequence number at the end of each file name, but I thought this would be difficult. In order for it to count from 01 to whatever the last file is per set p01, p02, etc, it would have to be aware of the set name and how many files are in it. So I settled for getting the last 3 digits of the original file name using splitext(). The strings were unique, so it worked out. However, I can see this being useful in other places, so I was wondering if there is a good way to do this. Is there a term or phrase I can search on? I'm sorry but I don't fully understand that paragraph. And why would you need to know the number of files? - I'd be interested to read any other comments on the code. I'm new to python and I have only a bit of computer science study, quite some time ago. Beware using tabs as indents. As rendered by Thunderbird they appear as 8 spaces which is IMHO overkill. It is much better to use spaces. Most Python IDEs have an option to convert tabs to spaces. The Python recommendation is 4; I use 2. #!/usr/bin/env python3 import os import csv # get longnames from fileNames.tab def get_long_name(glnAbbrev): with open( os.path.join(os.path.expanduser('~'),'temp2','fileNames.tab') ) as filenames: filenamesdata = csv.reader(filenames, delimiter='\t') for row in filenamesdata: if row[0] == glnAbbrev: return row[1] # find shortname from slice in picture filename def get_slice(fn): threeColSlice = fn[1:4] return threeColSlice Writing a function to get a slice seems overkill also. Just slice in place. # get 3-digit sequence number from basename def get_bn_seq(fn): seq = os.path.splitext(fn)[0][-3:] return seq # directory locations indir = os.path.join(os.path.expanduser('~'),'temp4') outdir = os.path.join(os.path.expanduser('~'),'temp5') # rename for f in os.listdir(indir): if f.endswith(".jpg"): os.rename( os.path.join(indir,f),os.path.join( outdir, get_long_name(get_slice(f))+"_-_"+get_bn_seq(f)+".jpeg") ) exit() HTH - remember to reply-all so a copy goes to the list, place your comments in-line as I did, and delete irrelevant text. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with Guess the number script
> On Mar 8, 2014, at 7:29 AM, eryksun wrote: >> >>not not (guess < 1 or guess > 100) > > Why a not not? Wouldn’t that just be saying do this because the > second not is undoing the first? In boolean algebra, `not (A or B)` is equivalent to `not A and not B` (De Morgan's law). I double negated in order to mechanically apply this rule, e.g. A or B = not not (A or B) = not (not A and not B) >> Anyway, you needn't go out of your way to rewrite the expression using >> a chained comparison. The disjunctive expression is actually >> implemented more efficiently by CPython's compiler, which you can >> verify using the dis module to disassemble the bytecode. > > I’m not sure what you’re talking about in the above paragraph. There's hardly any difference in how the interpreter evaluates the code in a simple case like this, and it's actually slightly more efficient (in CPython) without chaining. That said, chained comparisons are more efficient when the expressions being compared are computationally expensive, since each expression is only evaluated once. Regarding bytecode, CPython compiles Python source code to a sequence of bytecode operations that get interpreted at runtime. It's an implementation detail, but examining CPython bytecode is nonetheless informative. Here's a basic example: >>> def f(): ... x = 'abc' The function's code object contains the compiled code as a byte sting in its co_code attribute: >>> f.__code__.co_code b'd\x01\x00}\x00\x00d\x00\x00S' This assembled code isn't easy to read. Also, reading it requires referencing other fields of the code object such as co_consts and co_varnames. The dis module disassembles it to a form that's a bit easier to read: >>> dis.dis(f) 2 0 LOAD_CONST 1 ('abc') 3 STORE_FAST 0 (x) 6 LOAD_CONST 0 (None) 9 RETURN_VALUE The documentation for the dis module includes a basic description of each operation. http://docs.python.org/3/library/dis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor