2013-05-15 39 views
0

按照由Bryan奥克利,关于我的问题testerday更全面的代码请求(不知道怎么做,而无需另外一个问题)Tkinter的消息框askyesno starnge响应

#!/usr/local/bin/python3 
    # -*- coding: utf-8 -*- 

    import ast 
    from tkinter import * 
    import tkinter.font 
    from tkinter import messagebox 
    from tkinter.scrolledtext import ScrolledText 
    from tkinter import ttk 
    from tkinter.ttk import * 


    acronyms = {} 
    with open('new_dict.py','r', encoding = "utf-8") as dic: 
     acronyms = ast.literal_eval(dic.read()) 
    def find_acronym(): 

     found = False 

     # if search term in database returns acronym and expansion 
     for abbr, text in acronyms.items(): 
      if abbr == search_analyte.get(): 
       expansion.insert(0.0,'{0:>6}: {1: <10}\n'.format(abbr, text)) 
       found = True 
      elif str(search_analyte.get()) in text: 
       expansion.insert(0.0,'{0:>6}: {1: <10}\n'.format(abbr, text)) 
       found = True 

     # if search term not in database  
     if not found: 
      expansion.insert 

      messagebox.askyesno(0.0,'"{0}"{1} \n {2}\n'.format(search_analyte.get(),' is not in the database.','Add,if appropriate')) 
      if messagebox.askyesno() == True: 
       open_second() 
      else: 
       open_first() 
    def open_second(): # opens second tab 
     referwork.select(f2) 
    def open_first(): # opens first tab 
     referwork.select(f1) 

    root =Tk() 
    root.title('find/translate acronyms for referred work') 

    root.configure(bg='darkgreen') 
    #state variables for the app 
    search_analyte = StringVar() # search criterion 
    expanded_text=StringVar() # expanded text matching acronym 
    # set up notebook 
    referwork = ttk.Notebook(root) 
    f1 = ttk.Frame(referwork) 
    f2 = ttk.Frame(referwork) 
    referwork.add(f1, text="Search", padding = 1) 
    referwork.add(f2, text="Add/Delete", padding = 1) 
    #referwork.configure (height = 500, width = 800) 
    referwork.grid(row=0, column=0, sticky=(N,W,S,E)) 
    # contents of notebook pages 
    # SEARCH PAGE 
    acronym_entry =Entry(f1,textvariable=search_analyte) 
    acronym_entry.grid(row=1, column = 1, sticky = W) 
    acronym_entry.focus_set() 
    expansion_lbl = Label(f1, text='RESULT') 
    expansion_lbl.grid(row=2, column =0, sticky = W) 
    expansion= ScrolledText(f1,wrap=WORD) 
    expansion.configure(height = 10, width = 50) 
    expansion.grid(row=2, column = 1, columnspan = 2, sticky = W) 
    translate_button = ttk.Button(f1,text="SEARCH",command=find_acronym) 
    translate_button.grid(row=3, column = 0, sticky =W) 

    root.mainloop() 

expanded_text=StringVar() # expanded text matching acronym 
add_expansion= StringVar() # expanded text to be added. Not required for deletion 
edit_acronym = StringVar  # acronym to be added ,edited or deleted 

# set up notebook 
referwork = ttk.Notebook(root, style =".TNotebook") 
f1 = ttk.Frame(referwork) 
f2 = ttk.Frame(referwork) 
referwork.add(f1, text="Search", padding = 1) 
referwork.add(f2, text="Add/Delete", padding = 1) 

#referwork.configure (height = 500, width = 800) 
referwork.grid(row=0, column=0, sticky=(N,W,S,E)) 

# contents of notebook pages 
# SEARCH PAGE 
acronym_entry =Entry(f1,textvariable=search_analyte, style =".TEntry") 
acronym_entry.grid(row=1, column = 1, sticky = W) 
acronym_entry.focus_set() 

expansion_lbl = Label(f1, text='RESULT',font=root.customFont) 
expansion_lbl.grid(row=2, column =0, sticky = W) 
expansion= ScrolledText(f1,wrap=WORD, font=root.customFont) 
expansion.configure(height = 10, width = 50) 
expansion.grid(row=2, column = 1, columnspan = 2, sticky = W) 



# MANAGEMENT PAGE 
# acronyms_add is used for addition (together with text_add) and deletion 
acronym_edit_label = Label(f2, text='enter acronym to be added, edited or deleted',font=root.customFont) 
acronym_edit_label.grid(row = 0, column = 0, sticky = W) 
acronym_edit =Entry(f2,textvariable=edit_acronym, style =".TEntry") 
acronym_edit.grid(row = 0, column = 1,columnspan = 2, sticky = W) 
acronym_edit.focus_set() 
# adds expansion to be added with acronym 
text_add_label = Label(f2, text='enter expanded text to be added or edited',font=root.customFont) 
text_add_label.grid(row = 1, column = 0, sticky = W) 
textadd= Entry(f2, textvariable=add_expansion, style =".TEntry" , width = 40) 
textadd.grid(row = 1, column = 1,columnspan = 2, sticky = W) 

# output display 
display = Text(f2) 
display.config(height = 5, width = 80) 
display.grid(row = 6, column = 0,columnspan = 3, sticky = W) 

translate_button = ttk.Button(f1,text="SEARCH",command=find_acronym,style="Search.TButton") 
root.bind("<Return>", lambda event: translate_button.invoke())# this links <RETURN> to the convert function (otherwise called by the button as well) 
translate_button.grid(row=3, column = 0, sticky =W) 

# ADD/EDIT/DELETE 
add_button = ttk.Button(f2,text="add", command=add_acronym ,style="C.TButton",state = NORMAL) 
add_button.grid(row = 7, column = 0, sticky = W) 


root.mainloop() 

这进一步是我的问题realting到消息框。 askyesno需要双击才能生成预期的呼叫。字典格式为{'acronym':'扩展文本',...}

+0

此代码不会像发布一样工作 - 第一部分的缩进似乎是错误的。 –

+0

可能重复的[tkinter askyesno消息框行为](http://stackoverflow.com/questions/16522245/tkinter-askyesno-message-box-behaviour) –

回答

0

假设代码是正确的,至少有一个关键错误是您在两个地方拨打mainloop。在GUI程序中必须只调用一次mainloop

此外,你打电话askyesno两次,一次没有问题。也许这是你的问题。问题是这里:

messagebox.askyesno(0.0,'"{0}"{1} \n {2}\n'.format(search_analyte.get(),' is not in the database.','Add,if appropriate')) 
if messagebox.askyesno() == True: 
    open_second() 
else: 
    open_first() 

看看你是如何要求显示一个对话框两次?

+0

已经改变,如果真的工作。我误解了messagebox的功能,因为你看到并认为True的评估需要测试True/False的条件。愚蠢的错误,但对我来说是一个很好的学习点。谢谢 – user1478335