前言:
什么是tk,python里的tk是什么,能干什么?
Tkinter模塊("Tk 接口")是Python的標(biāo)準(zhǔn)Tk GUI工具包的接口.
Tk和Tkinter可以在大多數(shù)的Unix平臺下使用,同樣可以應(yīng)用在Windows和Macintosh系統(tǒng)里.Tk8.0的后續(xù)版本可以通過ttk實(shí)現(xiàn)本地窗口風(fēng)格,并良好地運(yùn)行在絕大多數(shù)平臺中。
簡單就是一個簡單的界面制作,學(xué)習(xí)這里可以快速學(xué)期其他的界面比如說Qt5
這里我們來使用tkinter庫來制作一個登錄界面
首先我們先制作一個窗口
這里就是一個窗體的代碼,運(yùn)行查看效果
?可以可以,下面我們就來制作一下他的按鈕的功能,先是登錄按鈕的功能
這里我是設(shè)置可以登錄三次,三次失敗后就直接把窗體給退出,就是程序結(jié)束。
前提我們要有一個賬號和密碼存放的文件?
這里我們就定義一個read1函數(shù)來獲取賬號密碼等數(shù)據(jù)?
?這里我們就把登錄按鈕制作好了,下面我們來制作注冊按鈕的功能
這里我使用的是把窗口變成透明色,這樣我們的用戶就看不到登錄窗口了
而后我們再寫一個函數(shù)來實(shí)現(xiàn)注冊功能
?而后我們運(yùn)行代碼
?最后我們實(shí)現(xiàn)一下返回功能就好了
這樣我就使用python完成了登錄界面的制作
下面是我的代碼
# 先導(dǎo)入tkinter, sys, re庫
from tkinter import *
from tkinter import messagebox as msgbox
import sys
import re
# 這里定義一個Tking類
class Tking(object):
# 初始化
def __init__(self):
self.win = Tk()
self.count = 3
# 定義函數(shù)來獲取數(shù)據(jù)內(nèi)容
def read1(self):
with open('zhanghao.txt', 'r')as f:
file = f.read()
return file
# 定義函數(shù)來實(shí)現(xiàn)“登錄”按鈕的功能
def js(self):
# 調(diào)用函數(shù)獲取賬號信息
data = self.read1()
# 獲取用戶輸入的信息
user = self.username.get()
passwd = self.passwd.get()
# 使用正則得到我們想要的數(shù)據(jù)
user1 = re.findall(r'賬號:(.*\d?)', data)
passwd1 = re.findall(r'密碼:(.*\w?)', data)
# 這里判斷輸入錯誤的次數(shù),等于次就結(jié)束程序
if self.count == 0:
msgbox.showinfo('警告!', '非法入侵!\n啟動自動銷毀模式!!!')
self.win.quit()
sys.exit(0)
# 這里對用戶的數(shù)據(jù)和賬號數(shù)據(jù)進(jìn)行對比
for i in range(len(user1)):
if user == user1[i] and passwd == passwd1[i]:
msgbox.showinfo('登錄', '登錄成功!')
break
else:
msgbox.showinfo('失敗!', '你還可以登錄%s次' % self.count)
self.count -= 1
# 定義返回函數(shù),使用“返回”按鈕的功能
def fanhui(self):
# 使登錄窗口恢復(fù)顏色,并銷毀掉注冊窗口
self.win.attributes('-alpha', 1)
self.win1.destroy()
# 定義函數(shù),實(shí)現(xiàn)注冊功能
def register(self):
# 獲取用戶輸入的數(shù)據(jù)
username = self.username1.get()
passwd1 = self.passwd1.get()
passwd2 = self.passwd2.get()
if passwd1 == '' or passwd2 == '':
return msgbox.showerror('失?。?, '不能填空?。?!')
# 判斷用戶輸入的數(shù)據(jù)
if passwd1 != passwd2:
msgbox.showerror('失敗!', '注冊失敗,兩次密碼不一致!')
else:
msgbox.showinfo('成功', '恭喜你注冊成功!')
with open('zhanghao.txt', 'a')as file:
file.write('賬號:')
file.write(username + '\n')
file.write('密碼:')
file.write(passwd2 + '\n')
# 定義函數(shù)實(shí)現(xiàn)“注冊”按鈕的功能
def js1(self):
# 使用登錄界面變成透明色,讓用戶看不到
self.win.attributes('-alpha', -1)
# 定義窗口2,來實(shí)現(xiàn)注冊窗口
self.win1 = Tk()
self.win1.geometry("300x200")
self.win1.title("注冊")
# 定義輸入框
self.username1 = Entry(self.win1)
self.passwd1 = Entry(self.win1, show="*")
self.passwd2 = Entry(self.win1, show="*")
self.username1.place(x=110, y=25)
self.passwd1.place(x=110, y=75)
self.passwd2.place(x=110, y=125)
# 定義標(biāo)簽
name4 = Label(self.win1, text="用戶名:", font=('宋體', 15), width=10)
name4.place(x=1, y=20)
name5 = Label(self.win1, text="密碼:", font=('宋體', 15), width=10)
name5.place(x=1, y=70)
name6 = Label(self.win1, text="確認(rèn)密碼:", font=('宋體', 15), width=10)
name6.place(x=1, y=120)
# 定義按鈕
button = Button(self.win1, text="返回", font=('宋體', 10), width=5, command=self.fanhui)
button.place(x=250, y=170)
button1 = Button(self.win1, text="注冊", font=('宋體', 15), width=10, command=self.register)
button1.place(x=100, y=160)
# 窗口運(yùn)行
self.win1.mainloop()
# 定義函數(shù),創(chuàng)建窗口
def windows(self):
self.win.geometry("300x200")
self.win.title("登錄")
# 設(shè)置按鈕
self.username = Entry(self.win)
self.passwd = Entry(self.win, show="*")
self.username.place(x=110, y=25)
self.passwd.place(x=110, y=75)
# 設(shè)置標(biāo)簽,實(shí)現(xiàn)在窗口上打印“用戶名:”等
name1 = Label(self.win, text="用戶名:", font=('宋體', 15), width=10)
name1.place(x=1, y=20)
name2 = Label(self.win, text="密碼:", font=('宋體', 15), width=10)
name2.place(x=1, y=70)
# 設(shè)置按鈕
button = Button(self.win, text="登錄", font=('宋體', 15), width=10, command=self.js)
button.place(x=20, y=110)
button1 = Button(self.win, text="注冊", font=('宋體', 15), width=10, command=self.js1)
button1.place(x=140, y=110)
# 窗口運(yùn)行
self.win.mainloop()
# 調(diào)用類
if __name__ == '__main__':
tk = Tking()
tk.windows()
?運(yùn)行看看效果
?
代碼試用成功!文章來源:http://www.zghlxwxcb.cn/news/detail-765753.html
感謝大家的閱讀!文章來源地址http://www.zghlxwxcb.cn/news/detail-765753.html
到了這里,關(guān)于Python(Tk)登錄界面的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!