Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem in Python

Options
  • 26-02-2008 10:29pm
    #1
    Registered Users Posts: 401 ✭✭


    Hi guy's,
    I'm new to python and can't understand why my if statements aren't working? I'm sure its a simple enough error but i just can't spot it, any help would be great.

    Heres my code:
    #!/usr/bin/env python
    
    gifdir = "/home/gifs/"
    
    from Tkinter import *
    
    global canvas 
    global check 
    global check2 
    global check3 
    global check4 
    
    check = False
    check2 = False
    check3 = False
    check4 = False
    
    def forward(event):
        print 'f'
        global Forward
        Forward = canvas.create_image(200, 50, image=imgFwd, anchor=CENTER)
        check = True     
        
        if check2 == True:            
            canvas.delete(Reverse)
        
        if check3 == True:    
            canvas.delete(Left)    
        
        if check4 == True:
            canvas.delete(Right)
    
    
    def reverse(event):
             
        print 's'
        global Reverse
        Reverse = canvas.create_image(200, 350, image=imgRev, anchor=CENTER)
        check2 = True    
        
        if check == True:             
            canvas.delete(Forward)
                            
        if check3 == True:    
            canvas.delete(Left)    
        
        if check4 == True:
            canvas.delete(Right)
    
    def left(event):
        print'a'
        global Left    
        Left = canvas.create_image(50, 200, image=imgLef, anchor=CENTER)    
        check3 = True
        
        if check == True:    
            canvas.delete(Forward)
    
        if check2 == True:    
            canvas.delete(Reverse)    
        
        if check4 == True:
            canvas.delete(Right)    
        
    def right(event):    
        print'd'
        global Right
        Right = canvas.create_image(350, 200, image=imgRig, anchor=CENTER)
        check4 = True    
        
        if check == True:    
            canvas.delete(Forward)
    
        if check2 == True:    
            canvas.delete(Reverse)
        
        if check3 == True:
            canvas.delete(Left)
    
    def quit(event):
        print'exit'
        s.close()    
        sys.exit()
    
    tkroot = Tk()
    canvas = Canvas(width=400, height=400, bg='white')   # 0,0 is top left corner 
    canvas.pack(expand=YES, fill=BOTH)                   # increases down, right 
    
    canvas.create_oval(70, 70, 330, 330, width=2, fill='blue')
    canvas.create_text(200, 200, font = 'bold', text='Robot')
    imgFwd = PhotoImage(file=gifdir+"forward.gif")
    imgRev = PhotoImage(file=gifdir+"forward.gif")
    imgLef = PhotoImage(file=gifdir+"forward.gif")
    imgRig = PhotoImage(file=gifdir+"forward.gif")
    
    tkroot.bind('<Key-w>',forward)
    tkroot.bind('<Key-a>',left)
    tkroot.bind('<Key-d>',right)
    tkroot.bind('<Key-s>',reverse)
    tkroot.bind('<Key-Escape>',quit)
    tkroot.mainloop()
    


Comments

  • Closed Accounts Posts: 413 ✭✭sobriquet


    What exactly is going wrong?
    sharkDawg wrote: »
    canvas.delete(Test)
    
    That looks out of place to start.

    Also I don't know Tk, but how is canvas related to tkroot?


  • Registered Users Posts: 401 ✭✭sharkDawg


    That was a typo, thanks, the issue is when say 'w' is pressed the .gif file is displayed on the canvas, but then when 's' is pressed the .gif file also appears in a different location but the original one form 'w' doesn't get deleted. The if statements don't seem to get read at all, i put print statements into them, but still got nothing


  • Closed Accounts Posts: 413 ✭✭sobriquet


    I'm at work so I can't really look into it properly but try declaring Forward, Reverse etc outside the keyb handlers first, ie Forward=None, as with the check variables.

    Oh hang on, you added prints? The ifs will definitely be evaluated, I wouldn't doubt that. In that case look up some tutorials on using globals, the checks may be treated as constant values - I don't know tbh, I've never used globals vars like that and can't test it at the mo.

    You should try to use more descriptive variable names too, check1 etc is hard to read.


  • Registered Users Posts: 401 ✭✭sharkDawg


    Ya sorry about the variable names. I see now that the if statements aren't the problem that its my global variables! When 'w' is pressed, check is assigned to True in the forward method but once it goes outside that method check reverts back to its defined False. How can this be changed?
    I know its bad practice to use globals but i don't know any other way and don't have time to learn either!


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Know absolutely nothing with Python but has it got a 'Pass by Reference' mechanism? - quick google suggests that Python passes by reference by default? :confused:


  • Advertisement
  • Closed Accounts Posts: 413 ✭✭sobriquet


    sharkDawg wrote: »
    Ya sorry about the variable names. I see now that the if statements aren't the problem that its my global variables! When 'w' is pressed, check is assigned to True in the forward method but once it goes outside that method check reverts back to its defined False. How can this be changed?
    I know its bad practice to use globals but i don't know any other way and don't have time to learn either!

    Well, to be honest I don't know off hand how to make the globals work, but without thinking it through, my first thought would be wrap the 'Robot' state up into a class, with variables for isMovingForward and methods for Forward() etc, something like:
    class Robot:
    	def __init__(self):
    		self.isMovingForward = False
    		self.isMovingBackward = False
    		self.isMovingLeft = False
    		self.isMovingRight = False
    
    	def Forward(self, event):
    		## Your Tk code
    		self.isMovingForward = True
    		
    		if isMovingBackward == True:
    			## Your Tk code
    
    robot = Robot()
    tkroot.bind('<Key-w>', robot.Forward) # No idea if this works
    
    Completely untested and without warranty, no refunds. Hope if helps anyway. For future reference there's probably a cleaner way to pass in the canvas object to do your Tk code, accessing a global from within a class is ugly. Hope it helps though.


  • Registered Users Posts: 401 ✭✭sharkDawg


    Thanks for the reply guys, got it sorted. Just involved moving the global variables inside the methods, i know it not the best/proper way to do it but it works so i'm not touching it! I'll post up the code later


  • Registered Users Posts: 401 ✭✭sharkDawg


    Here it is:
    #!/usr/bin/env python
    
    gifdir = "/home/enda/gifs/"
    
    from Tkinter import *
    import tkSnack
    import sys
    
    
    
    
    def forward(event):
        
        print 'f'
        global check
        global Forward
        Forward = canvas.create_image(200, 50, image=imgFwd, anchor=CENTER)
        check = True     
        mysound.stop()    
    
        if check2 == True:            
            canvas.delete(Reverse)
        
        if check3 == True:    
            canvas.delete(Left)    
        
        if check4 == True:
            canvas.delete(Right)
    
        if check5 == True:
            canvas.delete(Stop)
    
    
    
    def reverse(event):
        print 's'
        global check2
        global Reverse
        Reverse = canvas.create_image(200, 350, image=imgRev, anchor=CENTER)
        mysound.play()    
        check2 = True    
        
        if check == True:             
            canvas.delete(Forward)
                            
        if check3 == True:    
            canvas.delete(Left)    
        
        if check4 == True:
            canvas.delete(Right)
    
        if  check5 == True:
            canvas.delete(Stop)
        
    
    
    def left(event):
        print'a'
        global check3
        global Left    
        mysound.stop()    
        Left = canvas.create_image(50, 200, image=imgLef, anchor=CENTER)    
        check3 = True
        
        if check == True:    
            canvas.delete(Forward)
    
        if check2 == True:    
            canvas.delete(Reverse)    
        
        if check4 == True:
            canvas.delete(Right)
    
        if check5 == True:
            canvas.delete(Stop)    
    
    
        
    def right(event):    
        print'd'
        global check4
        global Right
        mysound.stop()    
        Right = canvas.create_image(350, 200, image=imgRig, anchor=CENTER)
        check4 = True    
        
        if check == True:    
            canvas.delete(Forward)
    
        if check2 == True:    
            canvas.delete(Reverse)
        
        if check3 == True:
            canvas.delete(Left)
    
        if check5 == True:
            canvas.delete(Stop)
    
    
     
    
    def stop(event):    
        print'p'
        global check5
        global Stop
        mysound.stop()    
        Stop = canvas.create_image(200, 200, image=imgRig, anchor=CENTER)
        check5 = True    
        
        if check == True:    
            canvas.delete(Forward)
    
        if check2 == True:    
            canvas.delete(Reverse)
        
        if check3 == True:
            canvas.delete(Left)
    
        if check4 == True:
            canvas.delete(Right)
    
    
    
    def quit(event):
        print'exit'
        sys.exit()
    
    
    tkroot = Tk()
    check = False
    check2 = False 
    check3 = False
    check4 = False
    check5 = False
     
    canvas = Canvas(width=400, height=400, bg='white')   # 0,0 is top left corner 
    canvas.pack(expand=YES, fill=BOTH)                   # increases down, right 
    
    canvas.create_oval(70, 70, 330, 330, width=2, fill='blue')
    
    imgFwd = PhotoImage(file=gifdir+"forward.gif")
    imgRev = PhotoImage(file=gifdir+"forward.gif")
    imgLef = PhotoImage(file=gifdir+"forward.gif")
    imgRig = PhotoImage(file=gifdir+"forward.gif")
    imgStp = PhotoImage(file=gifdir+"forward.gif")
    
    labelfont = ('helvetica',20,'bold')
    widget = Label(canvas, text='Robot', fg='white', bg='black', font = labelfont)
    canvas.create_window(200, 200, window=widget)
    
    tkSnack.initializeSnack(tkroot)
    global mysound
    mysound = tkSnack.Sound()
    mysound.read('/home/enda/gifs/1.mp3')
    
    
    tkroot.bind('<Key-w>',forward)
    tkroot.bind('<Key-a>',left)
    tkroot.bind('<Key-d>',right)
    tkroot.bind('<Key-s>',reverse)
    tkroot.bind('<Key-p>',stop)
    tkroot.bind('<Key-Escape>',quit)
    
    tkroot.mainloop()
    
    


Advertisement