[Tutor] Wrapping my head around global variables!!

Nevins Duret duretn at bellsouth.net
Sun Jan 30 20:19:58 CET 2011


Hello Elwin,

    Elwin I have no objections with you taking the idea, all I want to 
do is learn and connect with my some and give him something to aspire to 
and just plain out challenge.  I purchased several python books so if 
you have any recommendations on others that helped you to become
,what I consider, a python guru then please send me some links.  My only 
challenge is time and patience and sticking with.  I really appreciated 
everyone's input in helping me understand the underlying concepts of 
this.  It is more than appreciated.  Well, Elwin, I ran the code using 
python2.6 and it worked perfectly.  My only problem now is in trying to 
run it in python3.  These are the steps that I've taken to resolve it:

|
#!/usr/bin/env python3.1

from tkinter import *
import tkinter.font

letters ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
vowels = "AEIOU"
sometimes = "Y"
def changeLetter(letter):

     chosenLetter.configure(text = letter)
     if letter in vowels:
         message.configure(text = "IS A VOWEL")
     elif letter in sometimes:
         message.configure(text = "SOMETIMES A VOWEL")
     else:
         message.configure(text = "IS A CONSONANT")

root = Tk()
root.title('!!NAME MY CONSONANT/ NAME MY VOWEL!!')
lgFont = tkinter.font(family = "HELVETICA", size = 200)
msgFont = tkinter.font(family = "HELVETICA", size = 16)
mainFrame = Frame()
mainFrame.pack(padx = 10, pady = 10)
letterMessage = Frame(mainFrame)
chosenLetter = Label(letterMessage, width = 2, height = 1, font = 
lgFont, relief = 'sunken')
message = Label(letterMessage, text = "CHOOSE LETTER", font = msgFont, 
relief = 'sunken')
chosenLetter.pack(padx = 10, pady = 10, fill = 'x')
message.pack(padx = 10, pady = 10, anchor = 'w', fill = 'x', ipady = 5)

vowelsFound = Label(mainFrame)
vowelsFound.grid(row = 7, column =1, columnspan = 5, sticky = 'W')
letterMessage.grid(row = 1, column = 6, rowspan = 13)
count = 1
letterButtons = dict()
rowCount = 1
columnCount = 1
stopPoints = (5, 10, 15, 20, 25, 26)
stopIndex = 0

#letterbutton[1] = button for letter 'A'
#letterbutton[2] = button for letter 'B'
for letter in letters:
     letterButtons[count] = Button(mainFrame, text = letter, width = 5, 
command = lambda arg1 = letter : changeLetter(arg1))
     if count == 26:
         columnCount = 3
     letterButtons[count].grid(row = rowCount, column = columnCount)
     columnCount += 1
     if count == stopPoints[stopIndex] and stopIndex <= 4:
         rowCount += 1
         columnCount = 1
         stopIndex += 1

     count += 1

root.mainloop()|

When I try and  run this code, I get the error:

|Traceback (most recent call last):
   File "/home/neduret/pythonproj/alphabet3.py", line 21, in <module>
     lgFont = tkinter.font(family = "HELVETICA", size = 200)
TypeError: 'module' object is not callable|


Now correct me if I'm wrong, but for whatever reason, is tkFont 
deprecated in Python3.1?  Well, this is why I used

import tkinter.font instead.

However, now I am getting the error message stated above.  I don't know 
if you know of any steps I can do to resolve this however, I'm sure it 
must stem from either a syntax or a module I've yet to import for 
Python3.1, being that I had to install tkFont to get it to run under the 
Python2.6 environment.  I specifically admired your both complete and 
descriptive explanation on your reasoning for every piece of line in the 
code.  I can't thank you enough.

Best Regards,

Nevins Duret








On 01/29/2011 07:01 PM, Elwin Estle wrote:
> I found myself rather intrigued with what I perceived to be the basic idea behind your programming attempt, so just for the heck of it, I played around further with the idea.
>
> Attached is what I have so far, if you are interested.  It is a Tkinter GUI version with buttons for each letter and code to tell the user if it is a vowel or a consonant, or the "sometimes" vowels, y and w.  It does not record letters chosen.
>
> It only uses the upper case letters, but I was thinking it might be interesting to use both upper and lower case.  Right now, the letters are arranged in order, but with a total of 52 characters, both upper and lower, arranged in a grid, one could perhaps "scramble" the order of the buttons using the random.shuffle function.
>
> Python does have sound capability, depending on the platform you are running on, so it would be cool to have it play some kind of .wav file that tells the user if the letter is a vowel/consonant.  You could even record the sound of each letter, then string the .wavs together so that it could say, "A is a vowel" or somesuch.  I haven't messed with the sound thing, myself.
>
> Anyway, the attached code is heavily commented. Feel free to delete the attachment (if you feel I am "horning in" on your idea) or use/modify it for your own purposes.  Writing it helped me learn a few things, like the whole "button - command" thing.  It was driving me nuts, until I found out what the problem was.  And it was a chance to use the code somebody on python tutor gave me for creating Tkinter widgets "on the fly" at program runtime.
>
> --- On Fri, 1/28/11, Nevins Duret<duretn at bellsouth.net>  wrote:
>
>> From: Nevins Duret<duretn at bellsouth.net>
>> Subject: [Tutor] Wrapping my head around global variables!!
>> To: tutor at python.org
>> Date: Friday, January 28, 2011, 10:22 AM
>> Hello Python collective,
>>
>>          I am trying to wrap my head
>> around what exactly is causing me not to get any output or
>> error message in the following code:
>>
>>> #!/usr/bin/env python3.1
>>>
>>> import random
>>>
>>> def main():
>>>
>>>       def chosen_letter():
>>>
>>>           chosen_letter =
>> Consonant()
>>>           chosen_letter =
>> Vowel()
>>>       return chosen_letter
>>>
>>>       Consonant = random.choice( [B,
>> C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Z] )
>>>       Vowel = random.choice( [A, E,
>> I, O, U, Y] )
>>>       print("Choose a letter"(
>> Consonant, Vowel ))
>>>       print("You randomly chose"(
>> Consonant, Vowel ))
>>>       if Consonant == ( "B C D F G H
>> J K L M N P Q R S T V W X Z" ):
>>>           print("You
>> randomly chose a Consonant")
>>>       else:
>>>           print("You
>> randomly chose a Vowel")
>>>       if Consonant:
>>>
>>     gameStatus = "CONTINUE"
>>>       elif Vowel == A or Vowel == E
>> or Vowel == I or Vowel == O or Vowel == U or Vowel == Y:
>>>
>>     gameStatus = "FINISHED ALL VOWELS"
>>>       if gameStatus == "FINISHED ALL
>> VOWELS":
>>>           print("FINISHED
>> DISPLAYING ALL VOWELS")
>>>       else:
>>>           print("LOOKING
>> FOR MORE VOWELS")
>>> if __name__ == "__main__": main()
>>>
>> All this code is intended to do is to have a user, in this
>> case my 4 year old, choose letters of the alphabet until all
>> vowels are specified.  For whatever reason I'm neither
>> getting an error message nor am I obtaining any messages in
>> the output console.  Any help on this would be greatly
>> appreciated.  I eventually want to make this into a gui
>> in order to introduce young kids to programming, by having
>> the letters print out to screen as the user makes choices
>> based on vowels and consonants.  Again, thank you for
>> any help and expertise.
>>
>> Best Regards,
>>
>> freesparks
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>        

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110130/1f5376ea/attachment-0001.html>


More information about the Tutor mailing list