Tkinter简单使用用户登陆-前端

感情迁移 提交于 2020-01-22 07:54:45

1.Tkinter官方文档:
https://docs.python.org/3/library/tkinter.html?highlight=tkinter#module-tkinter
2.新建HelloTK.py,把网站的A Simple Hello World Program下的代码粘过来,先运行下;

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

3.页面显示:
在这里插入图片描述
4.比猫画虎:做一个登陆连接,静态给定用户名和密码:

import tkinter as tk

import requests


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        # self.hi_there = tk.Button(self)
        # self.hi_there["text"] = "Hello World\n(click me)"
        # self.hi_there["command"] = self.say_hi
        # self.hi_there.pack(side="top")
        
        self.login = tk.Button(self)
        self.login['text'] = "登陆"
        self.login['command'] = self.login  #login是函数
        self.login.pack(side='top')
        #推出按钮
        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    # def say_hi(self):
    #     print("hi there, everyone!")
    
    def login(self):
        url = "http://10.0.113.211:8000/login_api/"         #登陆页面路由,这是自己写的项目的登陆路由,你可以做更改;

        data = {
            'username':'zy',
            'password':'110',
        }
        response = requests.post(url,json=data)

if __name__ == '__main__':

    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()

运行起来;如果你的登陆页面写的对,终端会给反馈;

5.动态设置用户名和密码的输入:
A.官网,把案例代码粘下来
在这里插入图片描述
B.对照案例写用户名和密码:

import tkinter as tk
import requests

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        #用户名;side="top"位置
        self.username = tk.Entry(side="top")
        self.username.pack(side='top')
        self.username_contents = tk.StringVar()
        self.username_contents.set('请输入用户名')
        self.username['textvariable'] = self.username_contents

        #密码
        self.password = tk.Entry(side="top")
        self.password.pack(side='top')
        self.password_contents = tk.StringVar()
        self.password_contents.set('请输入用户名')
        self.password['textvariable'] = self.password_contents

        #输入框
        # self.entrythingy = Entry()
        # self.entrythingy.pack()
        #
        # # here is the application variable
        # self.contents = StringVar()
        # # set it to some value
        # self.contents.set("this is a variable")
        # # tell the entry widget to watch this variable
        # self.entrythingy["textvariable"] = self.contents
        #
        # # and here we get a callback when the user hits return.
        # # we will have the program print out the value of the
        # # application variable when the user hits return
        # self.entrythingy.bind('<Key-Return>',
        #                       self.print_contents)


        # self.hi_there = tk.Button(self)
        # self.hi_there["text"] = "Hello World\n(click me)"
        # self.hi_there["command"] = self.say_hi
        # self.hi_there.pack(side="top")
        # 登陆按钮
        self.login = tk.Button(self)
        self.login['text'] = "登陆"
        self.login['command'] = self.login  #login是函数
        self.login.pack(side='top')


        #退出按钮
        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    # def print_contents(self, event):
    #     print("hi. contents of entry is now ---->",
    #           self.contents.get())

    # def say_hi(self):
    #     print("hi there, everyone!")

    def login(self):
        url = "http://10.0.113.211:8000/login_api/"         #登陆页面路由,这是自己写的项目的登陆路由,你可以做更改;

        username = self.username_contents.get()
        password = self.password_contents.get()
        print(username,password)

        # data = {
        #     'username':'zy',
        #     'password':'110',
        # }
        data = {
            'username':username,
            'password':password,
        }

        response = requests.post(url,json=data)

if __name__ == '__main__':

    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()

在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!