【问题标题】:Tkinter - Python : Changing background colorTkinter - Python:更改背景颜色
【发布时间】:2016-09-10 19:59:41
【问题描述】:
[Person: Ahmet] is very handsome . He is working at [Organization: Zara] , their family live in [Location: Istanbul]


Person: 1
Location: 1
Organization: 1

我有一个这样的文本输出,我在 tkinter text 中显示它。 我想这样改变我的背景颜色:

[Person: anyword] # yellow
[Organization: anyword] #green
[Location: anyword] #orange

如何创建标签配置?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import os.path
import io
import subprocess as sub

import Tkinter
from Tkinter import *
import csv
import subprocess

from tkFileDialog import *


class App(object):
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        self.text = Text()
    self.text1 = Text()
    self.text.pack()
    self.text1.pack()

        menu = Menu(master)
        root.config(menu=menu)
        # file menu
        filemenu = Menu(menu, tearoff=0)
        menu.add_cascade(label="Browse Text File", menu=filemenu)
        filemenu.add_command(label="New")
        filemenu.add_command(label="Open", command=self.file_open)
        filemenu.add_command(label="Save", command=self.file_save)        
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.do_exit)

    def file_open(self):
        """open a file to read"""
        # optional initial directory (default is current directory)
        initial_dir = "C:\Temp"
        # the filetype mask (default is all files)
        mask = \
        [("Text and Python files","*.txt"), 
        ("HTML files","*.htm"), 
        ("All files","*.*")]        
        fin = askopenfile(initialdir=initial_dir, filetypes=mask, mode='r')
        text = fin.read()
    proc = subprocess.Popen(['python', 'ner.py',  str(askopenfilename())], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        if text != None:
            self.text.delete(0.0, END)
            self.text.insert(END,text)
        self.text1.delete(0.0, END)
        self.text1.insert(END,proc.communicate()[0])



    def file_save(self):
        """get a filename and save the text in the editor widget"""
        # default extension is optional, here will add .txt if missing
        fout = asksaveasfile(mode='w', defaultextension=".txt")
        text2save = str(self.text.get(0.0,END))
        fout.write(text2save)
        fout.close()

    def do_exit(self):
        root.destroy()



root = Tk()
root.title("Named Entity Recognizer (TR)")
app = App(root)
root.mainloop()

首先我显示原始文本,然后我显示找到 ner 类型的已编辑文本。我知道它适用于标签配置,但我如何在括号内添加单词。感谢您的帮助。

text.tag_config("[Person: ??]", background="yellow", foreground="red")

【问题讨论】:

    标签: python colors tkinter background-color


    【解决方案1】:

    这是从某处的互联网上复制的

    def color_text(edit, tag, word, fg_color='black', bg_color='white'):
        # add a space to the end of the word
        word = word + " "
        edit.insert('end', word)
        end_index = edit.index('end')
        begin_index = "%s-%sc" % (end_index, len(word) + 1)
        edit.tag_add(tag, begin_index, end_index)
        edit.tag_config(tag, foreground=fg_color, background=bg_color)
    
    
    # pick word to be colored
    myword1 = 'Jack'
    myword2 = 'Jill'
    # create a list of unique tags
    tags = ["tg" + str(k) for k in range(len(word_list))]
    for ix, word in enumerate(word_list):
        # word[:len(myword)] for word ending with a punctuation mark
        if word[:len(myword1)] == myword1:
            color_text(edit, tags[ix], word, 'blue')
        elif word[:len(myword2)] == myword2:
            color_text(edit, tags[ix], word, 'red', 'yellow')
        else:
            color_text(edit, tags[ix], word)
    

    【讨论】:

      【解决方案2】:

      你在这里做的是创建一个 Tkinter 画布。我不确定 python 2 中的确切语法,因为我使用的是 3,但这只会导致微小的变化:

      from Tkinter import *
      root = Tk()
      
      c = Canvas(root, width=500, height=500, bg='red') #change the color at bg
      c.pack()
      c.mainloop()
      

      您可以根据输出使用变量更改画布颜色。上面写着bg='red' 的地方就是你改变颜色的地方。 如果我的 python 2 语法不正确,请告诉我,希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2020-10-09
        • 2023-04-10
        • 2012-07-31
        • 2017-10-16
        • 2018-08-19
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多