![](/img/trans.png)
[英]How to change the foreground or background colour of a Tkinter Button on Mac OS X?
[英]How do I change the background colour and foreground colour using a button in Tkinter?
所以我想做的是为我的应用程序创建一个主题选择器
例如,用户可以单击绿色/黑色按钮,它将每个小部件背景更改为黑色,并将每个小部件前景更改为绿色。 然后他们可以单击红色/白色按钮,它会做同样的事情,但将每个小部件背景更改为白色,将每个小部件前景更改为红色。
# Imports the tkinter library.
from tkinter import *
from tkmacosx import Button
# Pillow is a improved verison of PIL, stands for Python Image Library. Allows you to import image types like .jpg and .png
from PIL import ImageTk,Image
# Imports messagebox module from tkinter library.
from tkinter import messagebox
# Declare global variables
global selectedBackground
global selectedForeground
selectedBackground="white"
selectedForeground="red"
# Tk() helps to display the root window and manages all the other components of the tkinter application and assigns it to root.
root = Tk()
root.eval("tk::PlaceWindow . center")
root.configure(bg=selectedBackground)
cipherButton = Button(root, text=" Cipher ", padx=40, pady=20, command=openCipher, borderwidth=0, fg="#22fd35", bg="black", highlightbackground="#22fd35").grid(row=1, column=0)
decipherButton = Button(root, text="Decipher", padx=40, pady=20, command=openDecipher, borderwidth=0, fg="#22fd35", bg="black", highlightbackground="#22fd35").grid(row=1, column=1)
spacer1 = Label(root, text=" ", padx=10, pady=1, background="black").grid(row=4, column=1)
quitButton = Button(root, text="Exit d3cryptt", padx=10, pady=5, command=root.quit, borderwidth=0, fg="#22fd35", bg="black", highlightbackground="#22fd35").grid(row=5, column=0, columnspan=2)
spacer2 = Label(root, text=" ", padx=10, pady=1, background=selectedBackground).grid(row=6, column=1)
changecolour = Button(root, text="change colour", padx=1, pady=5, background="black", command=changeColour).grid(row=7, column=0)
#Enter the event main loop
root.mainloop()
我试过使用一个函数。 像这样的东西。
def changeColour():
selectedBackground="black"
selectedForeground="#22fd35"
changecolour = Button(root, text="change colour", padx=1, pady=5, background="black", command=changeColour).grid(row=7, column=0)
但这不起作用。 我认为函数是正确的方法,但我可能错了。
我还需要这个“主题”来继续从这个窗口创建的任何其他窗口。 我想我可以通过在小部件的命令部分使用 lambda 来做到这一点。
Widget.config(bg=color)
是你要找的。
这是一个改变主题的应用程序的小例子:
from tkinter import *
from tkmacosx import Button
root = Tk()
def changethemetoblack():
root.config(bg="#000000")
def changethemetowhite():
root.config(bg="#ffffff")
def changethemetored():
root.config(bg="#ff0000")
themeblackbutton = Button(root, text="Change Theme To Black", command=changethemetoblack, bg="#000000", fg="#ffffff")
themewhitebutton = Button(root, text="Change Theme To White", command=changethemetowhite)
themeredbutton = Button(root, text="Change Theme To Red", command=changethemetored, bg="#ff0000", fg="#ffffff")
themeblackbutton.pack()
themewhitebutton.pack()
themeredbutton.pack()
root.mainloop()
我会尝试更改它以使其适用于您的代码。
但是,您提供的脚本似乎不是一个有效的脚本。 我认为这是因为它只是真实片段的一小部分。 我不是在推动你的整个代码,而是编辑它以便我们可以运行它。 前任。 openCipher
方法导致错误,因为我们没有定义它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.