2016-02-13 79 views
0

我的保存和读取功能无法正常工作。我是否正确地将数据保存在文本文件中?我是否正确读取文本文件中的数据?有人可以纠正我,如果保存和读取文本文件不正确。我认为其他代码也适用。注册用户名和密码

import Tkinter 
WindowBox = Tkinter.Tk() 
WindowBox.geometry("250x200") 
WindowBox.title("Welcome to E-UPSR") 

getusername1 = Tkinter.StringVar() 
getpassword1 = Tkinter.StringVar() 
getusername2 = Tkinter.StringVar() 
getpassword2 = Tkinter.StringVar() 

LabelName = Tkinter.Label (WindowBox, text="Username:") 
LabelName.pack() 
TxtBoxName = Tkinter.Entry (WindowBox, textvariable= getusername1) 
TxtBoxName.pack() 

LabelName = Tkinter.Label (WindowBox, text="Password:") 
LabelName.pack() 
TxtBoxName = Tkinter.Entry (WindowBox, textvariable= getpassword1) 
TxtBoxName.pack() 

student=[] 


def read(): 
    if len(getusername1.get()) or len(getpassword1.get())==0: 
      labelShowName=Tkinter.Label(WindowBox, text="Invalid").pack() 
    else: 
     addstudent = open ("student.txt", "w") 
     addstudent.read("Username:" + getusername1.get()) 
     addstudent.read("Password: " + getpassword1.get()) 
     addstudent.close() 
     WindowBox.withdraw() 
     MenuBox.deiconify() 
    return 

def register(): 
    WindowBox.withdraw() 
    RegBox.deiconify() 
    return 

RegBox = Tkinter.Tk() 
RegBox.geometry("250x200") 
RegBox.title("register") 

LabelName = Tkinter.Label (RegBox, text="Username:") 
LabelName.pack() 
TxtBoxName = Tkinter.Entry (RegBox, textvariable= getusername2) 
TxtBoxName.pack() 
LabelName = Tkinter.Label (RegBox, text="Password:") 
LabelName.pack() 
TxtBoxName = Tkinter.Entry (RegBox, textvariable= getpassword2) 
TxtBoxName.pack() 
RegBox.withdraw() 

def back(): 
    RegBox.withdraw() 
    WindowBox.deiconify() 
    return  


def save(): 
    while True: 
     if len(getusername2.get())== 0 or len(getpassword2.get())== 0: 
      labelShowName=Tkinter.Label(RegBox, text="Please key-in").pack() 
      break 
     else: 
      addstudent = open ("student.txt", "w") 
      addstudent.write('Username:' + getusername2.get()) 
      addstudent.write('Password:' + getpassword2.get()) 
      labelShowName=Tkinter.Label(RegBox, text="Done").pack() 
      return 
    len(getusername2.get() and getpassword2.get())!= 0 
    return 

MenuBox = Tkinter.Tk() 
MenuBox.geometry("250x200") 
MenuBox.title("MainMenu") 
MenuBox.withdraw() 

BtnName = Tkinter.Button (RegBox, text="Back", command=back).pack() 
BtnName = Tkinter.Button (RegBox, text="Enter", command=save).pack() 
BtnName = Tkinter.Button (WindowBox, text="Register", command=register).pack() 
BtnName = Tkinter.Button (WindowBox, text="Proceed", command=read).pack() 
+0

@WoLy tq非常感谢你的工作。你能成为我的老师吗? – student

回答

1

当你写open ("student.txt", "w")student.txt文件将在每次调用保存方法时ecrased。
您需要添加,使用方法:

open("student.txt", "a") 

使用两个Tk窗口,最好使用TopLevel窗口为。

read()方法,来测试用户名和密码使用:

def read(): 
    if not getusername1.get() or not getpassword1.get(): 
     #Your code 

save()方法,你不需要while True

def save(): 
    if not getusername2.get() or not getpassword2.get(): 
     #Show an error massage 
    else: 
     #Save the file 

为了展示,你可以使用tkmassagebox.showerror()的错误信息:

import tkMessageBox 
. 
. 
. 
tkMessageBox.showerror('Invalid', 'Empty username or password') 

在bot你写^ h read()save()方法:

labelShowName=Tkinter.Label(WindowBox, text="Invalid").pack() 

labelShowName变量将是None因为pack()方法不返回任何东西,以保持参照Label你可以使用:

labelShowName=Tkinter.Label(WindowBox, text="Invalid") 
labelShowName.pack() 

都是一样的东西线如:

BtnName = Tkinter.Button (RegBox, text="Back", command=back).pack() 

改为使用:

BtnName = Tkinter.Button (RegBox, text="Back", command=back) 
BtnName.pack() 

如果您不需要参考保持到该按钮使用:

Tkinter.Button (RegBox, text="Back", command=back).pack() 

您正在使用相同的变量名与differente Tkinter.Entry()窗口小部件,你768,16混淆这一点。

编辑:

import Tkinter 
import tkMessageBox 
WindowBox = Tkinter.Tk() 
WindowBox.geometry("250x200") 
WindowBox.title("Welcome to E-UPSR") 


Tkinter.Label (WindowBox, text="Username:").pack() 

username1 = Tkinter.Entry (WindowBox) 
username1.pack() 

Tkinter.Label (WindowBox, text="Password:").pack() 

password1 = Tkinter.Entry (WindowBox) 
password1.pack() 

student=[] 


def read(): 
    if not username1.get() or not password1.get(): 
     tkMessageBox.showerror('Invalid', 'Empty username or password') 
    else: 
     addstudent = open ("student.txt", "r") 
     lines = addstudent.readlines() 
     addstudent.close() 
     i = 0 
     while i < len(lines) - 1: 
      # username and password are saved in two line, label and value are separated by ':'. 
      # to get them we need to reed two line in each iteration and split with ':' to get the value (second result of spliting) then strip to remove end line. 
      user = lines[i].split(':')[1].strip() 
      password = lines[i+1].split(':')[1].strip() 
      # test if the user is registred 
      if user == username1.get() and password == password1.get(): 
       WindowBox.withdraw() 
       MenuBox.deiconify() 
       break 
      i += 2 
    return 

def register(): 
    WindowBox.withdraw() 
    RegBox.deiconify() 
    return 

RegBox = Tkinter.Tk() 
RegBox.geometry("250x200") 
RegBox.title("register") 

Tkinter.Label (RegBox, text="Username:").pack() 

username2 = Tkinter.Entry (RegBox) 
username2.pack() 

Tkinter.Label (RegBox, text="Password:").pack() 
password2 = Tkinter.Entry (RegBox) 
password2.pack() 
RegBox.withdraw() 

def back(): 
    RegBox.withdraw() 
    WindowBox.deiconify() 
    return  


def save(): 
    if not username2.get() or not password2.get(): 
     tkMessageBox.showerror('Invalid', 'Empty username or password') 
    else: 
     addstudent = open ("student.txt", "a") 
     addstudent.write('Username:' + username2.get() + '\n') 
     addstudent.write('Password:' + password2.get()+'\n') 
     tkMessageBox.showinfo("Writing", "Done") 
    return 

MenuBox = Tkinter.Tk() 
MenuBox.geometry("250x200") 
MenuBox.title("MainMenu") 
MenuBox.withdraw() 

Tkinter.Button (RegBox, text="Back", command=back).pack() 
Tkinter.Button (RegBox, text="Enter", command=save).pack() 
Tkinter.Button (WindowBox, text="Register", command=register).pack() 
Tkinter.Button (WindowBox, text="Proceed", command=read).pack() 


WindowBox.mainloop() 

我没有用Tkinter.StringVar()

+0

如果文本框为空,会发生什么情况?读取函数中的 – student

+0

代码如何查看ike? – student

+0

它显示tkmassagebox没有定义 – student