from libtovid.netagui import Style
import sys, os
class ConfirmDialog(AppDialog):
"""A generic dialog class that allows you to make a dialog with single or
multiple buttons, and a choice of text or image (GIF only). Providing
text AND image makes a 2nd text label underneath the image label. Example:
from Tkinter import Tk
from libtovid.appdialogs import ConfirmDialog
btns='Preview OK ?, No'
root = Tk()
a = ConfirmDialog(root, 'Dialog', 'Menu Preview', btns, '/home/me/pv.gif')
a.run()
The exit codes of the buttons start at 0 and increment
"""
def __init__(self, parent, title='', text='', button_text='', image=''):
AppDialog.__init__(self,parent)
self.parent = parent
self.text = text
self._title = title
default_button_text = ['OK']
if button_text:
self.button_text = []
if type(button_text) == str:
self.button_text.append(button_text)
else:
self.button_text.extend(button_text)
else:
self.button_text = default_button_text
self.image = image
# get metagui font configuration
self.inifile = os.path.expanduser('~/.metagui/config')
self.style = Style()
self.style.load(self.inifile)
# draw the title
self.parent.title(self._title)
# draw the text and/or image
if self.image:
self.label1 = Label(parent, bd=2, relief='groove')
self.label1.pack(padx=20, pady=20, side='top')
if self.text:
label2 = Label(self.parent, text=self.text, font=self.lrg_font, \
justify='left', padx=20, pady=20)
label2.pack(side='top', fill='both', expand=1)
elif self.text:
label2 = Label(self.parent, text=self.text, font=self.style.font, \
justify='left', padx=20, pady=20)
label2.pack(side='top', fill='both', expand=1)
# draw the button(s) in a frame
frame = Frame(self.parent)
buttons = []
for index, item in enumerate(self.button_text):
button = Button(frame, text=item, relief='groove', overrelief='raised', \
anchor='s', command=lambda index=index:self.exit_with_status(index), \
#anchor='s', command=lambda index=index:sys.exit(index), \
font=self.style.font)
button.pack(side='left')
frame.pack(side='bottom')
def exit_with_status(self, status):
print status
self.parent.destroy()
def set_image(self):
img = PhotoImage(file=self.image)
self.im = img
self.label1.config(image=self.im)
def run(self):
if self.image:
self.set_image()
self.mainloop()