File: installgui.py

package info (click to toggle)
python-jtoolkit 0.7.8-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,436 kB
  • ctags: 2,536
  • sloc: python: 15,143; makefile: 20
file content (150 lines) | stat: -rwxr-xr-x 4,818 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""simple gui interfaces useful in installers for asking questions etc
supported are commandline, windows, Tk variants"""

# Copyright 2002, 2003 St James Software
# 
# This file is part of jToolkit.
#
# jToolkit is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# jToolkit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with jToolkit; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

class cmdline:
    """A command-line 'gui' interface"""
    def choosedirectory(self, title, initialdir):
        """asks the user for a directory"""
        path = raw_input(title + "[%s] " % initialdir)
        if path:
            return path
        else:
            return initialdir

    def askyesno(self, title, message):
        """asks the user a yes-no question"""
        print title
        response = raw_input(message + " ").lower()
        if response in ("y", "yes", "1", "t", "true"):
            return True
        elif response in ("n", "no", "0", "f", "false"):
            return False

    def displaymessage(self, title, message):
        """displays a message for the user"""
        print title
        print message

    def close(self):
        """closes the gui"""
        pass

class Tkgui:
    """A Tkinter-based gui interface"""
    def __init__(self):
        """This opens a gui interface if one is available"""
        # this code just hides the default Tk window
        from Tkinter import Tk
        self.root = Tk()
        self.root.withdraw()

    def choosedirectory(self, title, initialdir):
        """asks the user for a directory"""
        from tkFileDialog import askdirectory
        path = askdirectory(title=title, initialdir=initialdir, mustexist=1)
        return path

    def askyesno(self, title, message):
        """asks the user a yes-no question"""
        import tkMessageBox
        return tkMessageBox.askyesno(title, message)

    def displaymessage(self, title, message):
        """displays a message for the user"""
        import tkMessageBox
        tkMessageBox.showinfo(title, message)

    def close(self):
        """closes the gui"""
        if self.root is not None:
            self.root.quit()
            self.root.destroy()
            self.root = None

    def __del__(self):
        """make sure we're closed..."""
        self.close()

class win32nativegui(cmdline):
    """A native win32 gui"""
    def __init__(self):
        import win32com

    def choosedirectory(self, title, initialdir):
        """asks the user for a directory"""
        from win32com.shell import shell
        pidl, displayname, imagelist = shell.SHBrowseForFolder(0, None, title)
        path = shell.SHGetPathFromIDList(pidl)
        return path

    def askyesno(self, title, message):
        """asks the user a yes-no question"""
        import win32gui
        import win32con
        response = win32gui.MessageBox(0, message, title, win32con.MB_YESNO)
        if response == win32con.IDYES:
            return True
        elif response == win32con.IDNO:
            return False

    def displaymessage(self, title, message):
        """displays a message for the user"""
        import win32gui
        import win32con
        win32gui.MessageBox(0, message, title, win32con.MB_OK)

    def close(self):
        """closes the gui"""
        pass

guiclasses = (Tkgui, win32nativegui, cmdline)

def getgui(guioptions=None):
    """select an appropriate gui from the given options (in order of preference)"""
    if guioptions is None:
        guioptions = guiclasses
    for guiclass in guioptions:
       try:
           gui = guiclass()
           return gui
       except:
           pass

if __name__ == '__main__':
    import os
    for guiclass in guiclasses:
        print "testing",guiclass.__name__
        try:
            gui = guiclass()
        except Exception, error:
            print "couldn't use gui..."
            print error
            continue
        print "displaying message"
        gui.displaymessage("Title", "Testing displaymessage")
        response = gui.askyesno("Title", "Testing askyesno, what do you think?")
        print "yesno response:", response
        response = gui.choosedirectory("Testing choosedirectory", os.getcwd())
        print "dir response:", response