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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# -*- coding: UTF8 -*-

import sqlite3 as s3
import Tkinter as tk

phone_list = 'phone.db'

class PhoneList:
    """This object contains all neccessary methods to manage the phone book."""
    
    def __init__ ( self ):
        "On init connection to the database is made. If no DB is present, a new DB is created."
        
        self.conn = s3.connect ( phone_list )
        self.cur = self.conn.cursor()
        tmp = self.cur.execute ( 'SELECT `tbl_name` FROM `sqlite_master` WHERE `tbl_name` = "phonelist" LIMIT 1' )
        if len ( tmp.fetchall() ) < 1:
            self.cur.execute ( "CREATE TABLE `phonelist` (`id` INTEGER PRIMARY KEY, `name` TEXT, `first` TEXT, `number` TEXT)" )

    def add ( self, data ):
        self.cur.execute ( "INSERT INTO `phonelist` VALUES ( NULL, ?, ?, ? )", data )
        self.conn.commit()

    def rem ( self, id ):
        self.cur.execute ( "DELETE FROM `phonelist` WHERE `id` = ?", (id,) )
        self.conn.commit()

    def change ( self, data, id ):
        data.append ( id )
        self.cur.execute ( "UPDATE `phonelist` SET `name` = ?, `first` = ?, `number` = ? WHERE `id` = ?", data )
        self.conn.commit()

    def get ( self, id=None ):
        if id == None:
            tmp = self.cur.execute ( 'SELECT * FROM `phonelist`' )
        else:
            tmp = self.cur.execute ( 'SELECT * FROM `phonelist` WHERE `id` = ?', (id,) )
        return tmp.fetchall()

    def close ( self ):
        self.cur.close()
        self.conn.close()

class PhoneGui:
    """The frontend to the PhoneList object."""

    def __init__ ( self ):
        "The main window"
        
        self.List = PhoneList()

        self.root = tk.Tk()
        top_frame = tk.Frame ( self.root )
        top_frame.pack ( side = 'top', fill = 'x' )
        left_frame = tk.Frame ( self.root, bg = 'orange' )
        left_frame.pack ( side = 'left', fill = 'y' )
        main_frame = tk.Frame ( self.root )
        main_frame.pack ( side = 'left', fill = 'both', expand = 1 )

        label = tk.Label ( top_frame, text = 'Telefon - Liste', bg = 'yellow' )
        label.pack ( side = 'top', expand = 1, fill = 'x' )

        button_0 = tk.Button ( left_frame, text = 'Neu', relief = 'ridge', bg = 'orange', command = self.entry_neu )
        button_0.pack ( side = 'top', fill = 'x' )
        button_1 = tk.Button ( left_frame, text = 'Ändern', relief = 'ridge', bg = 'orange', command = self.entry_update )
        button_1.pack ( side = 'top', fill = 'x' )
        button_2 = tk.Button ( left_frame, text = 'Löschen', relief = 'ridge', bg = 'orange', command = self.entry_delete )
        button_2.pack ( side = 'top', fill = 'x' )
        button_3 = tk.Button ( left_frame, text = 'Beenden', relief = 'ridge', bg = 'orange', command = self.quit_prog )
        button_3.pack ( side = 'bottom', fill = 'x' )

        scrollbar = tk.Scrollbar ( main_frame, orient = 'vertical' )
        self.listbox = tk.Listbox ( main_frame, yscrollcommand = scrollbar.set, width = 50, relief = 'ridge', selectmode = 'extended' )
        scrollbar.config ( command = self.listbox.yview )
        scrollbar.pack ( side = 'right', fill = 'y' )
        self.listbox.pack ( side = 'left', fill = 'both', expand = 1 )
        self.fill_listbox()
        self.root.mainloop()

    def fill_listbox ( self ):
        self.listbox.delete ( 0, 'end' )

        entries = self.List.get()
        for e in entries:
            l = 40 - 3 - len(e[1]) - len(e[2]) - 5
#           tmp = '%03i | ' % e[0] + e[1] + ', ' + e[2] \
#               + '.' * l + e[3]
            tmp = '%03i | %s, %s %s %s' % (e[0], e[1], e[2], l*'.', e[3])
            self.listbox.insert ( 'end', tmp )

    def entry_neu ( self, update=None, id=None ):
        self.top = tk.Toplevel()
        truth_val = update == None and id == None
        if truth_val:
            titel = 'Neuer Eintrag'
            self.update_params = ( None, None )
        else:
            titel = 'Ändern eines Eintrags'
            self.update_params = ( update, id )

        label = tk.Label ( self.top, text = titel, bg = 'cyan' )
        label.grid ( row = 0, column = 0, columnspan = 2, sticky = 'we' )
        label = tk.Label ( self.top, text = 'Name:' )
        label.grid ( row = 1, column = 0, sticky = 'e' )
        label = tk.Label ( self.top, text = 'Vorname:' )
        label.grid ( row = 2, column = 0, sticky = 'e' ) 
        label = tk.Label ( self.top, text = 'Nummer:' )
        label.grid ( row = 3, column = 0, sticky = 'e' )

        self.entry = []
        self.entry.append ( tk.Entry ( self.top, width = 20, relief = 'ridge' ) )
        self.entry.append ( tk.Entry ( self.top, width = 20, relief = 'ridge' ) )
        self.entry.append ( tk.Entry ( self.top, width = 20, relief = 'ridge' ) )
        if not truth_val:
            tmp = self.List.get(id)
            self.entry[0].insert ( 0, tmp[0][1])
            self.entry[1].insert ( 0, tmp[0][2])
            self.entry[2].insert ( 0, tmp[0][3])

        tmp = 1
        for e in self.entry:
            e.grid ( row = tmp, column = 1, sticky = 'we' )
            tmp += 1

        button = tk.Button ( self.top, text = 'Abbrechen', command = self.top.destroy )
        button.grid ( row = 4, column = 0, sticky = 'we' )
        button = tk.Button ( self.top, text = 'Speichern', command = self.entry_neu_continue, bg = 'lightgreen' )
        button.grid ( row = 4, column = 1, sticky = 'we' )

    def entry_neu_continue ( self ):
        tmp = []
        for e in self.entry:
            tmp.append ( e.get() )
        
        truth_val = self.update_params[0] == None \
            and self.update_params[1] == None
        if truth_val:
            self.List.add ( tmp )
        else:
            self.List.change ( tmp, self.update_params[1] )
            del ( self.update_params )
        self.fill_listbox()
        self.top.destroy()

    def entry_delete ( self ):
        to_delete = self.listbox.curselection()
        for ind in to_delete:
            tmp = self.listbox.get ( ind )
            self.List.rem ( int(tmp[:3]) )

        self.fill_listbox()

    def entry_update ( self ):
        to_change = self.listbox.curselection()
        try:
            tmp = self.listbox.get ( to_change[0] )
        except IndexError:
            pass
        except:
            raise
        else:
            self.entry_neu ( update = True, id = int( tmp[:3] ) )
            if len ( to_change ) > 1:
                print 'Nur erster Eintrag wird geändert'

    def quit_prog ( self ):
        self.List.close()
        self.root.destroy()

if __name__ == '__main__':
    PhoneGui()