我正在使用tkinter和socket编写聊天室。当我成功登录时,我会获得服务器上所有用户的列表。然后,我从listbox1
中选择一项,然后按Send Request
。在这里,我暂时设置Send Request
来运行打印项目的功能。但是,无论我选择哪个项目,它都会打印()
我写过self.listbox1.curselection
。也许我错过了什么?
请帮帮我,谢谢!
from Tkinter import *
import socket
########HelperFunction########
def chunkstring (block): #Use to make the block into chunks and count the sum of ASCII value of chunks
M = []
for i in range(0, 512, 32):
L = str((block[0 + i : 32 + i]))
sum = 0
for r in range(len(L)):
sum = sum + ord(L[r])
M.append(sum)
return M
def leftrotate(x, c):
return (x << c) & 0xFFFFFFFF | (x >> (32 - c) & 0x7FFFFFFF >> (32 - c))
########Connection########
def StartConnection (IPAddress, PortNumber): #Use to set up the connection between computers and servers
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IPAddress, PortNumber))
return s
def login (s, username, password): #Login Function
print username
print password
s.send('LOGIN ' + username + '\n')
data = s.recv(512)
List = data.split(" ") #send the commend and get something back
CH = List[2] # pick up the CHALLENGE code
CH = CH[:-2] # delete the last two unnecessary code
PD = password
message = PD + CH # combine password and CHALLENGE together
block = message + "1"
block = block + "0" * (512 - len(message) - 3 - 1) # add '0' to block and remain the space for last three digits
numLen = len(str(len(message)))
if numLen == 2: #If the password is very long, we should consider the last digits may be affected
block = block + "0" + str(len(message))
elif numLen == 3:
block = block + str(len(message))
M = chunkstring(block)
########## MD5
P = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
K = [0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391]
#Initialize variables
a0 = 0x67452301
b0 = 0xefcdab89
c0 = 0x98badcfe
d0 = 0x10325476
A = a0
B = b0
C = c0
D = d0
#Mainloop
for i in range(0, 64):
if i >= 0 and i <= 15:
F = (B & C) | ((~ B) & D)
F = F & 0xFFFFFFFF
g = i
elif i >= 16 and i <= 31:
F = (D & B) | ((~ D) & C)
F = F & 0xFFFFFFFF
g = (5 * i + 1) % 16
elif i >= 32 and i <= 47:
F = B ^ C ^ D
F = F & 0xFFFFFFFF
g = (3 * i + 5) % 16
elif i >= 48 and i <= 63:
F = C ^ (B | (~ D))
F = F & 0xFFFFFFFF
g = (7 * i) % 16
dTemp = D
D = C
C = B
B = B + leftrotate((A + F + K[i] + M[g]), P[i])
B = B & 0xFFFFFFFF
A = dTemp
#Add this chunk's hash to result so far:
a0 = (a0 + A) & 0xFFFFFFFF
b0 = (b0 + B) & 0xFFFFFFFF
c0 = (c0 + C) & 0xFFFFFFFF
d0 = (d0 + D) & 0xFFFFFFFF
result = str(a0) + str(b0) + str(c0) + str(d0)
s.send("LOGIN " + username + " " + result + "\n") #send messagedigest to server
reply = s.recv(512)
print reply
if "Successful" in reply:
openMainScreen()
return True
else:
First.quit()
return False
def getUsers(s):
s.send('@users')
data = s.recv(512)
data = data.split('@') # use "@" help to split the list
data = data[4:] # start from the 4th element in order to avoid the elements I don't need
return data
def getFriends(s):
s.send('@friends')
data = s.recv(512)
data = data.split('@')
data = data[4:]
return data
def getRequests(s):
s.send('@rxrqst')
data = s.recv(512)
data = data.split('@')
data = data[3:]
return data
########Interface#########
#--------Login--------#
class Login(Frame):
def __init__(self, master):
frame = Frame(master)
frame.pack()
First.geometry("250x250")
self.lab1 = Label(frame, text = "Username")
self.lab1.grid(row = 0, column = 125)
self.ent1 = Entry(frame)
self.ent1.grid(row = 1, column = 125)
self.lab2 = Label(frame, text = "Password")
self.lab2.grid(row = 2, column = 125)
self.ent2 = Entry(frame, show = "*")
self.ent2.grid(row = 3, column = 125)
self.button = Button(frame, text = "OK", command = self.Submit)
self.button.grid(row = 5, column = 125)
def Submit(self):
username = self.ent1.get()
password = self.ent2.get()
login(ss, username, password)
class MainScreen(Frame):
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.lab1 = Label(frame, text = "All Users")
self.lab1.grid(row = 1, column = 100)
self.lab2 = Label(frame, text = "Your Friends")
self.lab2.grid(row = 1, column = 300)
self.lab3 = Label(frame, text = "Pending Requests")
self.lab3.grid(row = 1, column = 500)
self.button1 = Button(frame, text = "Send Request", command = self.Print)
self.button1.grid(row = 3, column = 100)
users = getUsers(ss)
self.listbox1 = Listbox(frame)
self.listbox1.grid(row = 2, column = 100)
for item in users:
self.listbox1.insert(END, item)
self.list1 = self.listbox1.curselection()
friends = getFriends(ss)
self.listbox2 = Listbox(frame)
self.listbox2.grid(row = 2, column = 300)
for item in friends:
self.listbox2.insert(END, item)
requests = getRequests(ss)
self.listbox3 = Listbox(frame)
self.listbox3.grid(row = 2, column = 500)
for item in requests:
self.listbox3.insert(END, item)
def Print(self):
print self.list1
def openMainScreen():
Second = Toplevel(First)
Second.title("Chat with Client")
Second.geometry("600x400")
mainscreen = MainScreen(Second)
First = Tk()
First.title("Login")
LoginScreen = Login(First)
ss = StartConnection("86.36.34.215", 15112)
First.mainloop()
答案 0 :(得分:0)
首先,您必须点击列表中的任何元素。
您必须在curselection()
Print()
def Print(self):
print self.listbox1.curselection()