tkinter messagebox askyesno starnge response

时间:2013-05-15 11:34:52

标签: python-3.x tkinter tkmessagebox

根据Bryan Oakley的要求,我的问题与昨天有关的更全面的代码(不知道如何在没有其他问题的情况下这样做)

    #!/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()

这是我对问讯箱的疑问。 askyesno需要双击才能生成预期的呼叫。字典的格式为{'首字母缩略词':'扩展文本',...}

1 个答案:

答案 0 :(得分: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()

了解您如何要求两次显示对话框?