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()
当我按下其中一个按钮 <4 时,我试图获取一个要调用的 setcolor 定义。我在编写设置颜色定义时遇到问题。一旦我按下按钮更改从输入框收集的文本框中的文本颜色,最好的方法是什么?当我单击“红色”框时,文本变为红色,“蓝色”变为蓝色等。
我需要再次调用 dofileopen 还是可以在事后更改颜色?