As the title suggests, there are some major flaws in my new program, though it still functions nonetheless. Here's the code:

POS = 1

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

import os

def clear():
    if os.name == "nt":
        os.system("cls")
    else:
        print ("\n"*40)

def key(event):
    clear()
    global POS
    global MENU
    MENU = "Nothing selected"
    if event.keysym == 'Escape':
        root.destroy()
    if (event.keysym in ["Up", "Down"]) == True:
        direction = event.keysym
        if direction == "Down":
            if POS == 9:
                POS = 1
            else:
                POS += 1
        if direction == "Up":
            if POS == 1:
                POS = 9
            else:
                POS -= 1
    if (event.keysym) == "Return":
        MENU = str(POS)

    if POS == 1:
        POS1 = "->"
    else:
        POS1 = "  "

    if POS == 2:
        POS2 = "->"
    else:
        POS2 = "  "
        
    if POS == 3:
        POS3 = "->"
    else:
        POS3 = "  "

    if POS == 4:
        POS4 = "->"
    else:
        POS4 = "  "

    if POS == 5:
        POS5 = "->"
    else:
        POS5 = "  "

    if POS == 6:
        POS6 = "->"
    else:
        POS6 = "  "

    if POS == 7:
        POS7 = "->"
    else:
        POS7 = "  "

    if POS == 8:
        POS8 = "->"
    else:
        POS8 = "  "

    if POS == 9:
        POS9 = "->"
    else:
        POS9 = "  "
        
    if POS == 1:
        POS1 = "->"
    else:
        POS1 = "  "

    print (" Press escape to quit")
    print ("---------------------")
    print (POS1, "1) Position 1")
    print (POS2, "2) Position 2")
    print (POS3, "3) Position 3")
    print (POS4, "4) Position 4")
    print (POS5, "5) Position 5")
    print (POS6, "6) Position 6")
    print (POS7, "7) Position 7")
    print (POS8, "8) Position 8")
    print (POS9, "9) Position 9")
    print ("---------------------")
    print (MENU)
    
print ("Welcome to InterfaceTest v0.1 alpha")
print ("Hit any key to begin")

root = tk.Tk()
root.bind_all('<Key>', key)
root.withdraw()
root.mainloop()

You first of all probably noticed that you can't click any where with the mouse other wise it doesn't work. This isn't really debug friendly either, because it isn't compatible with most IDE's. You also see the part where you have lots of repetitive code. Would've used a for loop but I can't seem to figure out how to combine the loop variable and the normal variable.
SO.....
Other than that it's all working hunky dory, not really... I need to find out some way to fix the clicking thing, and optimize that big block of repetitive code in the middle of the script. I've tried everything and nothing seems to work.

Recommended Answers

All 2 Replies

Member Avatar for masterofpuppets

hi, I really don't get what the program is supposed to do, if you could give some sample input and output of what you want it to do it would be helpful.
As for the big chunk of code that you have there in the middle, i.e

if POS == 1:
        POS1 = "->"
    else:
        POS1 = "  "
 
    if POS == 2:
        POS2 = "->"
    else:
        POS2 = "  "
 
    if POS == 3:
        POS3 = "->"
    else:
        POS3 = "  "
 
    if POS == 4:
        POS4 = "->"
    else:
        POS4 = "  "
 
    if POS == 5:
        POS5 = "->"
    else:
        POS5 = "  "
 
    if POS == 6:
        POS6 = "->"
    else:
        POS6 = "  "
 
    if POS == 7:
        POS7 = "->"
    else:
        POS7 = "  "
 
    if POS == 8:
        POS8 = "->"
    else:
        POS8 = "  "
 
    if POS == 9:
        POS9 = "->"
    else:
        POS9 = "  "
 
    if POS == 1:
        POS1 = "->"
    else:
        POS1 = "  "

i suggest using a dictionary to hold the values of POS, like this

posValues = {}
for i in range( 1, 10 ):
     posValues[ str( i ) ] = "  " #this is double space

#you'll get a dictionary that looks like this
posValues = { "1":"  ", "2":"  "....."9":"  " }

#and now you can do the checking...
if posValues.has_key( str( POS ) ):
   posValues[ str( POS ) ] = "->"
else:
   #do something else...

#and now to print everithing
print (" Press escape to quit")
print ("---------------------")
print (posValues[ "1" ], "1) Position 1")
print (posValues[ "2" ], "2) Position 2")
print (posValues[ "3" ], "3) Position 3")
print (posValues[ "4" ], "4) Position 4")
print (posValues[ "5" ], "5) Position 5")
print (posValues[ "6" ], "6) Position 6")
print (posValues[ "7" ], "7) Position 7")
print (posValues[ "8" ], "8) Position 8")
print (posValues[ "9" ], "9) Position 9")
print ("---------------------")
print (MENU)

Note that you might want to restart the dictionary every time...
Sorry if that's confusing :)

Member Avatar for masterofpuppets

i just noticed that the printing part could also be simplified, i.e

#and now to print everithing
print (" Press escape to quit")
print ("---------------------")
print (posValues[ "1" ], "1) Position 1")
print (posValues[ "2" ], "2) Position 2")
print (posValues[ "3" ], "3) Position 3")
print (posValues[ "4" ], "4) Position 4")
print (posValues[ "5" ], "5) Position 5")
print (posValues[ "6" ], "6) Position 6")
print (posValues[ "7" ], "7) Position 7")
print (posValues[ "8" ], "8) Position 8")
print (posValues[ "9" ], "9) Position 9")
print ("---------------------")
print (MENU)

can be rewritten as

print (" Press escape to quit")
print ("---------------------")
for p in posValues:
     print (posValues[ p ], p, ") Position", p )
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.