Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Spyro543 on November 10, 2011, 05:53:41 pm

Title: Tkinter window help needed
Post by: Spyro543 on November 10, 2011, 05:53:41 pm
Alright, I am making my own home-made chat protocol!!  :o
I'm having problems with the Client GUI:
Code: (here's the code!) [Select]
import socket
import sys
s = socket.socket()

from Tkinter import *

def GetUsername():
    global username
    username = usrnam.get()
    s.connect(("anova.57o9.org", 5000))
    s.send("!!!JOIN " + username)
   
    UsrBox.destroy()
   

def SendMessage(messagetosend):
    s.send("<%s> %s" % (username, messagetosend))

def Disconnect():
    s.send("!!!QUIT " + username)
    s.shutdown()
    s.close()
    sys.exit()
   

UsrBox = Tk()

UsrBox.title("")

title = Label(UsrBox, text="Please enter a username.")
title.pack()

usrnam = Entry(UsrBox)
usrnam.pack()

b = Button(UsrBox, text="Connect", width=10, command=GetUsername)
b.pack()

UsrBox.mainloop()

master = Tk()
master.title("chat5000")

titlelbl = Label(master, text="Connected. Username is " + username)
titlelbl.pack()

msg = Text(master)
msg.pack()

e = Entry(master)
e.pack()
e.insert(END, "Enter message here")

send = Button(master, text="Send Message", command=SendMessage(e.get()))
send.pack()

disconbtn = Button(master, text="Disconnect", command=Disconnect)
disconbtn.pack()

while True:
    data = s.recv(2048)
    if data != "" or data != None:
        msg.insert(END, "\n" + data)

master.mainloop()

I click Run, and the first window shows up fine. I type in a username, and click Connect. I'm pretty sure it connects, then the window goes away cuz of UsrBox.destroy(). The main client window should come up, right? If it's not supposed to, how can I make it come up? (the server program is running on the server while I tested this, btw)