Python 在文本的两种颜色之间交替
是否有一种方法可以在每次单击按钮时切换按钮的颜色?例如,它开始时为白色,第一次单击时变为蓝色,下次单击时变回白色,如果第三次单击,则再次变为蓝色。。。以同样的模式不断地重复 如果这是可能的,python是否有办法记住上一个会话中按钮的颜色,并在下次启动时保持该颜色 是的,这是可能的 这里,按下按钮时,文本颜色从红色切换为蓝色 如果将按钮选项的关键字参数从fg更改为bg,则在某些非OSX系统上,您将更改背景颜色而不是文本颜色。如果你愿意,试试看Python 在文本的两种颜色之间交替,python,button,tkinter,colors,Python,Button,Tkinter,Colors,是否有一种方法可以在每次单击按钮时切换按钮的颜色?例如,它开始时为白色,第一次单击时变为蓝色,下次单击时变回白色,如果第三次单击,则再次变为蓝色。。。以同样的模式不断地重复 如果这是可能的,python是否有办法记住上一个会话中按钮的颜色,并在下次启动时保持该颜色 是的,这是可能的 这里,按下按钮时,文本颜色从红色切换为蓝色 如果将按钮选项的关键字参数从fg更改为bg,则在某些非OSX系统上,您将更改背景颜色而不是文本颜色。如果你愿意,试试看 import tkinter as tk
import tkinter as tk # <-- avoid star imports
def toggle_color(last=[0]): # <-- creates a closure to memoize the last value of the colors index used to toggle
colors = ['red', 'blue']
color = colors[last[0]]
last[0] = (last[0] + 1) % 2 # <-- ensure the index is 0 or 1 alternatively
btn.config(fg=color)
if __name__ == '__main__':
root = tk.Tk()
btn = tk.Button(root, text='toggle', fg='blue', command=toggle_color)
btn.pack()
root.mainloop()
您可以在按下按钮时更改按钮的颜色。有几种方法可以做到这一点。我已经发布了一些片段 下面是一个代码:
from tkinter import * #for python2 Tkinter
global x
x = 0
root = Tk()
def b1c(): #this function will run on button press
global x
if x == 0:
b1["bg"] = "blue"
x += 1
elif x == 1:
b1["bg"] = "white"
x = 0
b1 = Button(root,text="Press me to change the color",command = b1c) #making button
if x == 1:b1["bg"] = "white";x =0
b1.place(x=1,y=1) #placing the button
mainloop()
上面的代码有点复杂,所以如果你想要一个简单的方法,那么我已经做了另一个代码。您还可以通过更改当前白色和蓝色的color1和color2的值来更改按钮的颜色:
如果你有一个颜色列表,你想改变一个接一个当按下按钮,然后你也可以这样做。在以下代码中,列表为颜色:
与其他答案不同,我建议采用面向对象的方法。 下面的示例使用tkinter Button类作为基础,但添加了on_color和off_color参数,以指定按钮在打开/关闭时的颜色 通过这种方式,您可以拥有任意多个具有类似行为的按钮,但不必具有更改每个按钮状态的功能。此行为是在类中定义的 on_颜色和off_颜色是可选参数,如果未指定,将分别默认为“绿色”和“红色”
import tkinter as tk
def pressed():
print("Pressed")
class ToggleButton(tk.Button):
def __init__(self, master, **kw):
self.onColor = kw.pop('on_color','green')
self.offColor = kw.pop('off_color','red')
tk.Button.__init__(self,master=master,**kw)
self['bg'] = self.offColor
self.bind('<Button-1>',self.clicked)
self.state = 'off'
def clicked(self,event):
if self.state == 'off':
self.state = 'on'
self['bg'] = self.onColor
else:
self.state = 'off'
self['bg'] = self.offColor
root = tk.Tk()
btn = ToggleButton(root,text="Press Me", on_color='blue',off_color='yellow',command=pressed)
btn.grid()
btn2 = ToggleButton(root,text="Press Me",command=pressed)
btn2.grid()
root.mainloop()
当窗口关闭时,这将调用应用程序的onClose方法。在这个方法中,您只需收集按钮的各种状态并将它们写入一个文件。比如说
settings = {}
settings['btn2_state'] = btn2.state
with open('settings.json','w') as settings_file:
settings.write(json.dumps(settings))
然后,当您再次打开程序时,可以通过以下方式重新读入
with open('settings.json','r') as settings_file:
settings = json.load(settings_file)
btn2.state = settings['btn2_state']
请张贴您的密码。
root.protocol("WM_DELETE_WINDOW", app.onClose)
settings = {}
settings['btn2_state'] = btn2.state
with open('settings.json','w') as settings_file:
settings.write(json.dumps(settings))
with open('settings.json','r') as settings_file:
settings = json.load(settings_file)
btn2.state = settings['btn2_state']