2013-05-28 39 views
1

我一直在試圖創建一個打開的窗口,在打開Tkinter的筆記本之前詢問您的用戶名和密碼,我有兩個,但我不知道如何把它們放在一起。換句話說,我想要的是打開一個筆記本,一旦請求的用戶名和密碼是正確的。在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() 
+0

當你說「打開筆記本」時,你究竟是什麼意思?你的意思是你想創建一個單獨的窗口? –

回答

2

你有幾件事情在你的代碼會錯:

  • 你打電話mainloop兩次;你應該只能稱它一次。
  • 你不應該packgrid小部件在筆記本內。你是一個小部件,然後使用notebook.add;省略pack
  • 如果密碼良好,則您在根窗口上撥打destroy。這會導致您的應用程序退出。不要撥打destroy

通常,這樣做的方式是筆記本電腦是根窗口的子項,並且用戶名/密碼對話框是Toplevel的實例。您可以隱藏根窗口並彈出對話框,然後如果用戶登錄,則可以銷燬對話框並取消隱藏主窗口。