1

I'm coding a little stocks ticker displayer program with tkinter label and I need to merge in the same line text in red color and green color. How can I do that?

If not, is there any other widget which I can do it with?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Trimax
  • 2,413
  • 7
  • 35
  • 59
  • I am unsure what you mean by "merge in the same line". It is possible to change Label colour, but I don't understand what you're trying to accomplish. – RageCage Jul 15 '14 at 14:43
  • @Batman in a stock exchange ticket the symbols and prices of the securities are scrolling in a text lines, but each securitie is put in red or in green according with the daily changes of the price. Like this image: http://ak4.picdn.net/shutterstock/videos/3670265/preview/stock-footage-custom-made-stock-ticker-symbols-and-prices-animated-across-the-screen-symbols-are-original-not.jpg – Trimax Jul 15 '14 at 14:51
  • I don't understand your problem. Is there something wrong with the `Text.tag_config` method that you can't use it to set the `foreground` color of your desired text? If it's not working, show the code you've got. – Kevin Jul 15 '14 at 15:01
  • A quick google and I found this [SO question](http://stackoverflow.com/questions/14786507/how-to-change-the-color-of-certain-words-in-the-tkinter-text-widget). Does this help at all? – Shadow9043 Jul 15 '14 at 15:06
  • @Kevin I'm using a Label widget for display the ticker. Has the Label widget the .tag_config method or anyone similar? – Trimax Jul 15 '14 at 15:43
  • No, if you want to use the `tag_config` method, you should use the Text widget. – Kevin Jul 15 '14 at 15:45
  • @Shadow9043 The Text widget is a bit puzzling for me, so I'm trying to do that with the Label widget. – Trimax Jul 15 '14 at 15:45

2 Answers2

6

You cannot have multiple colors in a label. If you want multiple colors, use a one-line Text widget, or use a canvas with a text item.

Here's a quick and dirty example using a text widget. It doesn't do smooth scrolling, doesn't use any real data, and leaks memory since I never trim the text in the input widget, but it gives the general idea:

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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thank @BryanOakley, beautiful code!! I'd learned the Text tags with only read it ! Now, I've to refactor my scrolling algorithm that is more complex (here is, my code: https://github.com/SalvaJ/Learning/blob/master/tk_tutorial/ticker_display.py). You've helped me a lot! – Trimax Jul 15 '14 at 20:28
3

If you're looking to get two colours on the same line you can use several labels and use .grid() to get them on the same line.

If you know you wanted two words and two colours for example you can use something like this:

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()

Or if you wanted to have a different colours for each word in a string for example:

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)
Jacob
  • 750
  • 5
  • 19