[英]How to set different colors on label text in pandas pie chart
[英]How merge text in a tkinter label with different colors
我正在编写一个带有tkinter标签的小股票股票代码程序,我需要在红色和绿色的同一行文本中合并。 我怎样才能做到这一点?
如果没有,是否还有其他小部件,我可以用它?
标签中不能有多种颜色。 如果需要多种颜色,请使用单行文本小部件,或使用带有文本项的画布。
这是使用文本小部件的快速而肮脏的示例。 它不做平滑滚动,不使用任何实际数据,并且泄漏内存,因为我从不修剪输入小部件中的文本,但它给出了一般的想法:
import Tkinter as tk
import random
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.ticker = tk.Text(height=1, wrap="none")
self.ticker.pack(side="top", fill="x")
self.ticker.tag_configure("up", foreground="green")
self.ticker.tag_configure("down", foreground="red")
self.ticker.tag_configure("event", foreground="black")
self.data = ["AAPL", "GOOG", "MSFT"]
self.after_idle(self.tick)
def tick(self):
symbol = self.data.pop(0)
self.data.append(symbol)
n = random.randint(-1,1)
tag = {-1: "down", 0: "even", 1: "up"}[n]
self.ticker.configure(state="normal")
self.ticker.insert("end", " %s %s" % (symbol, n), tag)
self.ticker.see("end")
self.ticker.configure(state="disabled")
self.after(1000, self.tick)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
如果你想在同一行上获得两种颜色,可以使用几个标签并使用.grid()
将它们放在同一行上。
如果你知道你想要两个单词和两个颜色,例如你可以使用这样的东西:
root = Tk()
Label(root,text="red text",fg="red").grid(column=0,row=0)
Label(root,text="green text",fg="green").grid(column=0,row=1)
mainloop()
或者,如果您希望字符串中的每个单词都有不同的颜色,例如:
words = ["word1","word2","word3","word4"]
colours = ["blue","green","red","yellow"]
for index,word in enumerate(words):
Label(window,text = word,fg=colours[index]).grid(column=index,row=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.