Python可视化编程(Tkinter)-登陆界面的制作(源码)

login.py(登陆界面)

# -*- coding: utf-8 -*-
"""
Created on Mon Dec  9 16:52:17 2019
"""
 
from tkinter import *
from tkinter.messagebox import *
from sign_up import *
from home_windows import *
def login_window():
    import tkinter
    import tkinter as tk
    import os
    import os.path
    import sqlite3
    
    root=tkinter.Tk()
    #root=tkinter.Toplevel()
    img = os.getcwd() + '\\images'
    dat = os.getcwd() + '\\data'
    root.title('English-Study-System-登陆界面') 
    root.geometry('600x400+600+300') 
    root.resizable(False,False)
    photo=tkinter.PhotoImage(file=img + "\\login.png")
    label=tkinter.Label(root,image=photo)  #图片
    label.pack()
    def yanzheng():
        username = entry1.get()
        password = entry2.get()
        #print(username,password)
        cn=sqlite3.connect('English_study_system.db')
        cur=cn.cursor()
        n = cur.execute('select * from user')
        a = n.fetchall()
        c = 0
        d = 0
        for i in a:
            if i[0] == username:
                c = 1
                if i[1] == password:
                    d = 1
                break
        if username == "" or password == "":
            tkinter.messagebox.showerror('Error', '您好像忘了输入什么东西!')
        elif c == 0:
            tkinter.messagebox.showerror('Error', '该用户名尚未注册!')
        elif c == 1 and d == 0:
            tkinter.messagebox.showerror('Error', '您输入的账号和密码不匹配!')
        else:
            tkinter.messagebox.showinfo('Welcome', '您已登陆成功!')
            user = str(username)
            with open(dat +'\\username.txt', 'w',encoding='utf-8') as file:
                file.write(user)
            root.destroy()
            home_windows()
    button1 = tkinter.Button(root,text='登陆',bg = 'lightcyan',fg = 'green',activebackground = 'orange', command=yanzheng)
    button1.place(x=120, y=280, width=160, height=35)
 
    button2 = tkinter.Button(root,text='注册',bg = 'wheat',fg = 'red',activebackground = 'red', command=usr_sign_up)
    button2.place(x=320, y=280, width=160, height=35)
 
    lb1 = tkinter.Label(root,text="用户:",fg = 'indigo', bg = 'white')
    lb1.place(x = 180, y = 135, width = 50, height = 35)
 
    with open('data/username.txt', 'r', encoding='utf-8') as file:
            a = file.read()
    name = tk.StringVar()
    entry1 = tkinter.Entry(root,width=200, bd = 0, bg = 'white', textvariable=name)
    entry1.insert(0,str(a))
    entry1.place(x = 230, y=135,width=200,height=35)
 
 
    lb2 = tkinter.Label(root,text="密码:",fg = 'indigo', bg = 'white')
    lb2.place(x = 180, y = 180, width = 50, height = 35)
    
    entry2 = tkinter.Entry(root,width=200, bd = 0, bg = 'white', show='*')
    
            
    entry2.place(x = 230,y = 180,width=200,height = 35)
 
 
    cn=sqlite3.connect('English_study_system.db')
    cur=cn.cursor()
    #showinfo("logo","数据库连接成功,单击确定继续操作")
    try:
        cur.execute('create table user(username char(16)PRIMARY key, password char (16))')
        #showinfo("logo","数据表创建成功")
    except:
            #showinfo("logo","现在可以开始报名")
            p = 1
 
    cn.close()
    root.mainloop()

 

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这是一个VB6的IDE插件(Addin),使用VB6的IDE直接设计Python界面Python和VB都是能让人快乐的编程语言,我使用了Python之后,很多自己使用的工具都使用Python开发或改写了,因为最终实现的Python代码实在太短了(相比VB),有时候Python一行代码就可以实现VB一个函数的功能。 Python就是这种让人越用越开心的语言。 不过说实在,使用Python开发GUI界面还是麻烦了一些了,自带的标准库Tkinter使用起来非常简单,不过对于习惯了VB拖放控件完成界面设计的偶来说,还是不够人性。TK也有一个工具叫GUI Builder,不过它使用Layout布局,不够直观,而且用起来很不爽。。 至于PyQt/wxPython等GUI库,尽管有可设计工具,但总感觉做一般的轻量级应用是杀鸡用牛刀, 而且不够环保,不够低碳,要带一个很大的库,需要目标机器上夜同样安装了PyQt/wxPython,做不了绿色软件。 所以最终的结果是我更喜欢Tkinter,用起来很简单,绿色环保,真正的跨平台,一个py文件到处运行(担心泄密就编译成pyc)。 很多人都认为TK的界面不够美观,不过我经过多次实验后发现导入Python自带的标准TTK主题库,界面非常Native,不输PyQt/wxPython。 此Addin默认启用TTK支持,也可选择关闭。 总而言之,轻量级GUI,TK+TTK足够。 使用此Addin,你可以不用写一句代码就可以生成一个完整可运行的Python的GUI界面,支持2.X和3.X。 安装方法:将压缩包解压到你希望的目录,然后执行Setup.exe完成注册插件过程,打开VB6就可以用了。 在VB窗体上设计完成界面后(你可以大胆的设置各控件的属性,Addin尽量将其翻译为tkinter的控件属性),点工具栏上的VisualTkinter(图标为一片羽毛),再点'生成代码'按钮,即可生成可运行的Python代码,可以拷贝至剪贴板或保存至文件。 一般情况下你可以不用再改变tkinter的控件属性,但是如果你熟悉tkinter,需要更多的控制,可以一一核对各属性,并且修改,再生成代码。 当然除了用来设计界面外,此ADDIN内置的各控件属性列表可以做为编程参考,比较完整,除了极少数我认为大多数人都不用的属性外,属性定义基本上是我从官方的tkinter文档直接翻译的。 如果还没有VB6,网上找一个VB6精简版即可,不到20M,小巧玲珑。 代码已经在Github上托管,更新的版本可以在这上面找到,需求也可以在上面提: https://github.com/cdhigh/Visual-Tkinter-for-Python

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值