|
Post by Uncle Buddy on Sept 29, 2020 7:29:00 GMT -8
# messages.py (import as msg)
import tkinter as tk import styles as st import widgets as wdg import winsound
ST = st.ThemeStyles()
class Toplevelx(tk.Toplevel):
def __init__(self, parent=None, title=None, *args, **kwargs): tk.Toplevel.__init__(self, *args, **kwargs)
def winfo_subclass(self): ''' Works like built-in tkinter method winfo_class() except it gets subclass names of widget classes custom-made by inheritance ''' subclass = type(self).__name__ return subclass
class MessageModel(Toplevelx): ''' parent: modal dialog has no minimize/maximize buttons, goes where parent goes gparent: parent of parent might need to be destroyed on click of msg button title: goes on title bar of dialog using w.title('title') method prompt: tells the user to enter some data in an entry or combobox message: multiline Message widget text to inform user why dialog opened input: bad input from user displayed in dialog so he sees his typo/bad data x = inst.show(): data input by user into dialog is returned on dialog close ''' def __init__( self, parent, gparent=None, title=None, prompt=None, message=None, input=None, *args, **kwargs): Toplevelx.__init__(self, parent, title=None, *args, **kwargs)
self.edit_input = False self.add_data_to_db = True
self.parent = parent self.gparent = gparent self.prompt = prompt self.message = message self.input = input
if title: self.title(title)
self.grab_set() self.transient(parent)
self.make_widgets()
def make_widgets(self):
self.frm = wdg.Frame(self) self.frm.grid(column=0, row=0)
self.errlab = wdg.Label(self.frm, text='Input was: ') self.errlab.grid(column=0, row=0, padx=24, pady=(24,0), sticky='w') self.errshow = wdg.LabelItalic(self.frm) self.errshow.grid(column=1, row=0, pady=(24,0), sticky='w')
self.errmsg = tk.Message( self.frm, text=self.message, bd=3, relief='raised', aspect=400) self.errmsg.grid( column=0, row=1, padx=36, pady=36, ipadx=36, ipady=36, columnspan=2)
self.promptlab = wdg.Label(self.frm) self.var = tk.StringVar() self.altentry = wdg.Entry(self.frm) self.altentry.ent.config(textvariable=self.var)
self.promptlab.grid(column=0, row=2, pady=24, padx=24, sticky='w') self.altentry.grid(column=1, row=2, pady=24, sticky='w') self.promptlab.grid_remove() self.altentry.grid_remove()
self.altentry.focus_set() self.altentry.bind('<Return>', self.on_ok) self.bbox = wdg.Frame(self) self.bbox.grid(column=0, row=3, columnspan=2)
self.done = wdg.Button( self.bbox, text='DONE', command=self.on_ok, width=9) self.done.grid(column=0, row=0, padx=24, pady=(12,24)) self.done.grid_remove()
self.stop = wdg.Button( self.bbox, text='CANCEL1', command=self.on_cancel, width=9) self.stop.grid(column=1, row=0, padx=24, pady=(12,24)) self.stop.grid_remove()
self.done.focus_set()
self.accept = wdg.Button( self.bbox, text='Accept Original Input', command=self.on_ok, width=36) self.accept.grid(column=0, row=0, padx=24, pady=12) self.accept.grid_remove()
self.edit = wdg.Button( self.bbox, text='Submit Edited Input', command=self.on_alt, width=36) self.edit.grid(column=0, row=1, padx=24, pady=12) self.edit.grid_remove()
self.cancel_all = wdg.Button( self.bbox, text="Cancel (Don't Submit Anything)", command=self.on_cancel, width=36) self.cancel_all.grid(column=0, row=2, padx=24, pady=(12,36)) self.cancel_all.grid_remove()
self.bind('<Escape>', self.on_cancel)
winsound.PlaySound('SystemHand', winsound.SND_ASYNC)
self.protocol("WM_DELETE_WINDOW", self.on_cancel)
def on_ok(self, event=None): print('original input is OK; input is:', self.input)
def on_alt(self, event=None): self.edit_input = True self.destroy()
def on_cancel(self, event=None): self.add_data_to_db = False self.destroy()
class AltInputMessage(MessageModel): def __init__( self, parent, gparent=None, prompt=None, message=None, input=None, *args, **kwargs): MessageModel.__init__(self, parent, *args, **kwargs)
self.gparent = gparent
self.promptlab.grid() self.altentry.grid()
self.promptlab.config(text=prompt)
self.errshow.config(text=input) self.input = input
self.errmsg.config(text=message)
self.accept.grid() self.edit.grid() self.cancel_all.grid()
ST.config_generic(self)
def show(self): self.wm_deiconify() self.altentry.focus_force() self.wait_window() # won't work without this # Get return values accurate on window closing: return self.var.get(), self.edit_input, self.add_data_to_db
class YesNoMessage(MessageModel): def __init__(self, parent, message=None, input=None, *args, **kwargs): MessageModel.__init__(self, parent, *args, **kwargs)
self.input = input
self.errshow.config(text=self.input)
self.errmsg.config(text=message) self.done.config(text='SUBMIT') self.done.grid() self.stop.grid()
ST.config_generic(self)
def on_ok(self): self.parent.focus_set() self.destroy()
def show(self): self.wm_deiconify() self.wait_window() # won't work without this # Get return values accurate on window closing: return self.add_data_to_db
class ErrorMessage(MessageModel): def __init__(self, parent, message=None, input=None, *args, **kwargs): MessageModel.__init__( self, parent, *args, **kwargs)
if input: self.errshow.config(text=input) else: self.errlab.grid_forget() self.errlab.grid_forget()
self.errmsg.config(text=message)
self.done.grid()
self.done.config(command=self.cancel)
ST.config_generic(self)
def cancel(self): self.destroy()
if __name__ == "__main__":
def open_generic_dialog(): model = MessageModel(root)
root = tk.Tk() root.geometry("400x200") root.iconbitmap(default='c:/treebard_gps/favicon.ico')
button = wdg.Button(root, text="generic dialog", command=open_generic_dialog) label = tk.Label(root, text="", width=20) button.grid() label.grid() label2 = tk.Label(root, text="", width=20) label2.grid()
input = 'abcdefg'
# # SAMPLE INSTANCE ERROR MESSAGE # err = ErrorMessage( # root, # title='About Your Mistake', # message="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ac arcu quis justo maximus ultrices ac dignissim risus. In vitae facilisis nisl, eu pretium magna. Cras eros lacus, elementum nec odio vitae, dignissim vulputate diam. Pellentesque eget nulla semper, rhoncus leo tristique, hendrerit ligula. ", # input=input)
# SAMPLE INSTANCE YES/NO MESSAGE
yn = YesNoMessage( root, title='What Next?', message="Vestibulum feugiat mattis aliquet. Nunc et diam sed quam aliquam elementum sit amet ac dui. Aliquam convallis mi nec elit rutrum luctus. Vestibulum sed erat vitae est faucibus ullamcorper. Curabitur lacinia non arcu vitae varius.", input=input)
# # SAMPLE INSTANCE ALT INPUT MESSAGE
# altin = AltInputMessage( # root, # title='Change That?', # message="Maecenas quis elit eleifend, lobortis turpis at, iaculis odio. Phasellus congue, urna sit amet posuere luctus, mauris risus tincidunt sapien, vulputate scelerisque ipsum libero at neque. Nunc accumsan pellentesque nulla, a ultricies ex convallis sit amet. Etiam ut sollicitudin felis, sit amet dictum lacus. Mauris sed mattis diam.", # prompt='Replace input:', # input=input) # # data = altin.show() # run selectively per button not like this # # label2.config(text='You entered:\n' + data)
root.mainloop()
|
|