Trying_hard
Trying_hard

Reputation: 9501

Tkinter Lambda Button

 from Tkinter import *
 import os

 ALL = N+S+W+E

 class Application(Frame):
def __init__(self, master=None):
    Frame.__init__(self, master)
    self.master.rowconfigure(0, weight=1)
    self.master.columnconfigure(0, weight=1)
    self.grid(sticky=ALL)

    for r in range(2):
        self.rowconfigure(r, weight=1)

        names = {0:'RED', 1:'BLUE', 2:'GREEN', 3:'BLACK', 4:'OPEN'}    
    for c, colname in enumerate(names):
        name = names[c]
        self.columnconfigure(c, weight=1)
        if colname < 4:
            Button(self, text=name, command = lambda x = name:  self.setcolor(x)).grid(row=2, column=c, sticky=ALL)
        else:
            Button(self ,width =30, text=name, command=self.doFileOpen).grid(row=2,column=c,sticky=ALL)  

    Frame1 = Frame(self, bg="red")
    Frame1.grid(row = 0, column = 0, rowspan = 1, columnspan = 2, sticky = ALL)
    Frame1.bind("<Button-1>", self.handler_1)
    Frame2 = Frame(self, bg="yellow")
    Frame2.grid(row = 1, column = 0, rowspan = 1, columnspan = 2, sticky = ALL)
    Frame2.bind("<Button-1>", self.handler_2)
    Frame3 = Frame(self)
    Frame3.grid(row = 0, column = 2, rowspan = 2, columnspan = 3, sticky = ALL)
    self.text_in = Entry(Frame3)
    self.output = Text(Frame3)
    self.text_in.pack(fill = X, expand=False)
    self.output.pack(fill = BOTH, expand=True)


def handler_1(event):
        print("clicked Frame 1 at", event.x, event.y)

def handler_2(event):
        print("clicked Frame 2 at", event.x, event.y)

def doFileOpen(self):
    eText = self.text_in.get()
    self.output.delete(1.0,END)
    self.output.insert(1.0, eText)

def setcolor(self,bname):



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

I am trying to get a setcolor definition to be called when I push one of the buttons <4. I am having an issue writing the set color definition. what is the best way to once I push the button change the color of the text in the text box that is gather from the entry box? When I click the "RED" box the text becomes red, "BLUE" becomes blue etc.

Do I need to call again dofileopen or can I just change the color after the fact?

Upvotes: 0

Views: 383

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385880

All Tkinter widgets have a configure method which lets you change any of the options associated with that widget. For example, to change the foreground color of a widget you would do this:

self.output.configure(foreground="red")

For text widgets, you can change the attributes of a range of characters by configuring a tag, then adding that tag to a range of text. For example:

self.output.tag_configure("tag1", foreground="red")
...
self.output.tag_add("tag1", 1.0, 2.0)

Upvotes: 1

TankorSmash
TankorSmash

Reputation: 12747

I'm not sure I understand correctly, but you're asking how to write the setcolor function so that it takes a color, and then sets the color of the appropriate text box? You could do something like:

def setcolor(self,bname):
    if bname == "RED": #or 1, can't tell how you've written it
        self.text_in.config(foreground = RED) #I think that's how colors work in tkinter
    elif bname == "BLUE":
        self.text_in.config(foreground = BLUE)
    #etc etc

It assumes self has the Entry you're wanting to change.

Upvotes: 1

Related Questions