我想将我的ttk表单(凯撒代码脚本)的背景更改为我在窗口中拥有的颜色=#FEFB22
我想将白色“盒子”颜色更改为#FEFB22 click to see image !
from tkinter import *
from tkinter import ttk
# variables
letters="ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
# fenetre
root = Tk()
#window title
root.title('Cesar Crypt/Decrypt par Maxence')
root.iconbitmap("logo.ico")
# allows for window to be resized
root.geometry("850x480")
# window background
root.configure(background='#FEFB22')
root.frame_header = ttk.Frame()
ttk.Label(root.frame_header, text = 'Code César', style = 'Header.TLabel').grid(row = 0, column = 1)
ttk.Label(root.frame_header, text = 'Entrez un nombre entre 1 et 25 :', style = 'Header.TLabel').grid(row = 1, column = 0)
ttk.Label(root.frame_header, text = 'Votre Message:', style = 'Header.TLabel').grid(row = 2, column = 0)
ttk.Label(root.frame_header, text='Votre message crypté/decrypté:',style='Header.TLabel').grid(row=4, column=0)
enc_dec_text = ttk.Entry(root.frame_header, width=110)
enc_dec_text.grid(row=4,column=1)
cipher_shift_menu = StringVar()
Spinbox(root.frame_header,from_=1, to=25, textvariable=cipher_shift_menu).grid(row=1, column=1)
text_entry = ttk.Entry(root.frame_header, width=100)
text_entry.grid(row=2,column=1)
def encrypt_text():
stringtoencrypt = text_entry.get()
stringtoencrypt = str(stringtoencrypt)
stringtoencrypt=stringtoencrypt.upper()
ciphershift = cipher_shift_menu.get()
ciphershift=int(ciphershift)
stringencrypted=""
for character in stringtoencrypt:
position = letters.find(character)
newposition = position+ciphershift
if character in letters:
stringencrypted = stringencrypted + letters[newposition]
else:
stringencrypted = stringencrypted + character
enc_dec_text.insert(0, stringencrypted)
def decrypt_text():
stringtodecrypt = text_entry.get()
stringtodecrypt = str(stringtodecrypt)
stringtodecrypt=stringtodecrypt.upper()
ciphershift = cipher_shift_menu.get()
ciphershift=int(ciphershift)
stringdecrypted=""
for character in stringtodecrypt:
position = letters.find(character)
newposition = position-ciphershift
if character in letters:
stringdecrypted = stringdecrypted + letters[newposition]
else:
stringdecrypted = stringdecrypted + character
enc_dec_text.insert(0, stringdecrypted)
#add image
width = 200
lenght = 200
image = PhotoImage(file="logort.png")
#bouton image
button = Button(root, image=image, bg='#FEFB22', bd=0, relief=SUNKEN)
button.pack()
# boutous
encrypt_button = ttk.Button(root.frame_header,text='Encrypter',command = lambda: encrypt_text()).grid(row=3,column=0)
decrypt_button = ttk.Button(root.frame_header,text='Decrypter',command = lambda: decrypt_text()).grid(row=3,column=1)
#bouton q
# uitter
bouton_quitter = Button(root, text="Quitter !", command=root.quit, font=("Courrier", 20), bg='#000000', fg='#FEFB22')
bouton_quitter.pack(side=BOTTOM)
root.frame_header.pack()
root.mainloop()
您可以使用ttk.Style
更改框的背景色:
s = ttk.Style()
s.configure('My.TFrame', background='#FEFB22')
root.frame_header = ttk.Frame(root, style='My.TFrame')