View difference between Paste ID: i5uYPUMP and 3n2AuEXd
SHOW: | | - or go back to the newest paste.
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from __future__ import division
5
from __future__ import print_function
6
from __future__ import unicode_literals
7
from __future__ import absolute_import
8
9
# cross-version compatibility
10
import sys
11
if sys.version_info.major < 3:
12
    import Tkinter as tk
13
    import ttk
14
    import tkFileDialog as tkfiledialog
15
else:
16
    import tkinter as tk
17
    from tkinter import ttk
18
    from tkinter import filedialog as tkfiledialog
19
20
import os
21
22
23
class Example(ttk.Frame):
24
    def __init__(self, parent):
25
        # We can't use super() because in Python 2 the Tkinter objects
26
        # derive from old-style classes.
27
        ttk.Frame.__init__(self, parent, borderwidth=3)
28
        self.parent = parent
29
30
        # Create the Tkinter Variables
31
        self.destination_folder = tk.StringVar()
32
        self.archiver = tk.StringVar()
33
        self.compression = tk.StringVar()
34
        self.exclude_home = tk.BooleanVar()
35
        self.no_hidden = tk.BooleanVar()
36
        self.user_options = tk.StringVar()
37-
        self.destination_folder.set("/home/faramir")
37+
38
        # Set default values to the Tkinter Variables
39
        self.destination_folder.set(os.path.expandvars("$HOME"))
40
        self.archiver.set("tar")
41
        self.compression.set("gzip")
42
        self.exclude_home.set(False)
43
        self.no_hidden.set(False)
44
        self.user_options.set("")
45
46
        self.create_UI()
47
48
    def create_UI(self):
49
        # set theming
50
        self.style = ttk.Style()
51
        self.style.theme_use("default")
52
        self.pack(fill="both", expand=1)
53
54
        # set grid configuration
55
        self.columnconfigure(1, weight=0)
56
        self.columnconfigure(2, weight=1)
57
        self.columnconfigure(3, weight=0)
58
59
        # add widgets
60
        self.add_choose_folder(row=1, text="Destination folder:", variable=self.destination_folder)
61
        self.add_combobox(row=2, text="Archiver:", variable=self.archiver, values=["tar", "bsdtar"])
62
        self.add_combobox(row=3, text="Compression:", variable=self.compression, values=["gzip", "xz"])
63
        self.add_entry(row=4, text="Additional archiver options:", variable=self.user_options)
64
        self.add_checkbutton(row=5, text="Exclude /home but keep hidden files and folders", variable=self.exclude_home)
65
        self.add_checkbutton(row=6, text="Don't keep /home 's hidden files and folders.", variable=self.no_hidden)
66
67
    def add_checkbutton(self, row, text, variable):
68
        check_button = ttk.Checkbutton(self, text=text, variable=variable)
69
        check_button.grid(row=row, column=1, columnspan=2, sticky="nsew")
70
71
    def add_choose_folder(self, row, text, variable):
72
        # create the widgets
73
        label = ttk.Label(self, text=text)
74
        entry = ttk.Entry(self, textvariable=variable, state="readonly")
75
        button = ttk.Button(self, text="...", command=lambda:variable.set(tkfiledialog.askdirectory()), width=2)
76
        # place the widgets
77
        label.grid(row=row, column=1, sticky="nse")
78
        entry.grid(row=row, column=2, sticky="nsew")
79
        button.grid(row=row, column=3)
80
81
    def add_combobox(self, row, text, variable, values):
82
        # create the widgets
83
        label = ttk.Label(self, text=text)
84
        box = ttk.Combobox(self, textvariable=variable, state="readonly", values=values)
85
        # place the widgets
86
        label.grid(row=row, column=1, sticky="nse")
87
        box.grid(row=row, column=2, sticky="nsew")
88
89
    def add_entry(self, row, text, variable):
90
        # create the widgets
91
        label = ttk.Label(self, text=text)
92
        entry = ttk.Entry(self, textvariable=variable)
93
        # place the widgets
94
        label.grid(row=row, column=1, sticky="nse")
95
        entry.grid(row=row, column=2, sticky="nsew")
96
97
98
99
def main():
100
    root = tk.Tk()
101
    root.title("backup.sh GUI")
102
    app = Example(root)
103
    app.update()
104
    root.minsize(root.winfo_width(), root.winfo_height())
105
    root.mainloop()
106
107
108
if __name__ == '__main__':
109
    main()