我只是试验一个只在一台计算机上运行的消息程序。好的,这就是问题:如果我以用户身份登录并向另一个用户发送消息,即使我登录到接收用户的消息,也没有消息(它没有消息)。请帮帮我。您也可以提供自己的建议和简化代码的方法。
import Tkinter as tk
import sys
import tkMessageBox
import csv
accounts = dict()
messages = []
messagerec = []
messagesen = []
csvpath = "C:/Users/user1/Desktop/abc.csv"
csvreader = csv.reader(open(csvpath))
for y, z in csvreader:
accounts[y] = z
def GetUser():
user = userid.get()
return user
def GetPass():
password = passid.get()
return password
def SignUp():
def signupgo():
newuser = newuserid.get()
newpass = newpassid.get()
if newuser in accounts:
tkMessageBox.showerror("Username Taken", 'Sorry! The username you have requested has already been taken. Please try another username.' [2])
else:
accounts[newuser] = newpass
tkMessageBox.showinfo("Account Created", 'Congratulations! Your account has been created' [2])
newuserid = tk.StringVar()
newpassid = tk.StringVar()
SignUpWin = tk.Tk()
NewUserLabel = tk.Label(SignUpWin, text="New Username: ").pack()
NewUserInput = tk.Entry(SignUpWin,textvariable=newuserid).pack()
NewPassLabel = tk.Label(SignUpWin, text="New Password: ").pack()
NewPassInput = tk.Entry(SignUpWin, textvariable=newpassid).pack()
CreateAccount = tk.Button(SignUpWin, text="Create Account", command=signupgo).pack()
def logingo():
user = GetUser()
password = GetPass()
if user in accounts:
if accounts[user] == password:
LoggedIn(user)
elif accounts[user] != password:
tkMessageBox.showerror("Wrong Password", 'Try Again! You have entered the wrong password.')
elif user not in accounts:
tkMessageBox.showerror("User not existing", 'Try Again or Create an account! The username you have provided is not existing.')
def LoggedIn(user):
def MessageButtonClick():
if tkMessageBox.askquestion('Compose or Inbox?', 'Do you want to access your inbox(Yes) or compose a new message(No)?') == 'yes':
OpenInbox(user)
else:
MessageSender(user)
消息发送部分从此处开始。
def MessageSender(user):
messagerecvar = tk.StringVar()
messagecontentvar = tk.StringVar()
messagesenderwin = tk.Tk()
messagereclabel = tk.Label(messagesenderwin, text="Receiver:").pack()
messagerecinput = tk.Entry(messagesenderwin, textvariable=messagerecvar).pack()
messagecontentlabel = tk.Label(messagesenderwin, text="Content:").pack()
messagecontentinput = tk.Entry(messagesenderwin, textvariable=messagecontentvar).pack()
messagecontent = messagecontentvar.get()
messagerec = messagerecvar.get()
messagesendgobutton = tk.Button(messagesenderwin, text='Send Message', command=lambda:sendmessagego(messagecontent, user, messagerec)).pack()
def sendmessagego(content, sender, receiver):
messages.append(content)
messageno = len(messages)
messagerec.append(receiver)
messagesen.append(sender)
tkMessageBox.showinfo("Message Sent", 'Your message has been sent.')
def OpenInbox(user):
if 'a' in messagerec:
lenmess = messagerec.index(user)
tkMessageBox.showinfo('Message from '+messagesen[lenmess], 'Message from '+messagesen[lenmess]+': '+messages[lenmess])
elif user not in messagerec:
tkMessageBox.showinfo('No Messages', 'Sorry, no messages were found')
loggedinwin = tk.Tk()
tkMessageBox.showinfo("Welcome", 'Hello, '+user)
HomeLabel = tk.Label(loggedinwin, text="Home").pack()
MessageMenuButton = tk.Button(loggedinwin, text="Messaging", command=MessageButtonClick).pack()
maingui = tk.Tk()
userid = tk.StringVar()
passid = tk.StringVar()
UserEnterLabel = tk.Label(maingui, text="Username: ").pack()
UserInput = tk.Entry(maingui, textvariable=userid).pack()
PassEnterLabel = tk.Label(maingui, text="Password: ").pack()
PassInput = tk.Entry(maingui, textvariable=passid).pack()
LoginGo = tk.Button(maingui, text="Login", command=logingo).pack()
SignUpGo = tk.Button(maingui, text="Sign Up", command=SignUp).pack()
maingui.mainloop()
答案 0 :(得分:0)
第一个问题是您要创建多个Tk
的实例。 Tkinter并未设计为以这种方式工作,并且会产生意想不到的结果。如果您需要多个窗口,则需要创建Toplevel