- "python代碼高亮小程序4"
-
- import tkinter as tk
- from tkinter import scrolledtext
- from tkinter import messagebox
- from tkinter import ttk
- from pygments import highlight
- from pygments.lexers import PythonLexer
- from pygments.formatters import HtmlFormatter
- import webbrowser
-
-
- class Application(tk.Frame):
- def __init__(self, master=None):
- super().__init__(master)
- self.master = master
- self.master.title("Python代碼轉(zhuǎn)換成HTML格式高亮文本")
- self.master.geometry("1400x800")
- self.create_widgets()
-
- def create_widgets(self):
- # 左邊輸入框
- self.input_text1 = scrolledtext.ScrolledText(self.master, width=80, height=20)
- self.input_text1.grid(row=0, column=0, padx=10, pady=10)
-
- # 左邊輸出框
- self.output_text = scrolledtext.ScrolledText(self.master, width=80, height=20)
- self.output_text.grid(row=1, column=0, padx=10, pady=10)
-
- # 右邊預(yù)覽框
- self.preview_frame = tk.Frame(self.master, width=400, height=560)
- self.preview_frame.grid(row=0, column=1, rowspan=2, padx=10, pady=10)
- self.preview_text = tk.Text(self.preview_frame, width=60, height=40)
- self.preview_text.pack(fill="both", expand=True)
-
- # 粘貼源碼按鈕
- self.paste_button = ttk.Button(self.master, text="粘貼源碼", command=self.paste_code)
- self.paste_button.grid(row=2, column=0, padx=10, pady=10, sticky="w")
-
- # 轉(zhuǎn)為HTML按鈕
- self.convert_button = ttk.Button(self.master, text="轉(zhuǎn)為HTML", command=self.convert_to_html)
- self.convert_button.grid(row=2, column=0, padx=10, pady=10, sticky="n")
-
- # 復(fù)制HTML按鈕
- self.copy_button = ttk.Button(self.master, text="復(fù)制HTML", command=self.copy_html)
- self.copy_button.grid(row=3, column=0, padx=10, pady=10, sticky="e")
-
- # 預(yù)覽網(wǎng)頁按鈕
- self.preview_button = ttk.Button(self.master, text="預(yù)覽網(wǎng)頁", command=self.preview_web)
- self.preview_button.grid(row=3, column=0, padx=10, pady=10, sticky="s")
-
- def paste_code(self):
- self.input_text1.delete(1.0, tk.END)
- self.input_text1.insert(tk.END, self.master.clipboard_get())
-
- def convert_to_html(self):
- code = self.input_text1.get(1.0, tk.END)
- if not code.strip():
- messagebox.showerror("錯(cuò)誤", "請(qǐng)輸入Python代碼")
- return
- html = highlight(code, PythonLexer(), HtmlFormatter())
- self.output_text.delete(1.0, tk.END)
- self.output_text.insert(tk.END, html)
-
- def copy_html(self):
- html = self.output_text.get(1.0, tk.END)
- if not html.strip():
- messagebox.showerror("錯(cuò)誤", "請(qǐng)先轉(zhuǎn)換為HTML")
- return
- self.master.clipboard_clear()
- self.master.clipboard_append(html)
- messagebox.showinfo("提示", "已復(fù)制HTML到剪貼板")
-
- def preview_web(self):
- html = self.output_text.get(1.0, tk.END)
- if not html.strip():
- messagebox.showerror("錯(cuò)誤", "請(qǐng)先轉(zhuǎn)換為HTML")
- return
- with open("preview.html", "w", encoding="utf-8") as f:
- f.write(f"<style type='text/css'>{HtmlFormatter().get_style_defs('.highlight')}</style> ")
- f.write(html)
- webbrowser.open("preview.html")
-
-
- if __name__ == '__main__':
- root = tk.Tk()
- app = Application(master=root)
- app.mainloop()
|