| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 766 人关注过本帖
标题:[求助]text控件对指定内容染色
只看楼主 加入收藏
ouyang2009
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-8-3
收藏
 问题点数:0 回复次数:0 
[求助]text控件对指定内容染色
是这样的,我想要做一个html编辑器。然后我现在想实现一个部分关键字变色的效果(和Python IDLE输入print()变成橙色的效果一样。),可是我做不出来。还请各位大佬指导:
源代码:
程序代码:
import about_file as f
from tkinter import *
import webbrowser as webo

def close(*args):
  tk.destroy()
def iconify(*args):
  tk.iconify()
def z(*args):
  tk.overrideredirect(True)
def o(*args):
  tk.overrideredirect(False)
class commands():
    def __init__(self):
        self.file_name = f.getFileName('index.html')
    def save(self):
        with open(self.file_name, 'w') as file:
            global codes
            file.write(codes.get("1.0",END))
    def run(self):
        self.save()
        webo.open('file://'+self.file_name)
    def get(self):
        with open(self.file_name, 'r') as file:
            code = file.read()
            if code == '':
                return ''
            else:
                return code

tk = Tk()
c = commands()
# 获取屏幕宽
screen_width = tk.winfo_screenwidth()
# 获取屏幕高
screen_height = tk.winfo_screenheight()
tk.title('HTML编译器')
tk.geometry('%sx%s' % (screen_width, screen_height))
tk.overrideredirect(True)
tk.config(bg='white')
btn1 = Button(tk, text = '保存', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command = c.save)
btn1.grid(row = 0, column = 0)

btn2 = Button(tk, text = '运行', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command = c.run)
btn2.grid(row = 0, column = 1)

btn3 = Button(tk, text = '关闭', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command=close)
btn3.grid(row = 0, column = 2)
btn4 = Button(tk, text = '最小化', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command=iconify)
btn4.grid(row = 0, column = 3)
btn5 = Button(tk, text = '恢复标题栏', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command=o)
btn5.grid(row = 0, column = 5)
btn5 = Button(tk, text = '全屏化', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command=z)
btn5.grid(row = 0, column = 4)
tk.update()
codes = Text(tk, font=('微软雅黑', 10, 'bold'), bg='Cyan', fg='Green')
codes.place(x = 0, y = 25, width=screen_width, height=screen_height)
codes.insert(END,c.get())
codes.insert(END,'你好')

tk.mainloop()
搜索更多相关主题的帖子: white text Button def 微软 
2021-12-11 21:17
快速回复:[求助]text控件对指定内容染色
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.092189 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved