从颜色列表中使用for循环更改Tkinter框架颜色

时间:2017-11-01 17:13:13

标签: python tkinter

我有以下代码段。我需要编码,当我单击按钮时,我需要框架颜色从定义的颜色列表中逐个更改。

 from tkinter import *
 from tkinter import ttk

def ChangeColor():
colors = ['red','green', 'orange','blue']
for color in colors:
    #color = entry.get()
    frame.config(bg = color)

root = Tk()
root.title("Title")

frame = Frame (root, width = 260, height = 200)
frame.pack()

btn = ttk.Button(frame, text = 'Change color', command = ChangeColor)
btn.place (x = 80, y = 100)

entry = ttk.Entry (frame, width = 20)
entry.place(x = 80, y = 70)

root.mainloop()

3 个答案:

答案 0 :(得分:5)

您可以使用itertools中的cycle迭代器来实现此目的。

from tkinter import *
from tkinter import ttk
from itertools import cycle

root = Tk()
root.title("Title")

frame = Frame (root, width = 260, height = 200)
frame.pack()

colors = ['red','green', 'orange','blue']
color_gen = cycle(colors)

def ChangeColor():
    frame.config(bg = next(color_gen))

btn = ttk.Button(frame, text = 'Change color', command = ChangeColor)
btn.place (x = 80, y = 100)

entry = ttk.Entry (frame, width = 20)
entry.place(x = 80, y = 70)

root.mainloop()

我需要提一件事:请避免做“明星”进口。当你这样做

from tkinter import *

它将135个Tkinter名称放入您的命名空间;在Python 2中,你得到175个名字。这会在命名空间中造成不必要的混乱,并且可能导致名称冲突:如果您不小心使用导入神秘错误的导入名称之一命名其中一个变量。当你使用多个模块进行星级导入时,情况更糟,因为它们可以踩踏彼此的名字。此外,星型导入使代码更难以阅读,因为您必须记住在本地定义的名称和导入的名称。

答案 1 :(得分:2)

我会将你的应用程序更改为一个类,这样你就可以存储变量并轻松访问它们,我也将输入键绑定到条目小部件,这样也可以。这样,当您创建instance class appinstance Tk()import tkinter as tk from tkinter import ttk class app(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.title("Title") self.frame = tk.Frame(self, width = 260, height = 200) self.frame.pack() self.btn = ttk.Button(self.frame, text = 'Change color', command = self.ChangeColor) self.btn.place (x = 80, y = 100) self.entry = ttk.Entry (self.frame, width = 20) self.entry.place(x = 80, y = 70) self.entry.bind("<Return>",self.ChangeColorEntry) self.colors = ['red','green','orange','blue'] self.current_color = -1 self.standard_bg = self.frame['background'] def ChangeColor(self,event=None): if self.current_color == len(self.colors) - 1: self.frame.config(bg = self.standard_bg) self.current_color = -1 return else: self.current_color += 1 color = self.colors[self.current_color] self.frame.config(bg = color) def ChangeColorEntry(self,event=None): entered = self.entry.get().lower().strip() if entered == "none": self.frame.config(bg = self.standard_bg) else: try: self.current_color = self.colors.index(entered) self.frame.config(bg = entered) except: pass ,但您不必将其称为root

np.loadtxt(stockData.values, delimiter=' ', unpack=True, converters={0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})

答案 2 :(得分:1)

@PM 2Rings答案更清晰但是因为我正在研究这个问题,我想我还是会发布它,你想手动实现它

from tkinter import *
from tkinter import ttk

colors = ['red', 'green', 'orange', 'blue']
colors_it = iter(colors)

def get_next_color():
    try:
        global colors_it
        return next(colors_it)
    except StopIteration:
        colors_it = iter(colors)
        return next(colors_it)

def ChangeColor():
    frame.config(bg=get_next_color())

root = Tk()
root.title("Title")

frame = Frame (root, width = 260, height = 200)
frame.pack()

btn = ttk.Button(frame, text = 'Change color', command = ChangeColor)
btn.place (x = 80, y = 100)

entry = ttk.Entry (frame, width = 20)
entry.place(x = 80, y = 70)

root.mainloop()