Python + tkinter simulate "remember me" to automatically log on to the instance code, pythontkinter

Source: Internet
Author: User

Python + tkinter simulate "remember me" to automatically log on to the instance code, pythontkinter

The Code shared in this article is mainly used to simulate the "remember me" Automatic Logon function through Python + tkinter. The details are as follows.

Basic Idea: If a logon succeeds, a temporary file is created to record the relevant information. Each time the program is started, the system tries to automatically obtain and write the information that was successfully logged on last time. This article mainly demonstrates the idea of rewriting based on the actual needs of the system, such as reading the database, verifying that the user name and password are correct, and locally encrypting and storing the user name and password.

Import tkinterimport tkinter. messageboximport osimport OS. path # obtain the temporary Windows folder path = OS. getenv ('temp ') filename = OS. path. join (path, 'info.txt ') # create the application window root = tkinter. tk () # create a tag component labelName = tkinter in the window. label (root, text = 'user Name: ', justify = tkinter. RIGHT, width = 80) labelName. place (x = 10, y = 5, width = 80, height = 20) # create a string variable and a text box component, and set the associated variable varName = tkinter. stringVar (root, value = '') entryName = tkinter. Entry (root, width = 80, textvariable = varName) entryName. place (x = 100, y = 5, width = 80, height = 20) labelPwd = tkinter. label (root, text = 'user Pwd: ', justify = tkinter. RIGHT, width = 80) labelPwd. place (x = 10, y = 30, width = 80, height = 20) # create a Password text box varPwd = tkinter. stringVar (root, value = '') entryPwd = tkinter. entry (root, show = '*', width = 80, textvariable = varPwd) entryPwd. place (x = 100, y = 30, width = 80, height = 20) # try to automatically enter the user name and password try: With open (filename) as fp: n, p = fp. read (). strip (). split (',') varName. set (n) varPwd. set (p) failed T: pass # logon button event handler def login (): # Get username and password name = entryName. get () pwd = entryPwd. get () if name = 'admin' and pwd = '000000': tkinter. messagebox. showinfo (title = 'congratulation ', message =' login successful! ') # Write the login success information to the temporary file with open (filename, 'w') as fp: fp. write (','. join (name, pwd) else: tkinter. messagebox. showerror ('Warning ', message = 'username or password error') # create a button component and set the button event processing function buttonOk = tkinter. button (root, text = 'login', command = Login) buttonOk. place (x = 30, y = 70, width = 50, height = 20) # cancel the event handler def cancel (): # Clear the user name and password varName. set ('') varPwd. set ('') buttonCancel = tkinter. button (root, text = 'cancel', command = Cancel) buttonCancel. place (x = 90, y = 70, width = 50, height = 20) # Start the message loop root. mainloop ()

Demo result:

Summary

The above is all about the Python + tkinter simulated "remember me" Automatic Logon instance code in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.