[Tutor] import * not allowed at function level - wassup?

Jim Mooney cybervigilante at gmail.com
Thu Jun 6 11:30:17 CEST 2013


In the program below, which now works, I tried to use "from
tkinter.constants import *" to throw up an error message with tkinter,
and got the error "import * only allowed at module level." So I
instead imported tkinter.constants, but then I had to prepend every
constant with tkinter.constants, which is annoying. Is there a way
around that prohibition (and what's it for, anyway?)

So I kludged the problem a bit by letting K = tkinter.constants, but
I'd still just like to use the constants without any qualifiers, which
I can do without a function. Except the error routine doesn't belong
in the main program, IMHO, since it fuddles the main logic of getting
a Fibonacci sequence. I want it out of the way and callable.

(The message box appeared behind my editor, and was not visible, which
drove me nuts, but it works fine at the dos prompt ;')

#Using Python 3.3.2 on Win 7 - standard project, standard test file
"""Print a user-chosen Fibonacci sequence"""
x = 0
fib = [0,1]
attempts = 0

def die_on_errors():
    """Throw up a message box and quit if too many bad entries are made"""
    import tkinter
    import tkinter.constants
    K = tkinter.constants
    tk = tkinter.Tk()
    frame = tkinter.Frame(tk, relief=K.GROOVE, borderwidth=5)
    frame.pack(fill=K.BOTH,expand=1)
    label = tkinter.Label(frame, text="\n  Too many errors, quitting  \n  ")
    label.pack(fill=K.X, expand=1)
    tk.mainloop()

user_prompt = "How long do you want your Fibonacci sequence?"
user_error = "That's not an integer, please enter the desired integer length\
 of your Fibonacci series again."

while True:
    try:
        fiblong = int(input(user_prompt))
        break
    except ValueError:
        attempts += 1
        if attempts > 6:
            die_on_errors()
            exit('bye, bye')
        user_prompt = user_error

for cnt in range(0,fiblong - 2):
    fib.append(fib[x] + fib[x+1])
    x += 1

print(fib)


-- 
Jim
"Would you use Internet Explorer if someone put a gun to your head?"
"How big is the gun?"


More information about the Tutor mailing list