Login 在Tkinter中访问笔记本电脑的窗口

Login 在Tkinter中访问笔记本电脑的窗口,login,tkinter,Login,Tkinter,我一直在尝试创建一个打开的窗口,在Tkinter中打开笔记本之前,它会要求您输入用户名和密码,我有这两个,但我不知道如何将它们放在一起。换句话说,我想要的是在用户名和密码正确后打开一个笔记本 提前非常感谢 到目前为止,我所做的工作如下: import Tkinter from Tkinter import * import ttk from ttk import * #Combobox Definition import tkMessageBox #for Welcome Message i

我一直在尝试创建一个打开的窗口,在Tkinter中打开笔记本之前,它会要求您输入用户名和密码,我有这两个,但我不知道如何将它们放在一起。换句话说,我想要的是在用户名和密码正确后打开一个笔记本

提前非常感谢

到目前为止,我所做的工作如下:

import Tkinter
from Tkinter import *
import ttk
from ttk import *   #Combobox Definition
import tkMessageBox #for Welcome Message
import Tkinter as tk # For Main Frame Definition
from Tkinter import Tk, Text, BOTH, W, N, E, S
from ttk import Frame, Button, Label, Style

root = Tk()
root.title("Model A")
root.minsize(400, 220)
root.maxsize(410, 240)


# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill='both', expand='yes')
notebook.pressed_index = None


# create a child frame for each page
frameOne = Tkinter.Frame(notebook, bg='white',width=560, height=100)
frameOne.pack(fill='both', expand=True)

# create the pages
notebook.add(frameOne, text='Simple calculation')


#Login Starts    

failure_max = 8
passwords = [('name','password')]

def make_entry(parent, caption, width=None, **options):
    tk.Label(parent, text=caption).pack(side=tk.TOP)
    entry = tk.Entry(parent, **options)
    if width:
        entry.config(width=width)
    entry.pack(side=tk.TOP, padx=10, fill=tk.BOTH)
    return entry

def enter(event):
    check_password()

def check_password(failures=[]):
    if (user.get(), password.get()) in passwords:
        root.destroy()
        return
    failures.append(1)
    if sum(failures) >= failure_max:
       root.destroy()
       raise SystemExit('Unauthorized login attempt')
    else:
        root.title('Try again. Attempt %i/%i' % (sum(failures)+1, failure_max))


parent = Tkinter.Frame(notebook, padx=10, pady=18, bg='white')
parent.pack(fill=tk.BOTH, expand=True)

user = make_entry(parent, "User name:", 16, show='')
password = make_entry(parent, "Password:", 16, show="*")

b = tk.Button(parent,borderwidth=4,text="Login",width=10,pady=8,command=check_password)
b.pack(side=Tkinter.BOTTOM)
password.bind('<Return>', enter)

#Close Application Button
def quit(root):
    root.destroy()

tk.Button(root, text="Close Application", command=lambda root=root:quit(root)).pack()

#Calculation Starts

def defocus(event):
        event.widget.master.focus_set()

def multiply(*args):
    try:
        product.set(round(float(Num_One.get())*float(Num_Two.get())))
    except ValueError:
        pass

Num_One = StringVar()
Num_Two = StringVar()
product = DoubleVar()


ttk.Label(frameOne, text="Select First Number:").grid(column =3, row = 0)
NumOne_Select = Combobox(frameOne, values=("1", "2", "3","4", "5"),textvariable=Num_One)
NumOne_Select.grid(column=4, row=0, columnspan="5", sticky="nswe")
Num_One.trace("w", multiply)

ttk.Label(frameOne, text="Select Second Number:").grid(column =3, row = 6 )
NumTwo_Select = Combobox(frameOne, values=("1", "2", "3","4", "5"),textvariable=Num_Two)
NumTwo_Select.grid(column=4, row=6, columnspan="5", sticky="nswe")
Num_Two.trace("w", multiply)

ttk.Label(frameOne, text = "Product:").grid(column = 3, row = 8)
ttk.Label(frameOne, textvariable=product).grid(column = 4, row = 8)

user.focus_set()
parent.mainloop() 
root.mainloop()
导入Tkinter
从Tkinter进口*
导入ttk
从ttk导入*#组合框定义
导入tkMessageBox#以获取欢迎信息
将Tkinter作为tk#导入主框架定义
从Tkinter导入Tk、Text、BOTH、W、N、E、S
从ttk导入框架、按钮、标签、样式
root=Tk()
根标题(“模型A”)
root.minsize(400220)
root.maxsize(410240)
#笔记本的开始(多个选项卡)
notebook=ttk.notebook(根目录)
notebook.pack(fill='both',expand='yes')
notebook.u索引=无
#为每个页面创建子框架
frameOne=Tkinter.Frame(笔记本电脑,背景为白色,宽度为560,高度为100)
frameOne.pack(fill='both',expand=True)
#创建页面
notebook.add(frameOne,text='Simple calculation')
#登录开始
故障_max=8
密码=[('name','password')]
def make_条目(父项,标题,宽度=无,**选项):
标签(父项,文本=标题).pack(side=tk.TOP)
entry=tk.entry(父项,**选项)
如果宽度:
entry.config(宽度=宽度)
入口包装(侧面=顶部,padx=10,填充=顶部和顶部)
返回条目
def enter(事件):
检查密码()
def检查_密码(失败=[]):
如果密码中有(user.get(),password.get()):
root.destroy()
返回
失败。追加(1)
如果总和(失败)>=失败\u最大值:
root.destroy()
引发SystemExit('未经授权的登录尝试')
其他:
root.title('再试一次。尝试%i/%i'(总和(失败)+1,失败\u最大值))
parent=Tkinter.Frame(笔记本,padx=10,pady=18,bg='white')
parent.pack(fill=tk.BOTH,expand=True)
user=make_条目(父项,“用户名:”,16,show='')
密码=输入(父项,“密码:”,16,show=“*”)
b=tk.按钮(父级,borderwidth=4,text=“Login”,width=10,pady=8,command=check\u密码)
b、 包装(侧面=底部)
密码绑定(“”,输入)
#关闭应用程序按钮
def退出(根):
root.destroy()
按钮(root,text=“Close Application”,command=lambda root=root:quit(root)).pack()
#开始计算
def离焦(事件):
event.widget.master.focus\u set()
def乘法(*args):
尝试:
product.set(round(float(Num_-One.get())*float(Num_-Two.get()))
除值错误外:
通过
Num_One=StringVar()
Num_Two=StringVar()
product=DoubleVar()
ttk.Label(frameOne,text=“选择第一个数字:”).grid(列=3,行=0)
NumOne_Select=组合框(frameOne,值=(“1”、“2”、“3”、“4”、“5”),textvariable=Num_One)
NumOne_Select.grid(列=4,行=0,列span=“5”,sticky=“nswe”)
数字1.跟踪(“w”,乘法)
ttk.Label(frameOne,text=“选择第二个数字:”).grid(列=3,行=6)
NumTwo_Select=组合框(frameOne,值=(“1”、“2”、“3”、“4”、“5”),textvariable=Num_Two)
NumTwo_Select.grid(列=4,行=6,列span=“5”,sticky=“nswe”)
数字2.跟踪(“w”,乘法)
ttk.Label(frameOne,text=“Product:”).grid(列=3,行=8)
标签(frameOne,textvariable=product).grid(列=4,行=8)
user.focus_set()
parent.mainloop()
root.mainloop()

您的代码中有几处出错:

  • 您正在调用
    mainloop
    两次;你应该只打一次电话
  • 你不应该在笔记本中
    打包
    网格
    小部件。您正在
    打包
    一个小部件,然后使用
    笔记本。添加
    ;省略
  • 如果密码正确,您正在根窗口上调用
    destroy
    。这将导致应用程序退出。不要调用
    销毁
    
    
通常,这样做的方式是,笔记本是根窗口的子窗口,用户名/密码对话框是
Toplevel
的一个实例。您可以隐藏根窗口并弹出对话框,然后如果用户登录,您可以销毁该对话框并取消隐藏主窗口

当你说“打开笔记本”,你到底是什么意思?你的意思是要创建一个单独的窗口吗?