如何使用tkinter中的按钮更改背景颜色和前景颜色?

发布于 2025-02-13 20:55:36 字数 2136 浏览 3 评论 0原文

因此,我要做的是为我的应用程序创建一个主题选择器,

例如,用户可以单击一个绿色/黑色按钮,它将每个小部件的背景更改为黑色,它将每个小部件都会更改为绿色。然后,他们可以单击红色/白色按钮,它将执行相同的操作,但将每个小部件的背景更改为白色,每个小部件都将前景更改为红色。

# 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来做到这一点。

So what I am trying to do is create a theme picker for my application

For example, the user could click on a the Green/Black button and it would change every widgets background to Black and it would change every widgets foreground to Green. Then they could click the Red/White button and it would do the same thing but change every widgets background to White and every widgets foreground to Red.

# 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()

I have tried using a function. Something like this.

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)

But that doesn't work. I think a function is the right way to go, but I may be wrong.

I will also need this 'theme' to carry on to any other windows that are created from this window. I think I can just do that by using lambda in the command section of the widget.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[旋木] 2025-02-20 20:55:36

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方法正在引起错误,因为我们尚未定义它。

Widget.config(bg=color) is what your looking for.

Here is a small example of a theme-changing app:

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()

I'll try to change it so that it applies for your code.
However, your provided script does not seem to be a working one. I assume this is because it is only a snippet of the real one. I'm not pushing for your entire code, but edit it so we can run it. Ex. openCipher method is causing errors as we have not defined it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文