|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这因该是最终版了,bug基本都修完了,还是很好用的
- from tkinter import *
- from tkinter import scrolledtext, filedialog, messagebox
- import re
- import tempfile
- import webbrowser
- import os
- class HTMLCompiler:
- def __init__(self, root):
- self.root = root
- self.root.title("HTML 编译器")
- self.root.configure(bg='black')
- self.current_file = None
-
- # 创建菜单栏
- self.menu_bar = Menu(root)
-
- # 创建工具栏
- self.toolbar = Frame(root, bg='#333')
-
- self.new_btn = Button(self.toolbar, text="新建", command=self.new_file, bg='#333', fg='white')
- self.open_btn = Button(self.toolbar, text="打开", command=self.open_file, bg='#333', fg='white')
- self.save_btn = Button(self.toolbar, text="保存", command=self.save_file, bg='#333', fg='white')
- self.run_btn = Button(self.toolbar, text="运行", command=self.run_html, bg='#333', fg='white')
- self.help_btn = Button(self.toolbar, text='帮助', command=self.help_file, bg='#333', fg='white')
- self.new_btn.pack(side=LEFT, padx=2, pady=2)
- self.open_btn.pack(side=LEFT, padx=2, pady=2)
- self.save_btn.pack(side=LEFT, padx=2, pady=2)
- self.run_btn.pack(side=LEFT, padx=2, pady=2)
- self.help_btn.pack(side=LEFT, padx=2, pady=2)
-
- self.toolbar.pack(fill=X)
-
- # 创建文本编辑器
- self.text_area = scrolledtext.ScrolledText(
- root, wrap=WORD, bg='black', fg='white',
- insertbackground='white', font=('Consolas', 12)
- )
- self.text_area.pack(fill=BOTH, expand=True)
-
- # 绑定事件
- self.text_area.bind('<KeyRelease>', self.on_key_release)
- self.text_area.bind('<Tab>', self.handle_tab)
- self.text_area.bind('<Return>', self.handle_return)
-
- # 绑定快捷键
- self.root.bind('<Control-n>', lambda event: self.new_file())
- self.root.bind('<Control-o>', lambda event: self.open_file())
- self.root.bind('<Control-s>', lambda event: self.save_file())
- self.root.bind('<Control-r>', lambda event: self.run_html())
-
- # 语法高亮颜色配置
- self.tag_colors = {
- 'tag': '#569CD6',
- 'attribute': '#9CDCFE',
- 'value': '#CE9178',
- 'comment': '#6A9955',
- 'doctype': '#569CD6',
- 'string': '#CE9178',
- 'symbol': '#D4D4D4'
- }
-
- # 配置标签样式
- for tag, color in self.tag_colors.items():
- self.text_area.tag_config(tag, foreground=color)
-
- # 初始高亮
- self.highlight()
- self.new_file()
- #显示字数
- self.charn = Label(root,text='字数:0',fg='white',bg='black')
- self.charn.pack(side=LEFT)
- #显示位置
- self.place = Label(root,text='行:0;列:0',fg='white',bg='black')
- self.place.pack(side=LEFT)
- self.label_place()
- #显示初始文本
- self.text_area.insert(1.0,'''<!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta charset="UTF-8">
- </head>
- <body>
- <!--文档的内容......-->
- </body>
- </html>''')
- self.on_key_release()
- self.len_char()
-
- # 文件操作功能(保持不变)
- def new_file(self, event=None):
- self.text_area.delete('1.0', END)
- self.current_file = None
- self.root.title("HTML 编译器 - 未命名")
-
- def open_file(self, event=None):
- file_path = filedialog.askopenfilename(
- filetypes=[("HTML 文件", "*.html"), ("所有文件", "*.*")]
- )
- if file_path:
- try:
- with open(file_path, 'r', encoding='utf-8') as file:
- content = file.read()
- self.text_area.delete('1.0', END)
- self.text_area.insert('1.0', content)
- self.current_file = file_path
- self.root.title(f"HTML 编译器 - {os.path.basename(file_path)}")
- self.highlight()
- except Exception as e:
- messagebox.showerror("错误", f"无法打开文件:\n{str(e)}")
-
- def save_file(self, event=None):
- if self.current_file:
- try:
- with open(self.current_file, 'w', encoding='utf-8') as file:
- file.write(self.text_area.get('1.0', END))
- messagebox.showinfo("保存", "文件保存成功!")
- except Exception as e:
- messagebox.showerror("错误", f"无法保存文件:\n{str(e)}")
- else:
- self.save_as_file()
-
- def save_as_file(self):
- file_path = filedialog.asksaveasfilename(
- defaultextension=".html",
- filetypes=[("HTML 文件", "*.html"), ("所有文件", "*.*")]
- )
- if file_path:
- try:
- with open(file_path, 'w', encoding='utf-8') as file:
- file.write(self.text_area.get('1.0', END))
- self.current_file = file_path
- self.root.title(f"HTML 编译器 - {os.path.basename(file_path)}")
- messagebox.showinfo("保存", "文件保存成功!")
- except Exception as e:
- messagebox.showerror("错误", f"无法保存文件:\n{str(e)}")
-
- def run_html(self, event=None):
- html_code = self.text_area.get("1.0", END)
-
- if not html_code.strip():
- messagebox.showwarning("警告", "没有内容可运行!")
- return
-
- try:
- with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.html',encoding='utf-8') as f:
- f.write(html_code)
- temp_path = f.name
- try:
- webb = webbrowser.get('chrome')
- webb.open('file://' + os.path.abspath(temp_path))
- except webbrowser.Error:
- webbrowser.open('file://' + os.path.abspath(temp_path))
- # 延迟删除临时文件,确保浏览器已打开
- self.root.after(5000, lambda: os.unlink(temp_path) if os.path.exists(temp_path) else None)
- except Exception as e:
- messagebox.showerror("错误", f"无法运行HTML:\n{str(e)}")
- if 'temp_path' in locals() and os.path.exists(temp_path):
- os.unlink(temp_path)
- def help_file(self):
- messagebox.showinfo('提示','''ctrl+n新建
- ctrl+o打开
- ctrl+s保存
- ctrl+r运行''')
-
- # 编辑器功能
- def on_key_release(self, event=None):
- self.highlight()
-
- def handle_tab(self, event):
- current_pos = self.text_area.index(INSERT)
- line, col = map(int, current_pos.split('.'))
-
- space_count = 4 - (col % 4)
- self.text_area.insert(INSERT, ' ' * space_count)
- return 'break'
-
- def handle_return(self, event):
- current_pos = self.text_area.index(INSERT)
- line, col = map(int, current_pos.split('.'))
- line_text = self.text_area.get(f'{line}.0', f'{line}.end')
-
- indent = 0
- while indent < len(line_text) and line_text[indent] == ' ':
- indent += 1
-
- self.text_area.insert(INSERT, '\n' + ' ' * indent)
-
- if line_text.strip().startswith('<') and not line_text.strip().startswith('</'):
- match = re.match(r'^\s*<([a-zA-Z]+)[^>]*>', line_text)
- if match and not line_text.strip().endswith('/>'):
- tag = match.group(1)
- self.text_area.insert(INSERT, f'\n{" " * indent}</{tag}>')
- self.text_area.mark_set(INSERT, f'{line+1}.{indent+1}')
-
- return 'break'
-
- def highlight(self):
- for tag in self.tag_colors.keys():
- self.text_area.tag_remove(tag, '1.0', END)
-
- text = self.text_area.get('1.0', END)
-
- # 高亮注释
- self.highlight_pattern(r'<!--.*?-->', 'comment')
-
- # 高亮DOCTYPE
- self.highlight_pattern(r'<!DOCTYPE.*?>', 'doctype')
-
- # 高亮标签
- self.highlight_pattern(r'<\/?[a-zA-Z]+', 'tag')
- self.highlight_pattern(r'<\/[a-zA-Z]+>', 'tag')
-
- # 高亮属性
- self.highlight_pattern(r'\s[a-zA-Z-]+=', 'attribute')
-
- # 高亮属性值
- self.highlight_pattern(r'"[^"]*"', 'value')
- self.highlight_pattern(r"'[^']*'", 'value')
-
- # 高亮符号
- self.highlight_pattern(r'[<>\/=]', 'symbol')
-
- def highlight_pattern(self, pattern, tag):
- start = '1.0'
- end = END
-
- self.text_area.mark_set('matchStart', start)
- self.text_area.mark_set('matchEnd', start)
-
- count = IntVar()
- while True:
- index = self.text_area.search(
- pattern, 'matchEnd', end,
- count=count, regexp=True
- )
- if index == '': break
-
- self.text_area.mark_set('matchStart', index)
- self.text_area.mark_set('matchEnd', f'{index}+{count.get()}c')
-
- self.text_area.tag_add(tag, 'matchStart', 'matchEnd')
-
- #提示功能
- def len_char(self,event=None):
- self.char = len(self.text_area.get(1.0,END)) - 1
- self.textn = '字数:%d'%self.char
- self.charn.config(text=self.textn)
- root.after(10,self.len_char)
-
- def label_place(self):
- self.cursor_pos = self.text_area.index(INSERT)
- self.line, self.column = self.cursor_pos.split('.')
- self.place.config(text=f"行: {self.line}, 列: {self.column}")
- root.after(10,self.label_place)
- if __name__ == '__main__':
- root = Tk()
- root.geometry('800x600')
- app = HTMLCompiler(root)
- root.mainloop()
复制代码 |
评分
-
查看全部评分
|