0

As per request by Bryan Oakley,fuller code pertaining to my question testerday ( don't know how to do this without another question)

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

This is further to my question realting to the messagebox. askyesno which requires a double-click to generate expected call. The dictionary is in the format {'acronym': 'expanded text',...}

user1478335
  • 1,769
  • 5
  • 25
  • 37
  • This code won't work as posted -- the indentation of the first part seems to be in error. – Bryan Oakley May 15 '13 at 11:54
  • possible duplicate of [tkinter askyesno message box behaviour](http://stackoverflow.com/questions/16522245/tkinter-askyesno-message-box-behaviour) – Bryan Oakley May 15 '13 at 12:32

1 Answers1

0

Assuming the code is correct, at least one critical error is that you're calling mainloop in two places. mainloop must be called exactly once in a GUI program.

Also, you're calling askyesno twice, once without a question. Perhaps that is your problem. The problem is here:

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()

See how you're asking to display a dialog twice?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Have altered to if True which works. I misunderstood how messagebox would function as you saw and thought that evaluation of True would need the condition that tested True/False to be present as well. Silly mistake, but a great learning point for me. Thank you – user1478335 May 15 '13 at 12:04