python 3.x - tkinter messagebox askyesno starnge response -
as per request bryan oakley,fuller code pertaining question testerday ( don't know how without question)
#!/usr/local/bin/python3 # -*- coding: utf-8 -*- import ast tkinter import * import tkinter.font tkinter import messagebox tkinter.scrolledtext import scrolledtext tkinter import ttk tkinter.ttk import * acronyms = {} open('new_dict.py','r', encoding = "utf-8") dic: acronyms = ast.literal_eval(dic.read()) def find_acronym(): found = false # if search term in database returns acronym , expansion 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(),' not in 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 referred work') root.configure(bg='darkgreen') #state variables app search_analyte = stringvar() # search criterion expanded_text=stringvar() # expanded text matching acronym # set 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 added. not required deletion edit_acronym = stringvar # acronym added ,edited or deleted # set 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 used addition (together text_add) , deletion acronym_edit_label = label(f2, text='enter acronym 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 added acronym text_add_label = label(f2, text='enter expanded text 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())# links <return> convert function (otherwise called button 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 further question realting messagebox. askyesno requires double-click generate expected call. dictionary in format {'acronym': 'expanded text',...}
assuming code correct, @ least 1 critical error you're calling mainloop in 2 places. mainloop must called once in gui program.
also, you're calling askyesno twice, once without question. perhaps problem. problem here:
messagebox.askyesno(0.0,'"{0}"{1} \n {2}\n'.format(search_analyte.get(),' not in database.','add,if appropriate')) if messagebox.askyesno() == true: open_second() else: open_first() see how you're asking display dialog twice?
Comments
Post a Comment