我正在做一个聊天室。在我的代码中,我打开一个登录窗口和一个用户窗口。然后,我从列表框中选择用户名,然后点击课程Start Chat
MainScreen
在我的临时尝试中,点击该按钮后,它应该创建一个带有username
的十分之一的子窗口。也就是说,如果我选择" A" " B"和" C"在listbox2
中,它应该打开三个带标题的窗口" A" " B"和" C"
但是,我不想打开两个标题相同的窗口。如果我选择已在窗口标题中使用的用户名,则应将该窗口拉到前面。
我知道有一个函数可以检查子窗口是否存在。但它无法检测到窗口的标题。这些窗口具有相同的实例(我是否正确描述了它?)但函数ChatScreen
中的标题不同。那我该怎么办?
我的代码与我描述的完全没有关系,因为我只是制作临时代码。我只是想知道如何通过首先检测标题来检测窗口是否打开,然后我可以更改我的代码。
感谢您的帮助!
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
def sendFriendRequest(s, friend):
num = len(str(friend)) # count the len of my friends' name
if 22 + num < 100: # if it is short, so I can add three 0s
size = '000' + str(22 + num)
elif 22 + num >= 100: # if it is long, I have to consider another situation
size = '00' + str(22 + num)
s.send('@' + size + '@request@friend@' + friend)
data = s.recv(512)
if "@ok" in data:
SendRequestDialog()
return True
else:
print False
return False
def acceptFriendRequest(s, friend):
num = len(str(friend)) # count the len of my friends' name
if 21 + num < 100: # if it is short, so I can add three 0s
size = '000' + str(21 + num)
elif 21 + num >= 100: # if it is long, I have to consider another situation
size = '00' + str(21 + num)
s.send('@' + size + '@accept@friend@' + friend)
data = s.recv(512)
if "@ok" in data:
Second.update()
return True
else:
return False
########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.SendRequest)
self.button1.grid(row = 3, column = 100)
self.button2 = Button(frame, text = "Start Chat", command = ChatScreen)
self.button2.grid(row = 3, column = 300)
self.button3 = Button(frame, text = "Accept Request", command = self.AcceptRequest)
self.button3.grid(row = 3, column = 500)
users = getUsers(ss)
self.listbox1 = Listbox(frame)
self.listbox1.grid(row = 2, column = 100)
for item in users:
self.listbox1.insert(END, item)
self.value1 = str((self.listbox1.get(ACTIVE)))
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 SendRequest(self):
friends = self.value1
sendFriendRequest(ss, friends)
def AcceptRequest(self):
Accept = str((self.listbox3.get(ACTIVE)))
num = self.listbox3.curselection()
num = int(num[0])
if acceptFriendRequest(ss, Accept) == True:
self.listbox2.insert(END, Accept)
self.listbox3.delete(num)
def StartChat(self):
global chatguy
chatguy = str((self.listbox2.get(ACTIVE)))
def ChatScreen():
aaa = Toplevel(First)
aaa.title("aaa1")
aaa.geometry("300x200")
def SendRequestDialog():
Third = Toplevel(First)
Third.title("Send Successfully")
Third.geometry("300x100")
lab = Label(Third, text = "The friend request was successfully sent")
lab.pack()
def openMainScreen():
global Second
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)
由于TypeError: <function SlidersApp.create.<locals>.<lambda> at 0x106b09158> is not JSON serializable
是窗口小部件树中所有窗口的父级,因此您可以检查First
的子级以检测已打开的窗口。像这样:
First