tkinter python中出现一个意外的窗口

时间:2017-06-28 20:16:59

标签: python python-3.x tkinter

我为其他代码创建了一个名为clinic1.py的单独文件,并将其导入主页面。一切正常,但是当我点击添加新项目页面上的保存按钮时,会出现另一个窗口。

当我将所有代码放在主页上时,小窗口不会出现。 我找不到什么导致另一个窗口显示在另一个文件中。

这是我的主页:

<nav>
    <div class="panel center">
        <ul>    
            <li><a href="Home.html">Home</a></li>                <!--All the pages on the website-->
            <li><a href="SerieA_Tables.html">Table</a></li>
            <li><a href="News.html">News</a></li>
            <li><a href="Teams.html">Teams</a>
                <ul>
                    <li><a href = "#">Juventus</a></li>
                    <li><a href = "#">AC Milan</a></li>
                    <li><a href = "#">Torino</a></li>
                </ul>
            </li>
            <li><a href="About.html">About</a></li>
        </ul>
     </div>
</nav>

这是我在主页面导入的第二个文件,我保存为clin1.py

from tkinter import * 
from tkinter import ttk 
import tkinter.messagebox

large_font = ('Verdana',12)       
storedusername =['foo'] storedpass=['123'] storedretype=[]
list_of_users=storedusername 
list_of_passwords=storedpass


def all_clinic_frames(event):
    combo_clinic=combo.get()
    if combo_clinic == 'Clinic 1':
        enter()

root = Tk() 
root.geometry('800x600') 
root.title('CSSD') 
topFrame=Frame(root,width=800,height=100,padx=310) 
area=Label(topFrame,text='CSSD') 
area.config(font=("Courier", 50)) 
frame=Frame(root,highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, bd= 0) 
frame.place(relx=.5, rely=.5, anchor="center")

username = Label(frame, text='User Name') username.config(font='Arial',width=15) password = Label(frame, text='Password') password.config(font='Arial',width=15) enteruser = Entry(frame, textvariable=StringVar(),font=large_font) enterpass = Entry(frame, show='*', textvariable=StringVar(),font=large_font)
combo_choice=StringVar() 
combo=ttk.Combobox(frame,textvariable=combo_choice) 
combo['values']=('Clinic 1') 
combo.state(['readonly']) 
combo.grid(row=0,sticky=NW) 
combo.set('Choose Area...') 
combo.bind('<<ComboboxSelected>>',all_clinic_frames)

topFrame.grid(row=0,sticky=N) topFrame.grid_propagate(False) area.grid(row=0,column=1,sticky=N) username.grid(row=1, sticky=E) enteruser.grid(row=1, column=1) password.grid(row=2, sticky=E) enterpass.grid(row=2, column=1)

 def valid():
    usernameRight=enteruser.get()
    passwordRight=enterpass.get()
    while True:
         try:
            if (usernameRight==list_of_users[0]) and (passwordRight==list_of_passwords[0]):
            import clinic1
            clinic1.main_page()
            quit()
            break
         except IndexError:
            invalid = Label(frame, text='User name or Password is incorrect!', fg='red')
            invalid.grid(row=3, columnspan=2)
            break

def enter():
    register = Button(frame, text='Sign In',relief=RAISED,fg='white',bg='red',command=valid)
    register.grid(row=3,column=1,ipadx=15,sticky=E)

def quit():
    root.destroy()

2 个答案:

答案 0 :(得分:2)

这两个文件都有这行代码:

b()

每次执行此操作时,您都会获得另一个根窗口。 tkinter应用程序需要一次只运行一个root = Tk() 个实例。

您需要从clin1.py中删除最后两行。您还需要将Tk的引用传递给来自clin1.py的任何需要它的方法。

答案 1 :(得分:0)

第一档。

from tkinter import * 
from tkinter import ttk 
import tkinter.messagebox

large_font = ('Verdana',12)       
storedusername =['foo']
storedpass=['123']
storedretype=[]
list_of_users=storedusername 
list_of_passwords=storedpass


def all_clinic_frames(event):
    combo_clinic=combo.get()
    if combo_clinic == 'Clinic 1':
        enter()

root = Tk() 
root.geometry('800x600') 
root.title('CSSD') 
topFrame=Frame(root,width=800,height=100,padx=310) 
area=Label(topFrame,text='CSSD') 
area.config(font=("Courier", 50)) 
frame=Frame(root,highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, bd= 0) 
frame.place(relx=.5, rely=.5, anchor="center")

myvar=StringVar()
username = Label(frame, text='User Name')
username.config(font='Arial',width=15)
password = Label(frame, text='Password')
password.config(font='Arial',width=15)
enteruser = Entry(frame, textvariable=myvar, font=large_font)
pass1=StringVar()
enterpass = Entry(frame, show='*', textvariable=pass1, font=large_font)
combo_choice=StringVar() 
combo=ttk.Combobox(frame,textvariable=combo_choice) 
combo['values']=[('Clinic 1')]
combo.state(['readonly']) 
combo.grid(row=0,sticky=NW) 
combo.set('Choose Area...') 
combo.bind('<<ComboboxSelected>>',all_clinic_frames)

topFrame.grid(row=0,sticky=N)
topFrame.grid_propagate(False)
area.grid(row=0,column=1,sticky=N)
username.grid(row=1, sticky=E)
enteruser.grid(row=1, column=1)
password.grid(row=2, sticky=E)
enterpass.grid(row=2, column=1)

def valid():
    usernameRight=enteruser.get()
    passwordRight=enterpass.get()
    while True:
         try:
            if (usernameRight==list_of_users[0]) and (passwordRight==list_of_passwords[0]):
               import clinic1
               clinic1.main_page(root)
#               quit()
               break
         except IndexError:
            invalid = Label(frame, text='User name or Password is incorrect!', fg='red')
            invalid.grid(row=3, columnspan=2)
            break

def enter():
    register = Button(frame, text='Sign In',relief=RAISED,fg='white',bg='red',command=valid)
    register.grid(row=3,column=1,ipadx=15,sticky=E)

def quit():
    root.destroy()

root.mainloop()

clinic1.py

from tkinter import*
import tkinter.messagebox

newInstList=[]

def addItem(root, event=None):
    global back_add,quantityentry,itemEntry,itemEntry1,quantityentry1
    if event is None:
        event = Event()
    itemFrameTop=Frame(root, width=800, height=100, bg='pink')
    itemFrameTop.grid_propagate(False)
    itemFrameTop.grid(row=0)
    area1_item = Label(itemFrameTop, text='CSSD', pady=5,padx=230)
    area1_item.config(font=("Courier", 30))
    area1_item.grid_propagate(False)
    area1_item.grid(row=0,column=1,sticky=NE)
    clinic_1 = Label(itemFrameTop, text='Clinic 1', bg='red', fg='white', bd=5)
    clinic_1.config(font=("Courier", 15))
    clinic_1.grid_propagate(False)
    clinic_1.grid(row=1, sticky=W,padx=10)
    itemFrameMid=Frame(root,width=700,height=600,bg='blue')
    itemFrameMid.grid_propagate(False)
    itemFrameMid.grid(row=1)
    itemname=Label(itemFrameMid,text='Item name:')
    itemname.config(font=('Arial,15'))
    itemname.grid_propagate(False)
    itemname.grid(row=1,sticky=E)
    quantity=Label(itemFrameMid,text='Qty:')
    quantity.config(font=('Arial,15'))
    quantity.grid_propagate(False)
    quantity.grid(row=1,column=3, sticky=E,padx=10)
    itemEntry=Entry(itemFrameMid)
    itemEntry.config(font=('Arial,15'))
    itemEntry.grid(row=1,column=1,sticky=EW,padx=30,pady=10)
    itemEntry1 = Entry(itemFrameMid)
    itemEntry1.config(font=('Arial,15'))
    itemEntry1.grid(row=2, column=1)

    quantityentry=Entry(itemFrameMid,width=5)
    quantityentry.config(font=('Arial',15))
    quantityentry.grid(row=1, column=4)
    quantityentry1 = Entry(itemFrameMid, width=5)
    quantityentry1.config(font=('Arial', 15))
    quantityentry1.grid(row=2, column=4,padx=10)

    """When I click save button another small window appears"""
    okbutton = Button(itemFrameMid, text='Save', command=lambda: saveCheck(root))
    okbutton.config(font=('Arial', 12))
    okbutton.grid(row=3, column=4, padx=15)

    back_add = Label(itemFrameTop, text='Back')
    back_add.config(font=('Courier,15'))
    back_add.grid(row=0, sticky=W, padx=30)
    back_add.bind('<Button-1>', main_page)
    back_add.bind('<Enter>', red_text_back1)
    back_add.bind('<Leave>', black_text_back1)

def saveCheck(root):
    saveQuestion=tkinter.messagebox.askquestion('CSSD', 'Are you sure you want to save?')
    if saveQuestion == 'yes':
        newInstList.append(itemEntry.get())
        newInstList.append(quantityentry.get())
        newInstList.append(itemEntry1.get())
        newInstList.append(quantityentry1.get())
        print(newInstList)
        main_page(root)
    elif saveQuestion == 'no':
        pass

def red_text_back1(event=None):
    back_add.config(fg='red')

def black_text_back1(event=None):
    back_add.config(fg='black')

def red_text_add(event=None):
    addnew.config(fg='red')

def black_text_add(event=None):
    addnew.config(fg='black')

def main_page(root):

    global addnew,usedInst,logOut
    frame1 = Frame(root, width=800, height=100,bg='pink')
    frame1.grid(row=0, column=0, sticky="nsew")
    frame1.grid_propagate(False)
    midframe1=Frame(root,width=800,height=600)
    midframe1.grid_propagate(False)
    midframe1.grid(row=1)

    area1 = Label(frame1, text='CSSD',pady=5,padx=350)
    area1.config(font=("Courier", 30))
    area1.grid(row=0)
    clinic1=Label(frame1,text='Clinic 1',bg='red',fg='white',bd=5)
    clinic1.config(font=("Courier", 15))
    clinic1.grid_propagate(False)
    clinic1.grid(row=1,sticky=W,padx=10)
    addnew=Button(midframe1,text='+ Add new item', font=('Arial', 15), command=lambda: addItem(root))
    addnew.grid(row=2,column=1,sticky=E,ipadx=50)
#    addnew.bind('<Button-1>', lambda r=root: addItem(r))
    addnew.bind('<Enter>', red_text_add)
    addnew.bind('<Leave>', black_text_add)