[英]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.