0

I have written a basic python socket based chat program (My second one) and i would like to add some visuals to make it a little more user friendly.

  1. Should I layer the visuals over the existing program or should i make a new program around the visuals

  2. What python module should i use for the visuals (i know pygame is that suitable)

  3. Can i have some form of general idea on how to write this (Code examples maybe?)

Here is my existing code:

Client:
import socket, threading

#Setup The Variables
WindowTitle = 'Chat 2.0 - Client'
s = socket.socket()
host = raw_input("Please Enter External IP adress Here: ")
print
host = socket.gethostname()
port = 8008

#Attempted To Connect
print "Conecting..."
print

while True:
    try:
        s.connect((host, port))
        break
    except:
        pass

print "Connected To " + str(host) + " " + str(port)
print

#Check For Incomming Data

def check_for_data():
    while True:
        data = s.recv(1024)
        if data:
            print
            print "Other: " + str(data)
            print
            print "You: "
        else:
            print "Client closed connection"
            s.close()
            break

#Send Data

def send_data():
    while True:
        user_input = raw_input("You: ")
        print
        s.sendall(user_input)

#Start Threads \ Main Loop
t = threading.Thread(target=send_data)
t.daemon = True
t.start() #1

check_for_data()

s.close

Server:
import socket, threading

#Setup The Variables
WindowTitle = 'Chat 2.0 - Client'
host = socket.gethostname()
port = 8008

s = socket.socket()

s.bind((host, port))

print "Awaiting Connection..."
print

s.listen(5)

c, addr = s.accept()

print "Connection From: " + str(addr)
print

def check_for_data(c):
    while True:
        data = c.recv(1024)
        if data:
            print
            print "Other: " + str(data)
            print
            print "You: "
        else:
            print "Client closed connection"
            c.close()
            break

def send_data():
    while True:
        message = raw_input("You: ")
        print
        c.sendall(message)

#Start Threads \ Main Loop
t = threading.Thread(target=send_data)
t.daemon = True
t.start() #1

check_for_data(c)    

c.close()
J.Clarke
  • 392
  • 2
  • 15
  • Preferably you should have the GUI be separate from the inner workings of the program. You might also want to check out [`tkinter`](https://wiki.python.org/moin/TkInter) for your GUI. I made a Monopoly calculator over at my [GitHub](https://github.com/chuckoy/monopoly-cash-tracker), but I can't give you any guarantees regarding best practices, though I did my best at the time. – Chuck Mar 09 '16 at 09:33
  • 1
    Have to agree that tkinter is probably the better way to go here. For a chat program, pygame's sprites/rects/surfaces all have little use. However, tkinter has buttons and labels and other similar things built in that would suit your needs a bit better. Btw, to make your life with socket easier, look into the makefile method. – David Jay Brady Mar 09 '16 at 14:55
  • @DavidJayBrady Could you please post your comment as an answer so i can wrap up this question :) – J.Clarke Mar 09 '16 at 22:26
  • 1
    Yep! Also added some things to address your 3 questions. Let me know if you have any follow up questions – David Jay Brady Mar 09 '16 at 23:24
  • @DavidJayBrady Thanks :) – J.Clarke Mar 09 '16 at 23:48
  • @DavidJayBrady Tkinter is really good, but how do i position the button? – J.Clarke Mar 09 '16 at 23:48
  • button_frame = tkinter.Frame(master = self.option_window) button_frame.grid(row = 5, column = 1) ok_button = tkinter.Button(master = button_frame, text = 'OK', font = DEFAULT_FONT, command = self.on_ok_clicked) – David Jay Brady Mar 10 '16 at 00:51

1 Answers1

2

Have to agree that tkinter is probably the better way to go here. For a chat program, pygame's sprites/rects/surfaces all have little use. However, tkinter has buttons and labels and other similar things built in that would suit your needs a bit better. Btw, to make your life with socket easier, look into the makefile method. The makefile method allows for much easier use. I recommend looking at Socket.error [Error 10060] for a description of how to use it and its uses. It's not necessary, just makes life easier :)

Edit: "Should I layer the visuals over the existing program or should i make a new program around the visuals"

Not quite sure what you mean here by "existing program." When it comes to what you should code, split up your logic and user interface stuff. So have a module that handles the sending and receiving of messages, and another that handles the display.

"What python module should i use for the visuals (i know pygame is that suitable)"

Probably tkinter. I only have experience in tkinter and pygame, but of the two, you probably want tkinter for this. I explained why in the first paragraph.

"Can i have some form of general idea on how to write this (Code examples maybe?)"

Assuming you use tkinter, look into stringvars, they may or may not be useful for you. As for the structure of your program, I'm not exactly sure what you want so I can't help you there. But do start simple. For example, get messages to send between programs, and print them out. Then have the messages show up on a window.. Make a way for the user to type in message via GUI (look into tkinter entry widget!). Best of luck you!

Edit 2: To answer your question about how to position button. Here is some code from my last project where I had to use a button to do something.

    button_frame = tkinter.Frame(master = self.option_window)
    button_frame.grid(row = 5, column = 1)

    ok_button = tkinter.Button(master = button_frame, text = 'OK',
                               font = DEFAULT_FONT, command = self.on_ok_clicked)

The position of the button is based off of where I did the button_frame.grid(....). To organize your tkinter window, I recommend using grid and not pack.

Oh, and self.option_window was a tkinter.Tk() object in my case.

Community
  • 1
  • 1
David Jay Brady
  • 1,034
  • 8
  • 20