from tkinter import *

def handler(event):
    print("Clicked at", event.x, event.y)

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()

        for r in range(6):
            self.master.rowconfigure(r, weight=100)    
        #Creating custom buttons
        self.master.columnconfigure(0, weight=100)
        Button(master, text="Red",command=self.colorit).grid(row=6,column=0,sticky=E+W)
        self.master.columnconfigure(1, weight=100)
        Button(master, text="Blue").grid(row=6,column=1,sticky=E+W)
        self.master.columnconfigure(2, weight=100)
        Button(master, text="Green").grid(row=6,column=2,sticky=E+W)
        self.master.columnconfigure(3, weight=100)
        Button(master, text="Black").grid(row=6,column=3,sticky=E+W)
        self.master.columnconfigure(4, weight=100)
        Button(master, text="Open", command=self.hanlderf).grid(row=6,column=4,sticky=E+W)

        Frame1 = Frame(master, bg="red", width=100, height=50)
        Frame1.grid(row = 0, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S) 
        Frame1.bind("<Button-1>", handler)
        Label(Frame1, text='frame1').grid(row=0)
        
        Frame2 = Frame(master, bg="blue",width=100, height=50)
        Frame2.grid(row = 3, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S)
        Frame2.bind("<Button-1>", handler)
        Label(Frame2, text='frame2').grid(row=3)
        
        self.Frame3 = Frame(master, bg="green",width=200)
        self.Frame3.grid(row = 0, column = 2, rowspan = 6, columnspan = 3, sticky = W+E+N+S)
        self.text_in = Entry(self.Frame3,text="this is a text")
        self.text_in.grid(row = 5, column = 2)
        Label(self.Frame3, text='frame3').grid(row=0,column=2)
        
    def hanlderf(self):
        try:
            text = self.text_in.get()
            myFile=open(text) # get a file handle
            self.myText= myFile.read() # read the file to variable
            myFile.close() # close file handle
        except Exception as msg:
            print("An error has ocurred: %s" % msg)
            self.myText="An error has ocurred.!"
            
        self.myTextWidget= Text(self.Frame3)
        self.myTextWidget.insert(0.0,self.myText) # insert the file's text into the text widget
        self.myTextWidget.grid(row = 7, column = 2)
    
    def colorit(self):
        self.myTextWidget.tag_config(self.myText, foreground="blue")
        #text.tag_config("a", foreground="blue")
        #text.insert(contents, ("n", "a"))
        self.myTextWidget.insert(self.myText, (self.myText))
        print("changing the color")
        

root = Tk()
app = Application(master=root)
app.mainloop()

so Im trying at the beginning:
self.master.columnconfigure(0, weight=100)
Button(master, text="Red",command=self.colorit).grid(row=6,column=0,sticky=E+W)

of calling the colorit function to change the color of the fonts within the textwidget, however can't get it work, I've gone through the tkinter api few times but it seems im not getting it right...

can you give me some advice of how should i approach this guys? btw im pretty new to python so.. app for the crappy code :D

thanks!

First, you have to call "hanlderf()" and create the text widget. Since I don't have the file, the text inserted into the widget is different.

from tkinter import *
 
def handler(event):
    print("Clicked at", event.x, event.y)
 
class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
 
        for r in range(6):
            self.master.rowconfigure(r, weight=100)    
        #Creating custom buttons
        self.master.columnconfigure(0, weight=100)
        Button(master, text="Red",command=self.colorit).grid(row=6,column=0,sticky=E+W)
        self.master.columnconfigure(1, weight=100)
        Button(master, text="Blue").grid(row=6,column=1,sticky=E+W)
        self.master.columnconfigure(2, weight=100)
        Button(master, text="Green").grid(row=6,column=2,sticky=E+W)
        self.master.columnconfigure(3, weight=100)
        Button(master, text="Black").grid(row=6,column=3,sticky=E+W)
        self.master.columnconfigure(4, weight=100)
        Button(master, text="Open", command=self.hanlderf).grid(row=6,column=4,sticky=E+W)
 
        Frame1 = Frame(master, bg="red", width=100, height=50)
        Frame1.grid(row = 0, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S) 
        Frame1.bind("<Button-1>", handler)
        Label(Frame1, text='frame1').grid(row=0)
 
        Frame2 = Frame(master, bg="blue",width=100, height=50)
        Frame2.grid(row = 3, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S)
        Frame2.bind("<Button-1>", handler)
        Label(Frame2, text='frame2').grid(row=3)
 
        self.Frame3 = Frame(master, bg="green",width=200)
        self.Frame3.grid(row = 0, column = 2, rowspan = 6, columnspan = 3, sticky = W+E+N+S)
        self.text_in = Entry(self.Frame3,text="this is a text")
        self.text_in.grid(row = 5, column = 2)
        Label(self.Frame3, text='frame3').grid(row=0,column=2)
 
        ## call the function
        self.hanlderf()

    def hanlderf(self):
        """
        try:
            text = self.text_in.get()
            myFile=open(text) # get a file handle
            self.myText= myFile.read() # read the file to variable
            myFile.close() # close file handle
        except Exception as msg:
            print("An error has ocurred: %s" % msg)
            self.myText="An error has ocurred.!"
        """ 
        self.myTextWidget= Text(self.Frame3)
        self.myTextWidget.insert(0.0,"Change this color")
        self.myTextWidget.grid(row = 7, column = 2)
 
    def colorit(self):
        """ changes the text widget foreground color to blue when the 'red'
            button is clicked
        """
        self.myTextWidget.configure(fg='blue')
#        self.myTextWidget.tag_config(self.myText, foreground="blue")
        #text.tag_config("a", foreground="blue")
        #text.insert(contents, ("n", "a"))
        self.myTextWidget.insert(END, "\nColor changed")
        print("changing the color")
 
 
root = Tk()
app = Application(master=root)
app.mainloop()

@woooee thanks man, that worked!, marking it as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.