SyntaxError:标识符python 3.6中的无效字符

时间:2018-04-23 10:26:46

标签: python tkinter

from tkinter import *
from tkinter import messagebox as ms
import sqlite3
with sqlite3.connect('quit.db') as db:
c = db.cursor()
c.execute('CREATE TABLE IF NOT EXISTS user (username TEXT NOT NULL ,password TEXT NOT NULL);')
db.commit()
db.close()
class main:
def __init__(self, master):
    # Window
    self.master = master
    # Some Usefull variables
    self.username = StringVar()
    self.password = StringVar()
    self.n_username = StringVar()
    self.n_password = StringVar()
    # Create Widgets
    self.widgets()

# Login Function
def login(self):
    # Establish Connection
    with sqlite3.connect('quit.db') as db:
        c = db.cursor()
    # Find user If there is any take proper action
    find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
    c.execute(find_user, [(self.username.get()), (self.password.get())])
    result = c.fetchall()
    if result:
        self.logf.pack_forget()
        self.head['text'] = self.username.get() + '\n Loged In'
        self.head['pady'] = 150
    else:
        ms.showerror('Oops!', 'Username Not Found.')

def new_user(self):
    # Establish Connection
    with sqlite3.connect('quit.db') as db:
        c = db.cursor()

    # Find Existing username if any take proper action
    find_user = ('SELECT * FROM user WHERE username = ?')
    c.execute(find_user, [(self.username.get())])
    if c.fetchall():
        ms.showerror('Error!', 'Username Taken Try a Diffrent One.')
    else:
        ms.showinfo('Success!', 'Account Created!')
        self.log()
    # Create New Account
    insert = 'INSERT INTO user(username,password) VALUES(?,?)'
    c.execute(insert, [(self.n_username.get()), (self.n_password.get())])
    db.commit()

    # Frame Packing Methords

def log(self):
    self.username.set('')
    self.password.set('')
    self.crf.pack_forget()
    self.head['text'] = 'LOGIN'
    self.logf.pack()

def cr(self):
    self.n_username.set('')
    self.n_password.set('')
    self.logf.pack_forget()
    self.head['text'] = 'Create Account'
    self.crf.pack()

# Draw Widgets
def widgets(self):
    self.head = Label(self.master, text='LOGIN', font=('', 35), pady=10)
    self.head.pack()
    self.logf = Frame(self.master, padx=10, pady=10)
    Label(self.logf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
    Entry(self.logf, textvariable=self.username, bd=5, font=('', 15)).grid(row=0, column=1)
    Label(self.logf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
    Entry(self.logf, textvariable=self.password, bd=5, font=('', 15), show='*').grid(row=1, column=1)
    Button(self.logf, text=' Login ', bd=3, font=('', 15), padx=5, pady=5, command=self.login).grid()
    Button(self.logf, text=' Create Account ', bd=3, font=('', 15), padx=5, pady=5, command=self.cr).grid(row=2,
                                                                                                          column=1)
    self.logf.pack()

    self.crf = Frame(self.master, padx=10, pady=10)
    Label(self.crf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
    Entry(self.crf, textvariable=self.n_username, bd=5, font=('', 15)).grid(row=0, column=1)
    Label(self.crf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
    Entry(self.crf, textvariable=self.n_password, bd=5, font=('', 15), show='*').grid(row=1, column=1)
    Button(self.crf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.new_user).grid()
    Button(self.crf, text='Go to Login', bd=3, font=('', 15), padx=5, pady=5, command=self.log).grid(row=2,
                                                                                                     column=1)


if __name__ == '__main__':
   # Create Object
   # and setup window
   root = Tk()
   root.title('Login Form')
   # root.geometry('400x350+300+300')
   main(root)
   root.mainloop()

我的错误是:        root.mainloop()                      ^        SyntaxError:标识符中的无效字符  我检查了一切。无法克服这个问题。请帮助。我也调查过其他人的解决方案,找不到任何帮助。如果有人可以调查此事,我将非常感激。 还附上我的错误截图:

1 个答案:

答案 0 :(得分:0)

在最后一行的末尾有一个unicode字符。

在代码上运行.encode()时,会产生以下最后一行:

root.mainloop()\xef\xbb\xbf

你应该以某种方式移除角色。也许删除整行。