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
- 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
- # Create the Tkinter Variables
- self.destination_folder = tk.StringVar()
- self.archiver = tk.StringVar()
- self.compression = tk.StringVar()
- self.exclude_home = tk.BooleanVar()
- self.no_hidden = tk.BooleanVar()
- self.user_options = tk.StringVar()
- # Set default values to the Tkinter Variables
- self.destination_folder.set("/home/faramir")
- self.archiver.set("tar")
- self.compression.set("gzip")
- self.exclude_home.set(False)
- self.no_hidden.set(False)
- self.user_options.set("")
- self.create_UI()
- def create_UI(self):
- # 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)
- # add widgets
- 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_checkbutton(row=5, text="Exclude /home but keep hidden files and folders", variable=self.exclude_home)
- self.add_checkbutton(row=6, text="Don't keep /home 's hidden files and folders.", variable=self.no_hidden)
- 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="...", command=lambda:variable.set(tkfiledialog.askdirectory()), width=2)
- # 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 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