Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python3_to_HTML5.py
- import sys, os
- import os.path
- import pickle
- import re
- import time
- import shutil
- import random
- import subprocess
- import urllib.request
- import urllib.error
- import urllib.parse
- import tkinter as tk
- import tkinter.font
- import tkinter.ttk
- import tkinter.scrolledtext
- import tkinter.filedialog
- try:
- from PIL import Image
- from PIL import ImageTk
- except:
- raise Exception('PIL is not installed.')
- # consts
- PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
- # classes
- class MyTk(tk.Tk):
- def __init__(self):
- super().__init__()
- self.update()
- self.width = self.winfo_width()
- self.height = self.winfo_height()
- self.x = self.winfo_screenwidth() // 2 - self.width // 2
- self.y = self.winfo_screenheight() // 2 - self.height // 2
- self.geometry('+{}+{}'.format(self.x, self.y))
- self.tk_setPalette(background='#ececec')
- self.protocol('WM_DELETE_WINDOW', self.quit)
- class App(tk.Frame):
- def __init__(self, master=None):
- super().__init__(master)
- #self.pack(fill=tk.BOTH, expand=1)
- self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
- self.master.title('Python 3 to HTML5')
- self.master.bind('<Configure>', self._configure_window)
- self.master.bind('<Escape>', self.quit)
- self.create_widgets()
- def _configure_window(self, event):
- if event.width != self.master.winfo_width():
- self.master.grid_columnconfigure(0, weight=1)
- if event.height != self.master.winfo_height():
- self.master.grid_rowconfigure(0, weight=1)
- def quit(self, event=None):
- self.master.destroy()
- self.master.quit()
- def create_widgets(self):
- # title
- self.title = tk.Label(self, text='Python 3 to HTML5', font=tk.font.Font(family='Helvetica', size=14, weight='bold'))
- self.title.grid(row=0, column=0, columnspan=2, padx=10, pady=10, sticky=tk.N)
- # input
- self.input_label = tk.Label(self, text='Input')
- self.input_label.grid(row=1, column=0, padx=10, pady=10, sticky=tk.W)
- self.input_entry = tk.Entry(self, width=100)
- self.input_entry.grid(row=1, column=1, padx=10, sticky=tk.E+tk.W)
- self.input_button = tk.Button(self, text='Browse', command=self.browse_input)
- self.input_button.grid(row=1, column=2, padx=10, pady=10, sticky=tk.E)
- # output
- self.output_label = tk.Label(self, text='Output')
- self.output_label.grid(row=2, column=0, padx=10, pady=10, sticky=tk.W)
- self.output_entry = tk.Entry(self, width=100)
- self.output_entry.grid(row=2, column=1, padx=10, sticky=tk.E+tk.W)
- self.output_button = tk.Button(self, text='Browse', command=self.browse_output)
- self.output_button.grid(row=2, column=2, padx=10, pady=10, sticky=tk.E)
- # run
- self.run_button = tk.Button(self, text='Run', command=self.run, width=100)
- self.run_button.grid(row=3, column=0, columnspan=3, padx=10, pady=10, sticky=tk.E+tk.W)
- # description
- self.description_label = tk.Label(self, text='Description')
- self.description_label.grid(row=4, column=0, padx=10, pady=10, sticky=tk.W)
- self.description_text = tkinter.scrolledtext.ScrolledText(self)
- self.description_text.grid(row=4, column=1, padx=10, sticky=tk.E+tk.W)
- # output console
- self.output_label = tk.Label(self, text='Output console')
- self.output_label.grid(row=5, column=0, padx=10, pady=10, sticky=tk.W)
- self.output_text = tkinter.scrolledtext.ScrolledText(self)
- self.output_text.grid(row=5, column=1, padx=10, sticky=tk.E+tk.W)
- # status
- self.status_label = tk.Label(self, text='Status')
- self.status_label.grid(row=6, column=0, padx=10, pady=10, sticky=tk.W)
- self.status_text = tkinter.scrolledtext.ScrolledText(self)
- self.status_text.grid(row=6, column=1, padx=10, sticky=tk.E+tk.W)
- # settings
- self.settings_button = tk.Button(self, text='Settings', command=self.settings)
- self.settings_button.grid(row=7, column=0, padx=10, pady=10, sticky=tk.E+tk.W)
- # about
- self.about_button = tk.Button(self, text='About', command=self.about)
- self.about_button.grid(row=7, column=1, padx=10, pady=10, sticky=tk.E+tk.W)
- # quit
- self.quit_button = tk.Button(self, text='Quit', command=self.quit, width=100)
- self.quit_button.grid(row=8, column=0, columnspan=3, padx=10, pady=10, sticky=tk.E+tk.W)
- self.grid_rowconfigure(0, weight=0)
- self.grid_rowconfigure(1, weight=0)
- self.grid_rowconfigure(2, weight=0)
- self.grid_rowconfigure(3, weight=0)
- self.grid_rowconfigure(4, weight=1)
- self.grid_rowconfigure(5, weight=1)
- self.grid_rowconfigure(6, weight=1)
- self.grid_rowconfigure(7, weight=0)
- self.grid_rowconfigure(8, weight=0)
- self.grid_columnconfigure(0, weight=1)
- self.grid_columnconfigure(1, weight=1)
- self.grid_columnconfigure(2, weight=0)
- self.update()
- self.width = self.winfo_width()
- self.height = self.winfo_height()
- self.x = self.master.winfo_screenwidth() // 2 - self.width // 2
- self.y = self.master.winfo_screenheight() // 2 - self.height // 2
- self.master.geometry('+{}+{}'.format(self.x, self.y))
- self.master.grid_columnconfigure(0, weight=1)
- self.master.grid_rowconfigure(0, weight=1)
- def browse_input(self):
- path = tk.filedialog.askdirectory()
- if path:
- self.input_entry.insert(tk.END, path)
- def browse_output(self):
- path = tk.filedialog.askdirectory()
- if path:
- self.output_entry.insert(tk.END, path)
- def about(self):
- about = """
- Python 3 to HTML5
- Version 1.0.0
- Author: Kirk Lawrence
- """
- messagebox.showinfo('About', about)
- def settings(self):
- self.settings = Settings(self)
- self.wait_window(self.settings)
- def run(self):
- input = self.input_entry.get()
- output = self.output_entry.get()
- if not input or not output:
- messagebox.showerror('Error', 'Input and output must be specified')
- return
- if not os.path.exists(input):
- messagebox.showerror('Error', 'Input directory doesn't exist')
- return
- if not os.path.exists(output):
- messagebox.showerror('Error', 'Output directory doesn't exist')
- return
- if not os.path.isdir(output):
- messagebox.showerror('Error', 'Output must be a directory')
- return
- self.clear_output()
- self.clear_status()
- self.write_output('Processing...')
- self.master.update()
- input = os.path.abspath(input)
- output = os.path.abspath(output)
- self.write_output('Input directory: {}'.format(input))
- self.write_output('Output directory: {}'.format(output))
- self.write_output()
- start_time = time.time()
- self.process_dir(input, output)
- end_time = time.time()
- self.write_output()
- self.write_output('Total time: {}'.format(self.get_time(end_time - start_time)))
- self.write_output('Done.')
- self.write_status('Done.')
- messagebox.showinfo('Done', 'Done.')
- def process_dir(self, input, output):
- if not os.path.exists(output):
- os.makedirs(output)
- for name in os.listdir(input):
- path = os.path.join(input, name)
- if not os.path.isdir(path):
- if not self.is_python_file(name):
- continue
- self.write_output('Found Python file: {}'.format(path))
- self.write_output()
- self.process_file(path, output)
- self.write_output()
- else:
- new_output = os.path.join(output, name)
- self.write_output('Entering directory: {}'.format(path))
- self.write_output()
- self.process_dir(path, new_output)
- self.write_output()
- self.write_output('Exiting directory: {}'.format(path))
- def process_file(self, path, output):
- # open file
- self.write_output('Opening file: {}'.format(path))
- with open(path, 'r') as f:
- data = f.read()
- # write to output directory
- name, ext = os.path.splitext(path)
- ext = ext.lower()
- name = os.path.basename(name)
- if not ext:
- ext = '.html'
- output_path = os.path.join(output, name + ext)
- self.write_output('Writing to: {}'.format(output_path))
- with open(output_path, 'w') as f:
- f.write(self.convert(data))
- # run
- self.write_output()
- self.write_output('Running...')
- self.run_file(output_path)
- def convert(self, data):
- # convert
- self.write_output('Converting...')
- description = '
- '.join(self.get_description(data))
- self.write_description(description)
- return self.run_conversion(data)
- def run_conversion(self, data):
- return data
- def run_file(self, path):
- pass
- def get_description(self, data):
- lines = []
- for line in data.splitlines():
- line = line.strip()
- if line.startswith('#'):
- lines.append(line)
- else:
- break
- return lines
- def is_python_file(self, name):
- return name.endswith('.py')
- def get_time(self, seconds):
- m, s = divmod(seconds, 60)
- h, m = divmod(m, 60)
- return '%02d:%02d:%02d' % (h, m, s)
- def write_output(self, text=''):
- self.output_text.insert(tk.END, text + '
- ')
- self.output_text.see('end')
- self.master.update()
- def write_description(self, text=''):
- self.description_text.insert(tk.END, text + '
- ')
- self.description_text.see('end')
- self.master.update()
- def write_status(self, text=''):
- self.status_text.insert(tk.END, text + '
- ')
- self.status_text.see('end')
- self.master.update()
- def clear_output(self):
- self.output_text.delete('1.0', tk.END)
- self.master.update()
- def clear_description(self):
- self.description_text.delete('1.0', tk.END)
- self.master.update()
- def clear_status(self):
- self.status_text.delete('1.0', tk.END)
- self.master.update()
- class Settings(tk.Toplevel):
- def __init__(self, master=None):
- super().__init__(master)
- self.master = master
- self.title('Settings')
- self.protocol('WM_DELETE_WINDOW', self.quit)
- self.create_widgets()
- def quit(self):
- self.destroy()
- def create_widgets(self):
- self.grid_rowconfigure(0, weight=0)
- self.grid_columnconfigure(0, weight=1)
- self.grid_columnconfigure(1, weight=1)
- # buttons
- self.button_frame = tk.Frame(self)
- self.button_frame.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky=tk.E+tk.W)
- self.grid_rowconfigure(1, weight=0)
- self.quit_button = tk.Button(self.button_frame, text="Quit", command=self.quit)
- self.quit_button.grid(row=0, column=0, padx=5, pady=5)
- self.start_button = tk.Button(self.button_frame, text="Start", command=self.start)
- self.start_button.grid(row=0, column=1, padx=5, pady=5)
- self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop)
- self.stop_button.grid(row=0, column=2, padx=5, pady=5)
- # canvas
- self.canvas = tk.Canvas(self, width=200, height=200, bg="white")
- self.canvas.grid(row=0, column=0, columnspan=2, padx=10, pady=10)
- # self.canvas.bind("<B1-Motion>", self.paint)
- # mouse tracker
- self.mouse_tracker = tk.Label(self, text="Mouse position: ")
- self.mouse_tracker.grid(row=2, column=0, sticky=tk.W, padx=10, pady=10)
- # mouse tracker
- self.mouse_pos_label = tk.Label(self, text="")
- self.mouse_pos_label.grid(row=2, column=1, sticky=tk.E, padx=10, pady=10)
- def paint(self, event):
- x1, y1 = (event.x - 1), (event.y - 1)
- x2, y2 = (event.x + 1), (event.y + 1)
- self.canvas.create_oval(x1, y1, x2, y2, fill="black")
- def mouse_pos_update(self, event):
- self.mouse_pos_label["text"] = "("+str(event.x)+","+str(event.y)+")"
- class Py3_to_HTML5:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('html+javascript')
- self._pygments = pygments.lexers.get_lexer_by_name('python3')
- self._source = source
- self._options = options
- def get_syntax(self):
- return '<pre>' + pygments.highlight(self._source, self._pygments,
- pygments.formatters.HtmlFormatter()) + '</pre>'
- def get_syntax_with_linenos(self):
- return '<pre>' + pygments.highlight(self._source, self._pygments,
- pygments.formatters.HtmlFormatter(linenos='table')) + '</pre>'
- def get_syntax_with_linenos_and_options(self, options):
- return '<pre>' + pygments.highlight(self._source, self._pygments,
- pygments.formatters.HtmlFormatter(linenos=options)) + '</pre>'
- class HTML5Highlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('html+javascript')
- self._source = source
- self._options = options
- class ASMHighlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('x86asm')
- self._source = source
- self._options = options
- class ShellHighlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('console')
- self._source = source
- self._options = options
- class Z80Highlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('z80')
- self._source = source
- self._options = options
- class Asm8085Highlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('asm')
- self._source = source
- self._options = options
- class Asm8086Highlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('x86asm')
- self._source = source
- self._options = options
- class Asm8051Highlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('asm')
- self._source = source
- self._options = options
- class Asm8039Highlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('asm')
- self._source = source
- self._options = options
- class Asm6809Highlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('asm')
- self._source = source
- self._options = options
- class Asm6510Highlighter:
- def __init__(self, source, options=''):
- self._pygments = pygments.lexers.get_lexer_by_name('asm')
- self._source = source
- self._options = options
- class write_py3_to_js:
- def __init__(self, value):
- self.value = value
- self.encode('utf-8')
- def __str__(self):
- return self.value
- def encode(self, enc):
- self.value = self.value.encode(enc)
- def decode(self, enc):
- self.value = self.value.decode(enc)
- def tk_menu_item(menu, text, command):
- menu.add_command(label=text, command=command)
- def tk_menu_separator(menu):
- menu.add_separator()
- def tk_menu_item_enable(menu, text, command, enable):
- menu.add_command(label=text, command=command, state=('disabled' if not enable else 'normal'))
- def tk_menu_item_check(menu, text, command, var, value):
- menu.add_checkbutton(label=text, command=command, variable=var, onvalue=value)
- def tk_menu_item_radio(menu, text, command, var, value):
- menu.add_radiobutton(label=text, command=command, variable=var, value=value)
- def tk_menu_item_radio_get(menu):
- for item in menu.children.values():
- if item.type == 'radiobutton':
- if item.select():
- return item
- return None
- def tk_menu_item_radio_get_value(menu):
- for item in menu.children.values():
- if item.type == 'radiobutton':
- if item.select():
- return item.variable.get()
- return None
- def tk_menu_item_radio_get_text(menu):
- for item in menu.children.values():
- if item.type == 'radiobutton':
- if item.select():
- return item['text']
- return None
- def tk_menu_item_radio_get_index(menu):
- index = 0
- for item in menu.children.values():
- if item.type == 'radiobutton':
- if item.select():
- return index
- index += 1
- return None
- def tk_menu_item_radio_select(menu, text):
- for item in menu.children.values():
- if item.type == 'radiobutton':
- if item['text'] == text:
- item.select()
- def tk_menu_item_radio_select_index(menu, index):
- for item in menu.children.values():
- if item.type == 'radiobutton':
- if index == 0:
- item.select()
- return
- index -= 1
- def tk_menu_item_radio_get_all_text(menu):
- result = []
- for item in menu.children.values():
- if item.type == 'radiobutton':
- result.append(item['text'])
- return result
- def tk_menu_item_radio_get_all_value(menu):
- result = []
- for item in menu.children.values():
- if item.type == 'radiobutton':
- result.append(item.variable.get())
- return result
- def tk_menu_item_radio_get_all_index_value(menu):
- result = []
- index = 0
- for item in menu.children.values():
- if item.type == 'radiobutton':
- result.append((index, item.variable.get()))
- index += 1
- return result
- def tk_menu_item_radio_get_all_index_text(menu):
- result = []
- index = 0
- for item in menu.children.values():
- if item.type == 'radiobutton':
- result.append((index, item['text']))
- index += 1
- return result
- def tk_popup_menu(root, x, y, items):
- popup = tk.Menu(root, tearoff=0)
- popup.post(x, y)
- for item in items:
- if item[0] == '-':
- tk_menu_separator(popup)
- elif isinstance(item, tuple):
- tk_menu_item(popup, item[0], item[1])
- elif isinstance(item, list):
- tk_menu_item_enable(popup, item[0], item[1], item[2])
- elif isinstance(item, dict):
- tk_menu_item_check(popup, item[0], item[1], item[2], item[3])
- else:
- raise Exception('Unsupported item in popup menu: {}'.format(item))
- def tk_get_screen_size():
- root = tk.Tk()
- root.withdraw()
- x = root.winfo_screenwidth()
- y = root.winfo_screenheight()
- root.destroy()
- return x, y
- def tk_get_default_font():
- root = tk.Tk()
- root.withdraw()
- font = tk.font.nametofont('TkDefaultFont')
- font.configure(size=10)
- root.destroy()
- return font
- def tk_center_window(root, width, height):
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width // 2) - (width // 2)
- y = (screen_height // 2) - (height // 2)
- root.geometry('{}x{}+{}+{}'.format(width, height, x, y))
- def tk_set_window_icon(root, icon_path):
- if sys.platform == 'win32':
- root.iconbitmap(icon_path)
- else:
- img = tk.PhotoImage(file=icon_path)
- root.tk.call('wm', 'iconphoto', root._w, img)
- def tk_set_window_title(root, title):
- root.title(title)
- def tk_set_window_transparent(root, percent):
- percent = int(percent)
- if percent < 0:
- percent = 0
- if percent > 100:
- percent = 100
- alpha = percent * 65535 // 100
- root.attributes('-alpha', alpha)
- def tk_set_window_always_on_top(root, ontop):
- if ontop:
- root.attributes('-topmost', True)
- else:
- root.attributes('-topmost', False)
- def tk_set_window_no_resize(root):
- root.resizable(0, 0)
- def tk_set_window_no_close(root):
- if sys.platform == 'win32':
- root.protocol('WM_DELETE_WINDOW', root.quit)
- else:
- root.protocol('WM_DELETE_WINDOW', None)
- def tk_set_window_no_border(root):
- root.overrideredirect(1)
- def tk_set_window_no_minimize(root):
- root.attributes('-toolwindow', 1)
- def tk_set_window_no_maximize(root):
- root.attributes('-toolwindow', 1)
- def tk_set_window_no_taskbar(root):
- root.attributes('-toolwindow', 1)
- def tk_set_window_taskbar_icon(root, icon_path):
- if sys.platform == 'win32':
- root.iconbitmap(icon_path)
- else:
- img = tk.PhotoImage(file=icon_path)
- root.tk.call('wm', 'iconphoto', root._w, img)
- def tk_set_window_taskbar_title(root, title):
- root.title(title)
- def tk_set_window_taskbar_progress(root, percent):
- root.attributes('-alpha', percent)
- def tk_set_window_taskbar_overlay_icon(root, icon_path):
- root.attributes('-alpha', percent)
- def tk_set_window_taskbar_flash(root):
- root.attributes('-alpha', percent)
- def tk_get_window_position(root):
- return root.winfo_x(), root.winfo_y()
- def tk_set_window_position(root, x, y):
- root.geometry('+{}+{}'.format(x, y))
- def tk_set_window_size(root, width, height):
- root.geometry('{}x{}'.format(width, height))
- def tk_get_window_size(root):
- return root.winfo_width(), root.winfo_height()
- def tk_set_window_minimum_size(root, width, height):
- root.minsize(width, height)
- def tk_set_window_maximum_size(root, width, height):
- root.maxsize(width, height)
- def tk_set_clipboard_text(text):
- root = tk.Tk()
- root.withdraw()
- root.clipboard_clear()
- root.clipboard_append(text)
- root.destroy()
- def tk_get_clipboard_text():
- root = tk.Tk()
- root.withdraw()
- result = root.selection_get(selection='CLIPBOARD')
- root.destroy()
- return result
- def tk_create_tooltip(widget, text, delay=1.0):
- tooltip = tooltip_class(widget)
- tooltip.set_text(text)
- tooltip.set_delay(delay)
- class tooltip_class:
- def __init__(self, widget):
- self.widget = widget
- self.tipwindow = None
- self.id = None
- self.x = self.y = 0
- self.delay = 0.5
- self.wrap = 100
- self.text = None
- self.widget.bind('<Enter>', self.enter)
- self.widget.bind('<Leave>', self.leave)
- self.widget.bind('<ButtonPress>', self.leave)
- def enter(self, event=None):
- self.schedule()
- def leave(self, event=None):
- self.unschedule()
- self.hidetip()
- def schedule(self):
- self.unschedule()
- self.id = self.widget.after(int(self.delay * 1000), self.showtip)
- def unschedule(self):
- id = self.id
- self.id = None
- if id:
- self.widget.after_cancel(id)
- def showtip(self, event=None):
- if self.tipwindow:
- return
- x = self.widget.winfo_rootx() + self.widget.winfo_pointerx()
- y = self.widget.winfo_rooty() + self.widget.winfo_pointery() + 20
- self.tipwindow = tw = tk.Toplevel(self.widget)
- tw.wm_overrideredirect(1)
- tw.wm_geometry('+%d+%d' % (x, y))
- label = tk.Label(tw, text=self.text, justify=tk.LEFT, background='#ffffe0', relief='solid', borderwidth=1,
- font=('tahoma', '8', 'normal'))
- label.pack(ipadx=1)
- def hidetip(self):
- tw = self.tipwindow
- self.tipwindow = None
- if tw:
- tw.destroy()
- def set_text(self, text):
- self.text = text
- def set_delay(self, delay):
- self.delay = delay
- def tk_create_dialog(root, title, text, buttons):
- if not isinstance(buttons, (tuple, list)):
- raise Exception('Buttons must be tuple or list.')
- for button in buttons:
- if not isinstance(button, (tuple, list)):
- raise Exception('Buttons must contain tuple or list.')
- if len(button) != 2:
- raise Exception('Button must contain 2 elements: text and command.')
- dialog = tk.Toplevel(root)
- tk_set_window_title(dialog, title)
- tk_set_window_position(dialog, int(root.winfo_screenwidth() / 2 - 150), int(root.winfo_screenheight() / 2 - 75))
- tk_set_window_size(dialog, 300, 150)
- tk_set_window_no_close(dialog)
- #tk_set_window_no_resize(dialog)
- #tk_set_window_no_border(dialog)
- tk_set_window_taskbar_icon(dialog, PROJECT_DIR + '/img/icon.ico')
- frame_text = tk.Frame(dialog)
- frame_text.pack(fill='both', expand=1, padx=5, pady=5)
- label_text = tk.Label(frame_text, text=text, justify='left')
- label_text.pack(fill='both', expand=1)
- frame_buttons = tk.Frame(dialog)
- frame_buttons.pack(fill='x', expand=0, padx=5, pady=5)
- btn_default = None
- for button in buttons:
- if btn_default is None:
- btn_default = tk.Button(frame_buttons, text=button[0], command=button[1])
- btn_default.pack(side='left', padx=5, pady=5)
- else:
- tk.Button(frame_buttons, text=button[0], command=button[1]).pack(side='left', padx=5, pady=5)
- tk_center_window(dialog, 300, 150)
- dialog.focus_set()
- dialog.grab_set()
- btn_default.focus_set()
- return dialog
- def tk_create_message_box(root, title, text, buttons):
- if sys.platform == 'win32':
- import ctypes
- ctypes.windll.user32.MessageBoxW(None, text, title, buttons)
- return
- else:
- if not isinstance(buttons, (tuple, list)):
- raise Exception('Buttons must be tuple or list.')
- for button in buttons:
- if not isinstance(button, (tuple, list)):
- raise Exception('Buttons must contain tuple or list.')
- if len(button) != 2:
- raise Exception('Button must contain 2 elements: text and command.')
- dialog = tk.Toplevel(root)
- tk_set_window_title(dialog, title)
- tk_set_window_position(dialog, int(root.winfo_screenwidth() / 2 - 150), int(root.winfo_screenheight() / 2 - 75))
- tk_set_window_size(dialog, 300, 150)
- tk_set_window_no_close(dialog)
- tk_set_window_no_resize(dialog)
- tk_set_window_no_border(dialog)
- tk_set_window_taskbar_icon(dialog, PROJECT_DIR + '/img/icon.ico')
- frame_text = tk
- def tk_create_message_box(root, title, text, buttons):
- if sys.platform == 'win32':
- import ctypes
- ctypes.windll.user32.MessageBoxW(None, text, title, buttons)
- return
- else:
- if not isinstance(buttons, (tuple, list)):
- raise Exception('Buttons must be tuple or list.')
- for button in buttons:
- if not isinstance(button, (tuple, list)):
- raise Exception('Buttons must contain tuple or list.')
- if len(button) != 2:
- raise Exception('Button must contain 2 elements: text and command.')
- dialog = tk.Toplevel(root)
- tk_set_window_title(dialog, title)
- tk_set_window_position(dialog, int(root.winfo_screenwidth() / 2 - 150), int(root.winfo_screenheight() / 2 - 75))
- tk_set_window_size(dialog, 300, 150)
- tk_set_window_no_close(dialog)
- tk_set_window_no_resize(dialog)
- tk_set_window_no_border(dialog)
- tk_set_window_taskbar_icon(dialog, PROJECT_DIR + '/img/icon.ico')
- frame_text = tk.Frame(dialog)
- tk.Label(frame_text, text=text, justify=tk.CENTER).pack(fill=tk.X)
- frame_text.pack(fill=tk.X, padx=15, pady=15)
- frame_buttons = tk.Frame(dialog)
- for i in range(len(buttons)):
- if i == 0:
- tk.Button(frame_buttons, text=buttons[i][0], command=buttons[i][1]).pack(side=tk.LEFT, padx=5, pady=5)
- else:
- tk.Button(frame_buttons, text=buttons[i][0], command=buttons[i][1]).pack(side=tk.LEFT, padx=5, pady=5)
- frame_buttons.pack(fill=tk.X, padx=15, pady=15)
- tk_set_window_focus(dialog)
- dialog.wait_window()
- def tk_create_message_box_ok(root, title, text):
- tk_create_message_box(root, title, text, (('OK', root.destroy),))
- def tk_create_message_box_ok_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('OK', root.destroy), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_yes_no(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy), ('No', lambda: root.quit())))
- def tk_create_message_box_yes_no_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy), ('No', lambda: root.quit()), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_abort_retry_ignore(root, title, text):
- tk_create_message_box(root, title, text, (('Abort', root.destroy), ('Retry', root.destroy), ('Ignore', root.destroy)))
- def tk_create_message_box_abort_retry_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Abort', root.destroy), ('Retry', root.destroy), ('Continue', root.destroy)))
- def tk_create_message_box_yes_all_no_all(root, title, text):
- tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('No All', root.destroy)))
- def tk_create_message_box_yes_all_no_all_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('No All', root.destroy), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_yes_all_no_all_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('No All', root.destroy), ('Continue', root.destroy)))
- def tk_create_message_box_yes_all_no_all_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('No All', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_no_all_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('No All', root.destroy), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_no_all_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('No All', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_yes_all_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_yes_all_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('Continue', root.destroy)))
- def tk_create_message_box_yes_all_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes All', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_no_all_continue(root, title, text):
- tk_create_message_box(root, title, text, (('No All', root.destroy), ('Continue', root.destroy)))
- def tk_create_message_box_yes_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_yes_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Continue', root.destroy)))
- def tk_create_message_box_yes_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_no_continue(root, title, text):
- tk_create_message_box(root, title, text, (('No', root.destroy), ('Continue', root.destroy)))
- def tk_create_message_box_no_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('No', root.destroy), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_no_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('No', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Continue', root.destroy),))
- def tk_create_message_box_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('Cancel', lambda: root.quit()),))
- def tk_create_message_box_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_yes_no_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy), ('No', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_yes_no_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy), ('No', lambda: root.quit()), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_yes(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy),))
- def tk_create_message_box_no(root, title, text):
- tk_create_message_box(root, title, text, (('No', lambda: root.quit()),))
- def tk_create_message_box_yes_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Continue', root.destroy)))
- def tk_create_message_box_yes_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('Yes', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_no_continue(root, title, text):
- tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_no_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_no_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_no_yes(root, title, text):
- tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Yes', root.destroy)))
- def tk_create_message_box_no_yes_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Yes', root.destroy), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_no_yes_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('No', lambda: root.quit()), ('Yes', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_ok_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_ok_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_ok_continue(root, title, text):
- tk_create_message_box(root, title, text, (('OK', root.destroy), ('Continue', root.destroy)))
- def tk_create_message_box_ok_no_continue(root, title, text):
- tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('No', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_ok_no_yes_cancel_continue(root, title, text):
- 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)))
- def tk_create_message_box_ok_yes_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Yes', root.destroy), ('Cancel', lambda: root.quit())))
- def tk_create_message_box_ok_yes_cancel_continue(root, title, text):
- tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Yes', root.destroy), ('Cancel', lambda: root.quit()), ('Continue', root.destroy)))
- def tk_create_message_box_ok_cancel(root, title, text):
- tk_create_message_box(root, title, text, (('OK', lambda: root.quit()), ('Cancel', lambda: root.quit())))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement