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

Python socket issue with Tkinter

Options
  • 08-02-2008 4:20pm
    #1
    Registered Users Posts: 401 ✭✭


    Hi guys, im currently doing a robotics project in college, its a robot that has to be controlled from a pc over a wireless LAN connection. All was going well until i got to the server - client stage. I am using python to code these and can get a connection between them but cannot send characters from the client to the server. I haven't used python before so i may well have made some basic errors.

    Heres what i have:
    #!/usr/bin/env python
    
    from Tkinter import *
    import socket
    import sys
    import tkMessageBox
    
    def ifup():
       testConn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       try:
        host = 'localhost'
        port = 50002
        size = 1024
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host,port))
        tkMessageBox.showinfo('Connection Box',' Yes connected ')
       except:
        tkMessageBox.showinfo('Connection Box',' Not connected ')
    
    
    
    def forward(event):
        s.send('<Key-w>')
    
    def reverse(event):
        s.send('<Key-s>')
    
    def left(event):
        s.send('<Key-a>')
        
    def right(event):
        s.send('<Key-d>')
    
    def quit(event):
        s.send('<Key-Escape>')
        s.close()    
        sys.exit()
    
    ifup()
    
    tkroot = Tk()
    labelfont = ('courier',20,'bold')
    widget = Label(tkroot, text='I love Robotics!')
    widget.config(bg='yellow', font = labelfont)
    widget.config(height=5, width=20)
    widget.pack(expand=YES, fill = BOTH)
    widget.bind('<Key-w>',forward)
    widget.bind('<Key-a>',left)
    widget.bind('<Key-d>',right)
    widget.bind('<Key-s>',reverse)
    widget.bind('<Key-Escape>',quit)
    widget.focus()
    tkroot.mainloop()
    
    When I press any of the defined key's, i get an error like this
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "lib-tk/Tkinter.py", line 1406, in __call__
        return self.func(*args)
      File "/home/enda/test.py", line 23, in forward
        s.send('<Key-w>')
    NameError: global name 's' is not defined
    
    Is there something fundamental i'm doing wrong? Any help would be greatfully appreciated


Comments

  • Closed Accounts Posts: 413 ✭✭sobriquet


    sharkDawg wrote: »
    Is there something fundamental i'm doing wrong?
    Yep, it's a simple scoping issue: you declare 's' in ifup, and when that method ends, it goes out of scope. So in the method 'forward', you declare use 's' even though the interpreter has no idea what 's' is supposed to be.

    So you need to modify the scope of 's'. The best option might be to wrap up the ifup and forward (etc) methods you have into a class, have 's' as a class variable/property, and have forward/reverse etc stubs to wrap the appropriate class methods and to bind to the Tkinter widgets. (It might be possible to bind the widget to class.method directly, I'm not sure.)

    If you're not used Python, the issue doesn't come up until you run the 'forward' because it's dynamic - the expression 's.send(...)' isn't evaluated at all until the interpreter hits it. If you're used to Java, C# etc, you'd expect such a thing to be caught at compile time.


  • Registered Users Posts: 401 ✭✭sharkDawg


    Cheers man, thanks, it's quite obvious really, have it sorted now.


Advertisement