tk_customdialog.py 4.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Provides customized dialogs extending Tkinter.Toplevel.
    
    Name: tk_customdialog.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

from collections import OrderedDict
    
from tk_config import TkConfig

class SimpleDialog(Tk.Toplevel):
    
    
    def __init__(self, parent, title = None, command_ok = None):
        self.parent = parent

        Tk.Toplevel.__init__(self, self.parent, padx=10, pady=10)
        self.transient(self.parent)

        if title is not None:
            self.title(title)

        self.grab_set()
        
        self.geometry("+%d+%d" % (parent.winfo_width()/3,
                                    parent.winfo_height()/3))

        self.protocol("WM_DELETE_WINDOW", self.destroy)
    
    
class ConfigDialog(SimpleDialog):
    
    
    def __init__(self, parent, title = None, configs = None, command_ok = None):
        SimpleDialog.__init__(self, parent, title, command_ok)
    
        self._configs = None
        if configs is not None:
            self.add_configs(configs, command_ok)
        
        
    def add_configs(self, configs, command_ok):
        row = 0
        self._configs = OrderedDict()
        for key in configs:
            if configs[key].hidden == False:
                Tk.Label(self, text=configs[key].label).grid(row=row, padx=4, pady=4, sticky=Tk.W)
                
                entry = Tk.Entry(self)
                entry.insert(0, str(configs[key].value))
                entry.grid(row=row, column=1, padx=4, pady=4, sticky=Tk.W)
                if row == 0:
                    entry.focus_set()
                row += 1
            
                self._configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, tk_entry=entry, hidden=configs[key].hidden, meta=configs[key].meta ) 
            else:
                self._configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, hidden=configs[key].hidden, meta=configs[key].meta )

        B1 = Tk.Button(self, text="Ok", width=5, command = command_ok)
        B1.grid(row=row, padx=6, pady=6, sticky=Tk.W)
        
        self.bind("<Return>", command_ok)
        
        B2 = Tk.Button(self, text="Cancel", width=5, command = self.destroy)
        B2.grid(row=row, column=1, padx=6, pady=6, sticky=Tk.W)
        
        self.bind("<Escape>", lambda *_: self.destroy())

        
    def update_and_validate_configs(self):
        for key in self._configs:
            self._configs[key].value = self._configs[key].get_entry_val()
        
    def get_configs(self):
        return self._configs

        
class ChooseOneDialog(SimpleDialog):
    
    def __init__(self, parent, title = None, configs = None, command_ok = None):
        SimpleDialog.__init__(self, parent, title, command_ok)
        
        self.v = Tk.StringVar()
    
        self._configs = None
        if configs is not None:
            self.add_configs(configs, command_ok)
        
        
    def add_configs(self, configs, command_ok):
        row = 0
        self._configs = OrderedDict()
        for key in configs:
            if configs[key].hidden == False:
                Tk.Label(self, text=configs[key].label).grid(row=row, padx=4, pady=4, sticky=Tk.W)
                
                radio = Tk.Radiobutton(self, text=configs[key].label, value=key, variable=self.v, width=25, 
                                bg='white', fg='black', padx=4, pady=4, indicatoron=1, anchor=Tk.W, 
                                activebackground='white', activeforeground='black', highlightbackground='white')

                radio.grid(row=row, padx=4, pady=4, sticky=Tk.W)
                if configs[key].value == True:
                    radio.select()
                row += 1
            
                self._configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, tk_entry=radio, hidden=configs[key].hidden, meta=configs[key].meta) 
            else:
                self._configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, hidden=configs[key].hidden, meta=configs[key].meta )

        B1 = Tk.Button(self, text="Ok", width=5, command = command_ok)
        B1.grid(row=row, padx=6, pady=6, sticky=Tk.W+Tk.E)
        
        self.bind("<Return>", command_ok)
        
        #B2 = Tk.Button(self, text="Cancel", width=5, command = self.destroy)
        #B2.grid(row=row+1, column=0, padx=6, pady=6, sticky=Tk.W)
        
        self.bind("<Escape>", lambda *_: self.destroy())
        
    def update_and_validate_configs(self):
        for key in self._configs:
            self._configs[key].value = False
        self._configs[self.v.get()].value = True
        
    def get_configs(self):
        return self._configs