1

我一直在尝试创建一个打开的窗口,在 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()
4

1 回答 1

2

您的代码中有几处出错:

  • 你打mainloop了两次电话;你应该只调用一次。
  • 你不应该packgrid笔记本内的小部件。您正在pack创建一个小部件,然后使用notebook.add; 省略pack.
  • 如果密码正确,您正在调用destroy根窗口。这会导致您的应用程序退出。不要打电话destroy

通常这样做的方式是笔记本是根窗口的子窗口,用户名/密码对话框是Toplevel. 您可以隐藏根窗口并弹出对话框,然后如果用户登录,您可以销毁对话框并取消隐藏主窗口。

于 2013-05-29T16:07:35.097 回答