Python Forum

Full Version: Tkinter Font Color
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
im very very new to python and im trying to change the font color of a very basic tkinter gui. what the script does is read a text file and display it in the tkinter gui. then logs the readings to a log file. im just trying to make the text from Data.txt show up red and the background of the gui text box to be black. im using python 2.7 can someone please help me out thanks in advanced,

import Tkinter as tk
import tkFont
import time
import sys
timeString = time.strftime('%m-%d-%Y')



class App(tk.Tk):

    def __init__(self,*args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # ... YOUR widgets here ...
        self.T = tk.Text(self, height=2, width=4, font=("bold", 32, ))
        self.S = tk.Scrollbar(self)
        self.T.config(yscrollcommand=self.S.set)
        self.T.pack(side=tk.LEFT, fill=tk.Y)
        self.S.config(command=self.T.yview)
        self.S.pack(side=tk.RIGHT, fill=tk.Y)
        self.updateWidgets()


    def updateWidgets(self):
        with open('Data.txt') as f:
            newText = f.read()
            p = open( 'Data-Log.txt', 'a' ) #(w= write)(a= append add lines)
            p.write(newText + timeString + '\n' + "////////" + '\n' + '\n')
            p.close()
            
            
        # ... YOUR code for updating the Widgets ...
        #self.T.delete('1.0', tk.END) #delet old text for incoming new text
        self.T.insert(tk.END, newText, foreground="red")
        self.after(1000, self.updateWidgets)

app = App()
app.mainloop()
im still not having any luck. im not sure what to do because the text is being read from a file and i cant just do somthing like,

T = f.read()(height=2, width=30, fg="blue")

can someone please give me an example
(Mar-25-2018, 08:48 AM)Larz60+ Wrote: [ -> ]see: http://infohost.nmt.edu/tcc/help/pubs/tk.../text.html
and: http://infohost.nmt.edu/tcc/help/pubs/tk.../text.html
Would you please show me an example on how to change display "newText" is a specific color like red. and how to change the background of the text box in the gui to black. if i see the code i can disect it to better understand what im doing. will you please help me, thankyou
self.T.configure(font= (family='Helvetica', size=14, weight='', fg='#0059b3')) 
Not tested, but if not this, very close, see:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/fonts.html
fg = text forground color
color selection can be found here: https://www.w3schools.com/colors/colors_picker.asp
this here is amost exactly what i need. now i cant figure out how to prevent it from making new text entry boxes. i would just like it to update the variable im trying to print but not create a new line/text box every time. if i run this code in a loop it keeps creating new boxes with the text i would just like it to update the same box?

[inline]from Tkinter import *

def onclick():
pass

root = Tk()
text = Text(root)
with open('sensData.txt') as f:
newText = f.read()
text.insert(END, newText)
text.pack()
text.update_idletasks()

text.tag_add(newText, "1.0", "1.2")
text.tag_add("start", "1.0", "1.3", "2.0", "2.2")
text.tag_config("start", background="black", foreground="green")
root.mainloop()
[/inline]

(Mar-26-2018, 11:21 AM)Larz60+ Wrote: [ -> ]
self.T.configure(font= (family='Helvetica', size=14, weight='', fg='#0059b3')) 
Not tested, but if not this, very close, see:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/fonts.html
fg = text forground color
color selection can be found here: https://www.w3schools.com/colors/colors_picker.asp
i tried something similar to that for the past 2 days ive been googling examples and i always get an error about syntax at the = signs. for the example you showed me i get a error about the syntax of the = after "family" sorry i didnt see you post before that last post i made. ive been experimenting with anything i can get close to work. this python is way different that the c++ on the arduino platform. until 2 days ago i have never messed with python.

sorry for blowing up this thread, but i think ive sorta figured some things out. ill start with this
" self.T.configure(fg='#0059b3', font=("bold", 22))" and see where i can get from here. thanks again
If you use one equal sign, use them on everything.

This can be quite confusing, the following mess shows why:

The actual rule is that:

any required arguments must have values, and order must be preserved if they do not use '='.
If = is used on both, they can be out of order.
but once an equal sign has been used, all remaining required arguments must also have one.

Non required arguments can be in any order if and only if they contain '=' signs.

Non required arguments don't require '=' if all arguments remain in order

An example will make this clearer:

c, d and e are defaulted, so don't have to be supplied
and can be in any order so long as they contain an = sign
a and b are, required and must be in order
>>> def xxx (a, b, c=0, d=1, e=2):
...    print(a)
...    print(b)
...    print(c)
...    print(d)
...    print(e)
The function above requires and a and b at minimum, so:

>>> xxx(a=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: xxx() missing 1 required positional argument: 'b'
creates error because b is required and missing

>>> xxx(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: xxx() missing 1 required positional argument: 'b'
creates error because b is required and missing

>>> xxx(1, b=2)
1
2
0
1
2
>>>
Works because both a and b have been passed

as will:
xxx(1, 2)
but not:
>>> xxx(a=2,3)
  File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
>>>
because once '=' has been used for o
and the following will work:
>>> xxx(a=2, b=3)
2
3
0
1
2
>>>
>>> xxx(1,2,3, e=7)
1
2
3
1
7
>>>
works because 1,2,3 are in order and e contains =

and finally:
>>> xxx(1,2,c=3,4,e=7)
  File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
>>> xxx(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: xxx() missing 1 required positional argument: 'b'
does not. Once the first defaulted argument contains an = sign, all that follow also require it

Bottom line is Always use assignment ('=') it will save you grief
(Mar-26-2018, 04:09 PM)Larz60+ Wrote: [ -> ]If you use one equal sign, use them on everything.

This can be quite confusing, the following mess shows why:

The actual rule is that:

any required arguments must have values, and order must be preserved if they do not use '='.
If = is used on both, they can be out of order.
but once an equal sign has been used, all remaining required arguments must also have one.

Non required arguments can be in any order if and only if they contain '=' signs.

Non required arguments don't require '=' if all arguments remain in order

An example will make this clearer:

Bottom line is Always use assignment ('=') it will save you grief

thats good information. i have alot to learn but im slowly picking up on it. Can you sort of explain this to me im trying to make this textbox show up basically just the way it does except i want it to overwrite the text in the box when it updates and not just keep opening a new field every time.
Quote: def opensingleTextbox(self):

text = Text(self)
with open('SensData.txt') as f:
text.pack_forget()
newText = f.read()
text.insert(END, newText)
text.pack()
text.tag_add(newText, "1.0", "1.2")
text.tag_add("start", "1.0", "1.3", "2.0", "2.5")
text.tag_config("start", background="black", foreground="green")

im sort of learning towards it outputting the output from telnets "telnet.read_some()" but right now im still trying to figure out how to replace the text in the window instead of creating excessive text boxes. ive tried "text.pack_forget() text.pack_destroy etc
Please read https://python-forum.io/misc.php?action=help&hid=25
you should be using python bbcode tags around your code.
There's no indentation in what you posted, so it's difficult to see what is indented and what is not.
If you use the proper tags, indentation will be preserved.
(Mar-27-2018, 04:34 AM)Larz60+ Wrote: [ -> ]Please read https://python-forum.io/misc.php?action=help&hid=25
you should be using python bbcode tags around your code.
There's no indentation in what you posted, so it's difficult to see what is indented and what is not.
If you use the proper tags, indentation will be preserved.

sorry about that. the code works it just dont work the way id like it to.

def greet(self):
           
        text = Text(self)
        with open('PhData.txt') as f:
            text.pack_forget() 
            newText = f.read()
        text.insert(END, newText)
        text.pack()
        text.tag_add(newText, "1.0", "1.2")
        text.tag_add("start", "1.0", "1.3", "2.0", "2.5")
        text.tag_config("start", background="black", foreground="green")
id like it to just replace the text on the same windows whenever it receives it. but i im having a hard time figuring out how to make it replace instead of create a new window
Pages: 1 2