Advertisement
here2share

# Python3_to_HTML5.py

Jul 4th, 2022
817
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 32.99 KB | None
  1. # Python3_to_HTML5.py
  2.  
  3. import sys, os
  4. import os.path
  5. import pickle
  6. import re
  7. import time
  8. import shutil
  9. import random
  10. import subprocess
  11. import urllib.request
  12. import urllib.error
  13. import urllib.parse
  14. import tkinter as tk
  15. import tkinter.font
  16. import tkinter.ttk
  17. import tkinter.scrolledtext
  18. import tkinter.filedialog
  19.  
  20. try:
  21.     from PIL import Image
  22.     from PIL import ImageTk
  23. except:
  24.     raise Exception('PIL is not installed.')
  25. # consts
  26. PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
  27.  
  28. # classes
  29.  
  30. class MyTk(tk.Tk):
  31.     def __init__(self):
  32.         super().__init__()
  33.         self.update()
  34.         self.width = self.winfo_width()
  35.         self.height = self.winfo_height()
  36.         self.x = self.winfo_screenwidth() // 2 - self.width // 2
  37.         self.y = self.winfo_screenheight() // 2 - self.height // 2
  38.         self.geometry('+{}+{}'.format(self.x, self.y))
  39.         self.tk_setPalette(background='#ececec')
  40.         self.protocol('WM_DELETE_WINDOW', self.quit)
  41.  
  42. class App(tk.Frame):
  43.     def __init__(self, master=None):
  44.         super().__init__(master)
  45.         #self.pack(fill=tk.BOTH, expand=1)
  46.         self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
  47.         self.master.title('Python 3 to HTML5')
  48.         self.master.bind('<Configure>', self._configure_window)
  49.         self.master.bind('<Escape>', self.quit)
  50.         self.create_widgets()
  51.     def _configure_window(self, event):
  52.         if event.width != self.master.winfo_width():
  53.             self.master.grid_columnconfigure(0, weight=1)
  54.         if event.height != self.master.winfo_height():
  55.             self.master.grid_rowconfigure(0, weight=1)
  56.     def quit(self, event=None):
  57.         self.master.destroy()
  58.         self.master.quit()
  59.     def create_widgets(self):
  60.         # title
  61.         self.title = tk.Label(self, text='Python 3 to HTML5', font=tk.font.Font(family='Helvetica', size=14, weight='bold'))
  62.         self.title.grid(row=0, column=0, columnspan=2, padx=10, pady=10, sticky=tk.N)
  63.         # input
  64.         self.input_label = tk.Label(self, text='Input')
  65.         self.input_label.grid(row=1, column=0, padx=10, pady=10, sticky=tk.W)
  66.         self.input_entry = tk.Entry(self, width=100)
  67.         self.input_entry.grid(row=1, column=1, padx=10, sticky=tk.E+tk.W)
  68.         self.input_button = tk.Button(self, text='Browse', command=self.browse_input)
  69.         self.input_button.grid(row=1, column=2, padx=10, pady=10, sticky=tk.E)
  70.         # output
  71.         self.output_label = tk.Label(self, text='Output')
  72.         self.output_label.grid(row=2, column=0, padx=10, pady=10, sticky=tk.W)
  73.         self.output_entry = tk.Entry(self, width=100)
  74.         self.output_entry.grid(row=2, column=1, padx=10, sticky=tk.E+tk.W)
  75.         self.output_button = tk.Button(self, text='Browse', command=self.browse_output)
  76.         self.output_button.grid(row=2, column=2, padx=10, pady=10, sticky=tk.E)
  77.         # run
  78.         self.run_button = tk.Button(self, text='Run', command=self.run, width=100)
  79.         self.run_button.grid(row=3, column=0, columnspan=3, padx=10, pady=10, sticky=tk.E+tk.W)
  80.         # description
  81.         self.description_label = tk.Label(self, text='Description')
  82.         self.description_label.grid(row=4, column=0, padx=10, pady=10, sticky=tk.W)
  83.         self.description_text = tkinter.scrolledtext.ScrolledText(self)
  84.         self.description_text.grid(row=4, column=1, padx=10, sticky=tk.E+tk.W)
  85.         # output console
  86.         self.output_label = tk.Label(self, text='Output console')
  87.         self.output_label.grid(row=5, column=0, padx=10, pady=10, sticky=tk.W)
  88.         self.output_text = tkinter.scrolledtext.ScrolledText(self)
  89.         self.output_text.grid(row=5, column=1, padx=10, sticky=tk.E+tk.W)
  90.         # status
  91.         self.status_label = tk.Label(self, text='Status')
  92.         self.status_label.grid(row=6, column=0, padx=10, pady=10, sticky=tk.W)
  93.         self.status_text = tkinter.scrolledtext.ScrolledText(self)
  94.         self.status_text.grid(row=6, column=1, padx=10, sticky=tk.E+tk.W)
  95.         # settings
  96.         self.settings_button = tk.Button(self, text='Settings', command=self.settings)
  97.         self.settings_button.grid(row=7, column=0, padx=10, pady=10, sticky=tk.E+tk.W)
  98.         # about
  99.         self.about_button = tk.Button(self, text='About', command=self.about)
  100.         self.about_button.grid(row=7, column=1, padx=10, pady=10, sticky=tk.E+tk.W)
  101.         # quit
  102.         self.quit_button = tk.Button(self, text='Quit', command=self.quit, width=100)
  103.         self.quit_button.grid(row=8, column=0, columnspan=3, padx=10, pady=10, sticky=tk.E+tk.W)
  104.         self.grid_rowconfigure(0, weight=0)
  105.         self.grid_rowconfigure(1, weight=0)
  106.         self.grid_rowconfigure(2, weight=0)
  107.         self.grid_rowconfigure(3, weight=0)
  108.         self.grid_rowconfigure(4, weight=1)
  109.         self.grid_rowconfigure(5, weight=1)
  110.         self.grid_rowconfigure(6, weight=1)
  111.         self.grid_rowconfigure(7, weight=0)
  112.         self.grid_rowconfigure(8, weight=0)
  113.         self.grid_columnconfigure(0, weight=1)
  114.         self.grid_columnconfigure(1, weight=1)
  115.         self.grid_columnconfigure(2, weight=0)
  116.         self.update()
  117.         self.width = self.winfo_width()
  118.         self.height = self.winfo_height()
  119.         self.x = self.master.winfo_screenwidth() // 2 - self.width // 2
  120.         self.y = self.master.winfo_screenheight() // 2 - self.height // 2
  121.         self.master.geometry('+{}+{}'.format(self.x, self.y))
  122.         self.master.grid_columnconfigure(0, weight=1)
  123.         self.master.grid_rowconfigure(0, weight=1)
  124.     def browse_input(self):
  125.         path = tk.filedialog.askdirectory()
  126.         if path:
  127.             self.input_entry.insert(tk.END, path)
  128.     def browse_output(self):
  129.         path = tk.filedialog.askdirectory()
  130.         if path:
  131.             self.output_entry.insert(tk.END, path)
  132.     def about(self):
  133.         about = """
  134. Python 3 to HTML5
  135. Version 1.0.0
  136. Author: Kirk Lawrence
  137. """
  138.         messagebox.showinfo('About', about)
  139.     def settings(self):
  140.         self.settings = Settings(self)
  141.         self.wait_window(self.settings)
  142.     def run(self):
  143.         input = self.input_entry.get()
  144.         output = self.output_entry.get()
  145.         if not input or not output:
  146.             messagebox.showerror('Error', 'Input and output must be specified')
  147.             return
  148.         if not os.path.exists(input):
  149.             messagebox.showerror('Error', 'Input directory doesn't exist')
  150.             return
  151.         if not os.path.exists(output):
  152.             messagebox.showerror('Error', 'Output directory doesn't exist')
  153.             return
  154.         if not os.path.isdir(output):
  155.             messagebox.showerror('Error', 'Output must be a directory')
  156.             return
  157.         self.clear_output()
  158.         self.clear_status()
  159.         self.write_output('Processing...')
  160.         self.master.update()
  161.         input = os.path.abspath(input)
  162.         output = os.path.abspath(output)
  163.         self.write_output('Input directory: {}'.format(input))
  164.         self.write_output('Output directory: {}'.format(output))
  165.         self.write_output()
  166.         start_time = time.time()
  167.         self.process_dir(input, output)
  168.         end_time = time.time()
  169.         self.write_output()
  170.         self.write_output('Total time: {}'.format(self.get_time(end_time - start_time)))
  171.         self.write_output('Done.')
  172.         self.write_status('Done.')
  173.         messagebox.showinfo('Done', 'Done.')
  174.     def process_dir(self, input, output):
  175.         if not os.path.exists(output):
  176.             os.makedirs(output)
  177.         for name in os.listdir(input):
  178.             path = os.path.join(input, name)
  179.             if not os.path.isdir(path):
  180.                 if not self.is_python_file(name):
  181.                     continue
  182.                 self.write_output('Found Python file: {}'.format(path))
  183.                 self.write_output()
  184.                 self.process_file(path, output)
  185.                 self.write_output()
  186.             else:
  187.                 new_output = os.path.join(output, name)
  188.                 self.write_output('Entering directory: {}'.format(path))
  189.                 self.write_output()
  190.                 self.process_dir(path, new_output)
  191.                 self.write_output()
  192.                 self.write_output('Exiting directory: {}'.format(path))
  193.     def process_file(self, path, output):
  194.         # open file
  195.         self.write_output('Opening file: {}'.format(path))
  196.         with open(path, 'r') as f:
  197.             data = f.read()
  198.         # write to output directory
  199.         name, ext = os.path.splitext(path)
  200.         ext = ext.lower()
  201.         name = os.path.basename(name)
  202.         if not ext:
  203.             ext = '.html'
  204.         output_path = os.path.join(output, name + ext)
  205.         self.write_output('Writing to: {}'.format(output_path))
  206.         with open(output_path, 'w') as f:
  207.             f.write(self.convert(data))
  208.         # run
  209.         self.write_output()
  210.         self.write_output('Running...')
  211.         self.run_file(output_path)
  212.     def convert(self, data):
  213.         # convert
  214.         self.write_output('Converting...')
  215.         description = '
  216. '.join(self.get_description(data))
  217.         self.write_description(description)
  218.         return self.run_conversion(data)
  219.     def run_conversion(self, data):
  220.         return data
  221.     def run_file(self, path):
  222.         pass
  223.     def get_description(self, data):
  224.         lines = []
  225.         for line in data.splitlines():
  226.             line = line.strip()
  227.             if line.startswith('#'):
  228.                 lines.append(line)
  229.             else:
  230.                 break
  231.         return lines
  232.     def is_python_file(self, name):
  233.         return name.endswith('.py')
  234.     def get_time(self, seconds):
  235.         m, s = divmod(seconds, 60)
  236.         h, m = divmod(m, 60)
  237.         return '%02d:%02d:%02d' % (h, m, s)
  238.     def write_output(self, text=''):
  239.         self.output_text.insert(tk.END, text + '
  240. ')
  241.         self.output_text.see('end')
  242.         self.master.update()
  243.     def write_description(self, text=''):
  244.         self.description_text.insert(tk.END, text + '
  245. ')
  246.         self.description_text.see('end')
  247.         self.master.update()
  248.     def write_status(self, text=''):
  249.         self.status_text.insert(tk.END, text + '
  250. ')
  251.         self.status_text.see('end')
  252.         self.master.update()
  253.     def clear_output(self):
  254.         self.output_text.delete('1.0', tk.END)
  255.         self.master.update()
  256.     def clear_description(self):
  257.         self.description_text.delete('1.0', tk.END)
  258.         self.master.update()
  259.     def clear_status(self):
  260.         self.status_text.delete('1.0', tk.END)
  261.         self.master.update()
  262.  
  263. class Settings(tk.Toplevel):
  264.     def __init__(self, master=None):
  265.         super().__init__(master)
  266.         self.master = master
  267.         self.title('Settings')
  268.         self.protocol('WM_DELETE_WINDOW', self.quit)
  269.         self.create_widgets()
  270.     def quit(self):
  271.         self.destroy()
  272.     def create_widgets(self):
  273.         self.grid_rowconfigure(0, weight=0)
  274.         self.grid_columnconfigure(0, weight=1)
  275.         self.grid_columnconfigure(1, weight=1)
  276.         # buttons
  277.         self.button_frame = tk.Frame(self)
  278.         self.button_frame.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky=tk.E+tk.W)
  279.         self.grid_rowconfigure(1, weight=0)
  280.         self.quit_button = tk.Button(self.button_frame, text="Quit", command=self.quit)
  281.         self.quit_button.grid(row=0, column=0, padx=5, pady=5)
  282.         self.start_button = tk.Button(self.button_frame, text="Start", command=self.start)
  283.         self.start_button.grid(row=0, column=1, padx=5, pady=5)
  284.         self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop)
  285.         self.stop_button.grid(row=0, column=2, padx=5, pady=5)
  286.         # canvas
  287.         self.canvas = tk.Canvas(self, width=200, height=200, bg="white")
  288.         self.canvas.grid(row=0, column=0, columnspan=2, padx=10, pady=10)
  289.         # self.canvas.bind("<B1-Motion>", self.paint)
  290.         # mouse tracker
  291.         self.mouse_tracker = tk.Label(self, text="Mouse position: ")
  292.         self.mouse_tracker.grid(row=2, column=0, sticky=tk.W, padx=10, pady=10)
  293.         # mouse tracker
  294.         self.mouse_pos_label = tk.Label(self, text="")
  295.         self.mouse_pos_label.grid(row=2, column=1, sticky=tk.E, padx=10, pady=10)
  296.     def paint(self, event):
  297.         x1, y1 = (event.x - 1), (event.y - 1)
  298.         x2, y2 = (event.x + 1), (event.y + 1)
  299.         self.canvas.create_oval(x1, y1, x2, y2, fill="black")
  300.     def mouse_pos_update(self, event):
  301.         self.mouse_pos_label["text"] = "("+str(event.x)+","+str(event.y)+")"
  302.  
  303. class Py3_to_HTML5:
  304.     def __init__(self, source, options=''):
  305.         self._pygments = pygments.lexers.get_lexer_by_name('html+javascript')
  306.         self._pygments = pygments.lexers.get_lexer_by_name('python3')
  307.         self._source = source
  308.         self._options = options
  309.     def get_syntax(self):
  310.         return '<pre>' + pygments.highlight(self._source, self._pygments,
  311.                                             pygments.formatters.HtmlFormatter()) + '</pre>'
  312.     def get_syntax_with_linenos(self):
  313.         return '<pre>' + pygments.highlight(self._source, self._pygments,
  314.                                             pygments.formatters.HtmlFormatter(linenos='table')) + '</pre>'
  315.     def get_syntax_with_linenos_and_options(self, options):
  316.         return '<pre>' + pygments.highlight(self._source, self._pygments,
  317.                                             pygments.formatters.HtmlFormatter(linenos=options)) + '</pre>'
  318.  
  319. class HTML5Highlighter:
  320.     def __init__(self, source, options=''):
  321.         self._pygments = pygments.lexers.get_lexer_by_name('html+javascript')
  322.         self._source = source
  323.         self._options = options
  324.  
  325. class ASMHighlighter:
  326.     def __init__(self, source, options=''):
  327.         self._pygments = pygments.lexers.get_lexer_by_name('x86asm')
  328.         self._source = source
  329.         self._options = options
  330.  
  331. class ShellHighlighter:
  332.     def __init__(self, source, options=''):
  333.         self._pygments = pygments.lexers.get_lexer_by_name('console')
  334.         self._source = source
  335.         self._options = options
  336.  
  337. class Z80Highlighter:
  338.     def __init__(self, source, options=''):
  339.         self._pygments = pygments.lexers.get_lexer_by_name('z80')
  340.         self._source = source
  341.         self._options = options
  342.  
  343. class Asm8085Highlighter:
  344.     def __init__(self, source, options=''):
  345.         self._pygments = pygments.lexers.get_lexer_by_name('asm')
  346.         self._source = source
  347.         self._options = options
  348.  
  349. class Asm8086Highlighter:
  350.     def __init__(self, source, options=''):
  351.         self._pygments = pygments.lexers.get_lexer_by_name('x86asm')
  352.         self._source = source
  353.         self._options = options
  354.  
  355. class Asm8051Highlighter:
  356.     def __init__(self, source, options=''):
  357.         self._pygments = pygments.lexers.get_lexer_by_name('asm')
  358.         self._source = source
  359.         self._options = options
  360.  
  361. class Asm8039Highlighter:
  362.     def __init__(self, source, options=''):
  363.         self._pygments = pygments.lexers.get_lexer_by_name('asm')
  364.         self._source = source
  365.         self._options = options
  366.  
  367. class Asm6809Highlighter:
  368.     def __init__(self, source, options=''):
  369.         self._pygments = pygments.lexers.get_lexer_by_name('asm')
  370.         self._source = source
  371.         self._options = options
  372.  
  373. class Asm6510Highlighter:
  374.     def __init__(self, source, options=''):
  375.         self._pygments = pygments.lexers.get_lexer_by_name('asm')
  376.         self._source = source
  377.         self._options = options
  378.  
  379. class write_py3_to_js:
  380.     def __init__(self, value):
  381.         self.value = value
  382.         self.encode('utf-8')
  383.     def __str__(self):
  384.         return self.value
  385.     def encode(self, enc):
  386.         self.value = self.value.encode(enc)
  387.     def decode(self, enc):
  388.         self.value = self.value.decode(enc)
  389.     def tk_menu_item(menu, text, command):
  390.         menu.add_command(label=text, command=command)
  391.     def tk_menu_separator(menu):
  392.         menu.add_separator()
  393.     def tk_menu_item_enable(menu, text, command, enable):
  394.         menu.add_command(label=text, command=command, state=('disabled' if not enable else 'normal'))
  395.     def tk_menu_item_check(menu, text, command, var, value):
  396.         menu.add_checkbutton(label=text, command=command, variable=var, onvalue=value)
  397.     def tk_menu_item_radio(menu, text, command, var, value):
  398.         menu.add_radiobutton(label=text, command=command, variable=var, value=value)
  399.     def tk_menu_item_radio_get(menu):
  400.         for item in menu.children.values():
  401.             if item.type == 'radiobutton':
  402.                 if item.select():
  403.                     return item
  404.         return None
  405.     def tk_menu_item_radio_get_value(menu):
  406.         for item in menu.children.values():
  407.             if item.type == 'radiobutton':
  408.                 if item.select():
  409.                     return item.variable.get()
  410.         return None
  411.     def tk_menu_item_radio_get_text(menu):
  412.         for item in menu.children.values():
  413.             if item.type == 'radiobutton':
  414.                 if item.select():
  415.                     return item['text']
  416.         return None
  417.     def tk_menu_item_radio_get_index(menu):
  418.         index = 0
  419.         for item in menu.children.values():
  420.             if item.type == 'radiobutton':
  421.                 if item.select():
  422.                     return index
  423.                 index += 1
  424.         return None
  425.     def tk_menu_item_radio_select(menu, text):
  426.         for item in menu.children.values():
  427.             if item.type == 'radiobutton':
  428.                 if item['text'] == text:
  429.                     item.select()
  430.     def tk_menu_item_radio_select_index(menu, index):
  431.         for item in menu.children.values():
  432.             if item.type == 'radiobutton':
  433.                 if index == 0:
  434.                     item.select()
  435.                     return
  436.                 index -= 1
  437.     def tk_menu_item_radio_get_all_text(menu):
  438.         result = []
  439.         for item in menu.children.values():
  440.             if item.type == 'radiobutton':
  441.                 result.append(item['text'])
  442.         return result
  443.     def tk_menu_item_radio_get_all_value(menu):
  444.         result = []
  445.         for item in menu.children.values():
  446.             if item.type == 'radiobutton':
  447.                 result.append(item.variable.get())
  448.         return result
  449.     def tk_menu_item_radio_get_all_index_value(menu):
  450.         result = []
  451.         index = 0
  452.         for item in menu.children.values():
  453.             if item.type == 'radiobutton':
  454.                 result.append((index, item.variable.get()))
  455.                 index += 1
  456.         return result
  457.     def tk_menu_item_radio_get_all_index_text(menu):
  458.         result = []
  459.         index = 0
  460.         for item in menu.children.values():
  461.             if item.type == 'radiobutton':
  462.                 result.append((index, item['text']))
  463.                 index += 1
  464.         return result
  465.     def tk_popup_menu(root, x, y, items):
  466.         popup = tk.Menu(root, tearoff=0)
  467.         popup.post(x, y)
  468.         for item in items:
  469.             if item[0] == '-':
  470.                 tk_menu_separator(popup)
  471.             elif isinstance(item, tuple):
  472.                 tk_menu_item(popup, item[0], item[1])
  473.             elif isinstance(item, list):
  474.                 tk_menu_item_enable(popup, item[0], item[1], item[2])
  475.             elif isinstance(item, dict):
  476.                 tk_menu_item_check(popup, item[0], item[1], item[2], item[3])
  477.             else:
  478.                 raise Exception('Unsupported item in popup menu: {}'.format(item))
  479.     def tk_get_screen_size():
  480.         root = tk.Tk()
  481.         root.withdraw()
  482.         x = root.winfo_screenwidth()
  483.         y = root.winfo_screenheight()
  484.         root.destroy()
  485.         return x, y
  486.     def tk_get_default_font():
  487.         root = tk.Tk()
  488.         root.withdraw()
  489.         font = tk.font.nametofont('TkDefaultFont')
  490.         font.configure(size=10)
  491.         root.destroy()
  492.         return font
  493.     def tk_center_window(root, width, height):
  494.         screen_width = root.winfo_screenwidth()
  495.         screen_height = root.winfo_screenheight()
  496.         x = (screen_width // 2) - (width // 2)
  497.         y = (screen_height // 2) - (height // 2)
  498.         root.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  499.     def tk_set_window_icon(root, icon_path):
  500.         if sys.platform == 'win32':
  501.             root.iconbitmap(icon_path)
  502.         else:
  503.             img = tk.PhotoImage(file=icon_path)
  504.             root.tk.call('wm', 'iconphoto', root._w, img)
  505.     def tk_set_window_title(root, title):
  506.         root.title(title)
  507.     def tk_set_window_transparent(root, percent):
  508.         percent = int(percent)
  509.         if percent < 0:
  510.             percent = 0
  511.         if percent > 100:
  512.             percent = 100
  513.         alpha = percent * 65535 // 100
  514.         root.attributes('-alpha', alpha)
  515.     def tk_set_window_always_on_top(root, ontop):
  516.         if ontop:
  517.             root.attributes('-topmost', True)
  518.         else:
  519.             root.attributes('-topmost', False)
  520.     def tk_set_window_no_resize(root):
  521.         root.resizable(0, 0)
  522.     def tk_set_window_no_close(root):
  523.         if sys.platform == 'win32':
  524.             root.protocol('WM_DELETE_WINDOW', root.quit)
  525.         else:
  526.             root.protocol('WM_DELETE_WINDOW', None)
  527.     def tk_set_window_no_border(root):
  528.         root.overrideredirect(1)
  529.     def tk_set_window_no_minimize(root):
  530.         root.attributes('-toolwindow', 1)
  531.     def tk_set_window_no_maximize(root):
  532.         root.attributes('-toolwindow', 1)
  533.     def tk_set_window_no_taskbar(root):
  534.         root.attributes('-toolwindow', 1)
  535.     def tk_set_window_taskbar_icon(root, icon_path):
  536.         if sys.platform == 'win32':
  537.             root.iconbitmap(icon_path)
  538.         else:
  539.             img = tk.PhotoImage(file=icon_path)
  540.             root.tk.call('wm', 'iconphoto', root._w, img)
  541.     def tk_set_window_taskbar_title(root, title):
  542.         root.title(title)
  543.     def tk_set_window_taskbar_progress(root, percent):
  544.         root.attributes('-alpha', percent)
  545.     def tk_set_window_taskbar_overlay_icon(root, icon_path):
  546.         root.attributes('-alpha', percent)
  547.     def tk_set_window_taskbar_flash(root):
  548.         root.attributes('-alpha', percent)
  549.     def tk_get_window_position(root):
  550.         return root.winfo_x(), root.winfo_y()
  551.     def tk_set_window_position(root, x, y):
  552.         root.geometry('+{}+{}'.format(x, y))
  553.     def tk_set_window_size(root, width, height):
  554.         root.geometry('{}x{}'.format(width, height))
  555.     def tk_get_window_size(root):
  556.         return root.winfo_width(), root.winfo_height()
  557.     def tk_set_window_minimum_size(root, width, height):
  558.         root.minsize(width, height)
  559.     def tk_set_window_maximum_size(root, width, height):
  560.         root.maxsize(width, height)
  561.     def tk_set_clipboard_text(text):
  562.         root = tk.Tk()
  563.         root.withdraw()
  564.         root.clipboard_clear()
  565.         root.clipboard_append(text)
  566.         root.destroy()
  567.     def tk_get_clipboard_text():
  568.         root = tk.Tk()
  569.         root.withdraw()
  570.         result = root.selection_get(selection='CLIPBOARD')
  571.         root.destroy()
  572.         return result
  573.     def tk_create_tooltip(widget, text, delay=1.0):
  574.         tooltip = tooltip_class(widget)
  575.         tooltip.set_text(text)
  576.         tooltip.set_delay(delay)
  577.        
  578. class tooltip_class:
  579.     def __init__(self, widget):
  580.         self.widget = widget
  581.         self.tipwindow = None
  582.         self.id = None
  583.         self.x = self.y = 0
  584.         self.delay = 0.5
  585.         self.wrap = 100
  586.         self.text = None
  587.         self.widget.bind('<Enter>', self.enter)
  588.         self.widget.bind('<Leave>', self.leave)
  589.         self.widget.bind('<ButtonPress>', self.leave)
  590.     def enter(self, event=None):
  591.         self.schedule()
  592.     def leave(self, event=None):
  593.         self.unschedule()
  594.         self.hidetip()
  595.     def schedule(self):
  596.         self.unschedule()
  597.         self.id = self.widget.after(int(self.delay * 1000), self.showtip)
  598.     def unschedule(self):
  599.         id = self.id
  600.         self.id = None
  601.         if id:
  602.             self.widget.after_cancel(id)
  603.     def showtip(self, event=None):
  604.         if self.tipwindow:
  605.             return
  606.         x = self.widget.winfo_rootx() + self.widget.winfo_pointerx()
  607.         y = self.widget.winfo_rooty() + self.widget.winfo_pointery() + 20
  608.         self.tipwindow = tw = tk.Toplevel(self.widget)
  609.         tw.wm_overrideredirect(1)
  610.         tw.wm_geometry('+%d+%d' % (x, y))
  611.         label = tk.Label(tw, text=self.text, justify=tk.LEFT, background='#ffffe0', relief='solid', borderwidth=1,
  612.                          font=('tahoma', '8', 'normal'))
  613.         label.pack(ipadx=1)
  614.     def hidetip(self):
  615.         tw = self.tipwindow
  616.         self.tipwindow = None
  617.         if tw:
  618.             tw.destroy()
  619.     def set_text(self, text):
  620.         self.text = text
  621.     def set_delay(self, delay):
  622.         self.delay = delay
  623.     def tk_create_dialog(root, title, text, buttons):
  624.         if not isinstance(buttons, (tuple, list)):
  625.             raise Exception('Buttons must be tuple or list.')
  626.         for button in buttons:
  627.             if not isinstance(button, (tuple, list)):
  628.                 raise Exception('Buttons must contain tuple or list.')
  629.             if len(button) != 2:
  630.                 raise Exception('Button must contain 2 elements: text and command.')
  631.         dialog = tk.Toplevel(root)
  632.         tk_set_window_title(dialog, title)
  633.         tk_set_window_position(dialog, int(root.winfo_screenwidth() / 2 - 150), int(root.winfo_screenheight() / 2 - 75))
  634.         tk_set_window_size(dialog, 300, 150)
  635.         tk_set_window_no_close(dialog)
  636.         #tk_set_window_no_resize(dialog)
  637.         #tk_set_window_no_border(dialog)
  638.         tk_set_window_taskbar_icon(dialog, PROJECT_DIR + '/img/icon.ico')
  639.         frame_text = tk.Frame(dialog)
  640.         frame_text.pack(fill='both', expand=1, padx=5, pady=5)
  641.         label_text = tk.Label(frame_text, text=text, justify='left')
  642.         label_text.pack(fill='both', expand=1)
  643.         frame_buttons = tk.Frame(dialog)
  644.         frame_buttons.pack(fill='x', expand=0, padx=5, pady=5)
  645.         btn_default = None
  646.         for button in buttons:
  647.             if btn_default is None:
  648.                 btn_default = tk.Button(frame_buttons, text=button[0], command=button[1])
  649.                 btn_default.pack(side='left', padx=5, pady=5)
  650.             else:
  651.                 tk.Button(frame_buttons, text=button[0], command=button[1]).pack(side='left', padx=5, pady=5)
  652.         tk_center_window(dialog, 300, 150)
  653.         dialog.focus_set()
  654.         dialog.grab_set()
  655.         btn_default.focus_set()
  656.         return dialog
  657.     def tk_create_message_box(root, title, text, buttons):
  658.         if sys.platform == 'win32':
  659.             import ctypes
  660.             ctypes.windll.user32.MessageBoxW(None, text, title, buttons)
  661.             return
  662.         else:
  663.             if not isinstance(buttons, (tuple, list)):
  664.                 raise Exception('Buttons must be tuple or list.')
  665.             for button in buttons:
  666.                 if not isinstance(button, (tuple, list)):
  667.                     raise Exception('Buttons must contain tuple or list.')
  668.                 if len(button) != 2:
  669.                     raise Exception('Button must contain 2 elements: text and command.')
  670.             dialog = tk.Toplevel(root)
  671.             tk_set_window_title(dialog, title)
  672.             tk_set_window_position(dialog, int(root.winfo_screenwidth() / 2 - 150), int(root.winfo_screenheight() / 2 - 75))
  673.             tk_set_window_size(dialog, 300, 150)
  674.             tk_set_window_no_close(dialog)
  675.             tk_set_window_no_resize(dialog)
  676.             tk_set_window_no_border(dialog)
  677.             tk_set_window_taskbar_icon(dialog, PROJECT_DIR + '/img/icon.ico')
  678.             frame_text = tk
  679.  
  680. def tk_create_message_box(root, title, text, buttons):
  681.     if sys.platform == 'win32':
  682.         import ctypes
  683.         ctypes.windll.user32.MessageBoxW(None, text, title, buttons)
  684.         return
  685.     else:
  686.         if not isinstance(buttons, (tuple, list)):
  687.             raise Exception('Buttons must be tuple or list.')
  688.         for button in buttons:
  689.             if not isinstance(button, (tuple, list)):
  690.                 raise Exception('Buttons must contain tuple or list.')
  691.             if len(button) != 2:
  692.                 raise Exception('Button must contain 2 elements: text and command.')
  693.         dialog = tk.Toplevel(root)
  694.         tk_set_window_title(dialog, title)
  695.         tk_set_window_position(dialog, int(root.winfo_screenwidth() / 2 - 150), int(root.winfo_screenheight() / 2 - 75))
  696.         tk_set_window_size(dialog, 300, 150)
  697.         tk_set_window_no_close(dialog)
  698.         tk_set_window_no_resize(dialog)
  699.         tk_set_window_no_border(dialog)
  700.         tk_set_window_taskbar_icon(dialog, PROJECT_DIR + '/img/icon.ico')
  701.         frame_text = tk.Frame(dialog)
  702.         tk.Label(frame_text, text=text, justify=tk.CENTER).pack(fill=tk.X)
  703.         frame_text.pack(fill=tk.X, padx=15, pady=15)
  704.         frame_buttons = tk.Frame(dialog)
  705.         for i in range(len(buttons)):
  706.             if i == 0:
  707.                 tk.Button(frame_buttons, text=buttons[i][0], command=buttons[i][1]).pack(side=tk.LEFT, padx=5, pady=5)
  708.             else:
  709.                 tk.Button(frame_buttons, text=buttons[i][0], command=buttons[i][1]).pack(side=tk.LEFT, padx=5, pady=5)
  710.         frame_buttons.pack(fill=tk.X, padx=15, pady=15)
  711.         tk_set_window_focus(dialog)
  712.         dialog.wait_window()
  713.  
  714.     def tk_create_message_box_ok(root, title, text):
  715.         tk_create_message_box(root, title, text, (('OK', root.destroy),))
  716.  
  717.     def tk_create_message_box_ok_cancel(root, title, text):
  718.         tk_create_message_box(root, title, text, (('OK', root.destroy), ('Cancel', lambda: root.quit())))
  719.  
  720.     def tk_create_message_box_yes_no(root, title, text):
  721.         tk_create_message_box(root, title, text, (('Yes', root.destroy), ('No', lambda: root.quit())))
  722.  
  723.     def tk_create_message_box_yes_no_cancel(root, title, text):
  724.         tk_create_message_box(root, title, text, (('Yes', root.destroy), ('No', lambda: root.quit()), ('Cancel', lambda: root.quit())))
  725.  
  726.     def tk_create_message_box_abort_retry_ignore(root, title, text):
  727.         tk_create_message_box(root, title, text, (('Abort', root.destroy), ('Retry', root.destroy), ('Ignore', root.destroy)))
  728.  
  729.     def tk_create_message_box_abort_retry_continue(root, title, text):
  730.         tk_create_message_box(root, title, text, (('Abort', root.destroy), ('Retry', root.destroy), ('Continue', root.destroy)))
  731.  
  732.     def tk_create_message_box_yes_all_no_all(root, title, text):
  733.         tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('No All', root.destroy)))
  734.  
  735.     def tk_create_message_box_yes_all_no_all_cancel(root, title, text):
  736.         tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('No All', root.destroy), ('Cancel', lambda: root.quit())))
  737.  
  738.     def tk_create_message_box_yes_all_no_all_continue(root, title, text):
  739.         tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('No All', root.destroy), ('Continue', root.destroy)))
  740.  
  741.     def tk_create_message_box_yes_all_no_all_cancel_continue(root, title, text):
  742.         tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('No All', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  743.  
  744.     def tk_create_message_box_no_all_cancel(root, title, text):
  745.         tk_create_message_box(root, title, text, (('No All', root.destroy), ('Cancel', lambda: root.quit())))
  746.  
  747.     def tk_create_message_box_no_all_cancel_continue(root, title, text):
  748.         tk_create_message_box(root, title, text, (('No All', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  749.  
  750.     def tk_create_message_box_yes_all_cancel(root, title, text):
  751.         tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('Cancel', lambda: root.quit())))
  752.  
  753.     def tk_create_message_box_yes_all_continue(root, title, text):
  754.         tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('Continue', root.destroy)))
  755.  
  756.     def tk_create_message_box_yes_all_cancel_continue(root, title, text):
  757.         tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  758.  
  759.     def tk_create_message_box_no_all_continue(root, title, text):
  760.         tk_create_message_box(root, title, text, (('No All', root.destroy), ('Continue', root.destroy)))
  761.  
  762.     def tk_create_message_box_yes_cancel(root, title, text):
  763.         tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Cancel', lambda: root.quit())))
  764.  
  765.     def tk_create_message_box_yes_continue(root, title, text):
  766.         tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Continue', root.destroy)))
  767.  
  768.     def tk_create_message_box_yes_cancel_continue(root, title, text):
  769.         tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  770.  
  771.     def tk_create_message_box_no_continue(root, title, text):
  772.         tk_create_message_box(root, title, text, (('No', root.destroy), ('Continue', root.destroy)))
  773.  
  774.     def tk_create_message_box_no_cancel(root, title, text):
  775.         tk_create_message_box(root, title, text, (('No', root.destroy), ('Cancel', lambda: root.quit())))
  776.  
  777.     def tk_create_message_box_no_cancel_continue(root, title, text):
  778.         tk_create_message_box(root, title, text, (('No', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  779.  
  780.     def tk_create_message_box_continue(root, title, text):
  781.         tk_create_message_box(root, title, text, (('Continue', root.destroy),))
  782.  
  783.     def tk_create_message_box_cancel(root, title, text):
  784.         tk_create_message_box(root, title, text, (('Cancel', lambda: root.quit()),))
  785.  
  786.     def tk_create_message_box_cancel_continue(root, title, text):
  787.         tk_create_message_box(root, title, text, (('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  788.  
  789.     def tk_create_message_box_yes_no_continue(root, title, text):
  790.         tk_create_message_box(root, title, text, (('Yes', root.destroy), ('No', lambda: root.quit()), ('Continue', root.destroy)))
  791.  
  792.     def tk_create_message_box_yes_no_cancel_continue(root, title, text):
  793.         tk_create_message_box(root, title, text, (('Yes', root.destroy), ('No', lambda: root.quit()), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  794.  
  795.     def tk_create_message_box_yes(root, title, text):
  796.         tk_create_message_box(root, title, text, (('Yes', root.destroy),))
  797.  
  798.     def tk_create_message_box_no(root, title, text):
  799.         tk_create_message_box(root, title, text, (('No', lambda: root.quit()),))
  800.  
  801.     def tk_create_message_box_yes_continue(root, title, text):
  802.         tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Continue', root.destroy)))
  803.  
  804.     def tk_create_message_box_yes_cancel_continue(root, title, text):
  805.         tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  806.  
  807.     def tk_create_message_box_no_continue(root, title, text):
  808.         tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Continue', root.destroy)))
  809.  
  810.     def tk_create_message_box_no_cancel(root, title, text):
  811.         tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Cancel', lambda: root.quit())))
  812.  
  813.     def tk_create_message_box_no_cancel_continue(root, title, text):
  814.         tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  815.  
  816.     def tk_create_message_box_no_yes(root, title, text):
  817.         tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Yes', root.destroy)))
  818.  
  819.     def tk_create_message_box_no_yes_cancel(root, title, text):
  820.         tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Yes', root.destroy), ('Cancel', lambda: root.quit())))
  821.  
  822.     def tk_create_message_box_no_yes_cancel_continue(root, title, text):
  823.         tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Yes', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  824.  
  825.     def tk_create_message_box_ok_cancel(root, title, text):
  826.         tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Cancel', lambda: root.quit())))
  827.  
  828.     def tk_create_message_box_ok_cancel_continue(root, title, text):
  829.         tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  830.  
  831.     def tk_create_message_box_ok_continue(root, title, text):
  832.         tk_create_message_box(root, title, text, (('OK', root.destroy), ('Continue', root.destroy)))
  833.  
  834.     def tk_create_message_box_ok_no_continue(root, title, text):
  835.         tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('No', lambda: root.quit()), ('Continue', root.destroy)))
  836.  
  837.     def tk_create_message_box_ok_no_yes_cancel_continue(root, title, text):
  838.         tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('No', lambda: root.quit()), ('Yes', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  839.  
  840.     def tk_create_message_box_ok_yes_cancel(root, title, text):
  841.         tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Yes', root.destroy), ('Cancel', lambda: root.quit())))
  842.  
  843.     def tk_create_message_box_ok_yes_cancel_continue(root, title, text):
  844.         tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Yes', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
  845.  
  846.     def tk_create_message_box_ok_cancel(root, title, text):
  847.         tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Cancel', lambda: root.quit())))
  848.  
Advertisement
RAW Paste Data Copied
Advertisement