Use tkinter writing a login window

Tkinter Import
Import SYS
Import Re
Import mysql.connector
Top = tkinter.Tk ()
top.geometry ( '400x170 + 350 + 150')
top.wm title ( 'Examples of synthesis')
top.resizable (width = False, False height = ) not modify the window size
DEF validateText ():
Val = entry1.get ()
pwd = entry2.get ()
IF the re.findall ( '^ [0-9a-zA-the Z
]. 1 {,} $', STR (Val )):
return True
the else:
Label3 [ 'text'] = 'user names can contain letters, numbers, underscore'
return False
# --------------------- ----- obtain database information -----------------------------------
File Open = ( 'MySQL. the conf ',' R & lt ')
F = file.readlines ()
L = "" .join (F)
IP = the re.findall (R & lt "Host = (. \ S +)",l)
us = re.findall(r"user=(.\S+)", l)
the re.findall = PD (R & lt "the passwd = (. \ S +)", L)
Pt = the re.findall (R & lt "Port = (. \ S +)", L)
DB = the re.findall (R & lt "Database = (. \ S +) ", L)
# -------------------------- ------------ validate user login database -----------------------
DEF anw_button (DB_HOST = IP [0], US DB_USER = [0], db_pass = PD [0], Pt = DB_PORT [0], DB DB_DATA = [0]):
'' '
mydb = mysql.connector.connect (
host = "xx.xx.xx.xx", # host address database
user = "root", # database username
passwd = "123456", # database password
Port = "66",
database = "py_test"
)
'' '
db_user_name = entry1.get ()
mydb = mysql.connector.connect (
host = STR (DB_HOST), a host address database #
user = str (db_user),# Database user name
passwd = str (db_pass), # database password
port = str (db_port),
str = Database (DB_DATA)
)
MyCursor = mydb.cursor () # create a cursor
mycursor.execute ( "the SELECT * the FROM py_table the WHERE name =" + db_user_name)
Results = mycursor.fetchall ()
IF Results [0] [0]! = "":
db_user_pwd Results = [0] [. 1]
IF str.upper (entry1.get ()) == db_user_name and str.upper (entry2.get ()) == db_user_pwd:
Label3 [ 'text'] = 'login success '
top.destroy () # stops top.mainloop (), close the window
Import the Test
the Test
the else:
Label3 [' text '] =' user name or password is incorrect, please re-enter! '
DEF Registered ():
top.destroy ()
Import ZC
ZC
DEF down_zc ():
Label3 [' text '] =' Register is off '

label1 = tkinter.Label (top, text = ' User name:', font = ( 'Arial', '18 is'))
label1.grid (Row = 0, column = 0)
label2 = tkinter.Label (Top, text = 'password:', font = ( 'Arial', '18 is'))
label2 .grid (Row =. 1, column = 0)
V = tkinter.StringVar ()
entry1 = tkinter.Entry (Top, = font ( 'Arial' , '18'), textvariable = v, validate = 'focusout', validatecommand = validateText)

entry1.grid(row=0, column=1)
entry1.focus_force()
entry2 = tkinter.Entry(top, font=('宋体', '18'), show = '*')

entry2.grid (Row =. 1, column =. 1)
Button1 = tkinter.Button (Top, text = 'login', font = ( 'Arial', '18 is'), Command = anw_button)
button1.grid (Row = 2, = 0 column, padx = 50, 10 = pady)
Button2 = tkinter.Button (Top, text = 'register', font = ( 'Arial', '18 is'), Command = registered)
button2.grid (Row = 2, =. 1 column, padx = 80, 10 = pady)
Label3 = tkinter.Label (Top, text = 'information presentation area', font = ( 'new Chinese Wei', '16'), relief = 'ridge', width = 30)
label3.grid (Row =. 3, column = 0, padx = 10, 10 = pady, ColumnSpan = 2, Sticky = 'S')
top.mainloop ()

grid: grid layout
row = 3, the line number, starting from 0, the default is 0
column = 0, column number, starting from 0, the default is 0
filled padx = 10, x direction
pady = 10, y direction filling
columnspan = 2 ,
Sticky = 'S' direction s, n, e, w

Guess you like

Origin blog.51cto.com/10956218/2445335
Recommended