Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from __future__ import division
- from __future__ import print_function
- from __future__ import unicode_literals
- from __future__ import absolute_import
- # cross-version compatibility
- import sys
- if sys.version_info.major < 3:
- import Tkinter as tk
- import ttk
- import tkFileDialog as tkfiledialog
- else:
- import tkinter as tk
- from tkinter import ttk
- from tkinter import filedialog as tkfiledialog
- import os
- from collections import OrderedDict
- HOME_FOLDER_MODES = OrderedDict(
- [("Include /home/*", ""),
- ("Only include /home/* 's hidden files and folders.", "--exclude-home"),
- ("Exclude /home/*", "--exclude-home --no-hidden")])
- class Example(ttk.Frame):
- def __init__(self, parent):
- # We can't use super() because in Python 2 the Tkinter objects
- # derive from old-style classes.
- ttk.Frame.__init__(self, parent, borderwidth=3)
- self.parent = parent
- # set theming
- self.style = ttk.Style()
- self.style.theme_use("default")
- self.pack(fill="both", expand=1)
- # set grid configuration
- self.columnconfigure(1, weight=0)
- self.columnconfigure(2, weight=1)
- self.columnconfigure(3, weight=0)
- # Create the Tkinter Variables
- self.destination_folder = tk.StringVar()
- self.archiver = tk.StringVar()
- self.compression = tk.StringVar()
- self.home_folder = tk.StringVar()
- self.user_options = tk.StringVar()
- self.command = tk.StringVar()
- # Trace the Tkinter Variables!
- # http://stackoverflow.com/a/6549535/592289
- self.destination_folder.trace("w", self.construct_command)
- self.archiver.trace("w", self.construct_command)
- self.compression.trace("w", self.construct_command)
- self.home_folder.trace("w", self.construct_command)
- self.user_options.trace("w", self.construct_command)
- # Set default values to the Tkinter Variables
- self.destination_folder.set(os.path.expandvars("$HOME"))
- self.archiver.set("tar")
- self.compression.set("gzip")
- self.user_options.set("")
- self.construct_command()
- self.create_UI()
- def create_UI(self):
- self.add_choose_folder(row=1, text="Destination folder:", variable=self.destination_folder)
- self.add_combobox(row=2, text="Archiver:", variable=self.archiver, values=["tar", "bsdtar"])
- self.add_combobox(row=3, text="Compression:", variable=self.compression, values=["gzip", "xz"])
- self.add_entry(row=4, text="Additional archiver options:", variable=self.user_options)
- self.add_radiobuttons(row=5, variable=self.home_folder, modes=HOME_FOLDER_MODES)
- self.add_entry(row=8, text="Executable command:", variable=self.command)
- def add_radiobuttons(self, row, modes, variable):
- for i, (label, value) in enumerate(modes.items()):
- radio_button = ttk.Radiobutton(self, text=label, value=value, variable=variable)
- radio_button.grid(row=row + i, column=2, columnspan=1, sticky="nsew")
- def add_checkbutton(self, row, text, variable):
- check_button = ttk.Checkbutton(self, text=text, variable=variable)
- check_button.grid(row=row, column=1, columnspan=2, sticky="nsew")
- def add_choose_folder(self, row, text, variable):
- # create the widgets
- label = ttk.Label(self, text=text)
- entry = ttk.Entry(self, textvariable=variable, state="readonly")
- button = ttk.Button(self, text="...", width=2, command=lambda: variable.set(tkfiledialog.askdirectory()))
- # place the widgets
- label.grid(row=row, column=1, sticky="nse")
- entry.grid(row=row, column=2, sticky="nsew")
- button.grid(row=row, column=3)
- def add_combobox(self, row, text, variable, values):
- # create the widgets
- label = ttk.Label(self, text=text)
- box = ttk.Combobox(self, textvariable=variable, state="readonly", values=values)
- # place the widgets
- label.grid(row=row, column=1, sticky="nse")
- box.grid(row=row, column=2, sticky="nsew")
- def add_entry(self, row, text, variable):
- # create the widgets
- label = ttk.Label(self, text=text)
- entry = ttk.Entry(self, textvariable=variable)
- # place the widgets
- label.grid(row=row, column=1, sticky="nse")
- entry.grid(row=row, column=2, sticky="nsew")
- def construct_command(self, *args):
- """
- This callback function is called every time a Tkinter variable is
- changed.
- """
- arguments = [
- self.destination_folder.get(),
- "--archiver " + self.archiver.get(),
- "--compression " + self.compression.get(),
- self.home_folder.get(),
- self.user_options.get(),
- ]
- self.command.set("backup.sh " + " ".join(arguments))
- def main():
- root = tk.Tk()
- root.title("backup.sh GUI")
- app = Example(root)
- app.update()
- root.minsize(root.winfo_width(), root.winfo_height())
- root.mainloop()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment