简体   繁体   中英

An application of TTKTheme to the app style

I have installed the ttk themes through pip install ttkthemes, import and apply theme 'blue' in labels, entries and buttons, however, the app style did not apply the selected theme. Following is my py file.

import requests
from tkinter import *
from tkinter import ttk
from ttkthemes import ThemedTk

root = ThemedTk(theme='blue')
root.title('Currency Converter')
root.geometry("450x400")

style = ttk.Style()
style.theme_use('blue')  
                       
def currency_convertion():
    global result_label
    url = "https://api.apilayer.com/exchangerates_data/convert?to=" + to_currency_entry.get() + "&from=" + from_currency_entry.get() + "&amount=" + amount_entry.get()

    payload = {}
    headers= {
    "apikey": ""
    }

    response = requests.request("GET", url, headers=headers, data = payload)

    status_code = response.status_code
    data = response.text
   
    result_label = ttk.Label(label_frame, text=f'{to_currency_entry.get()} {data[231:240]}', font='Helvetica, 25',bd=0, bg='#292929', fg='silver')
    result_label.grid(row=5, column= 0, columnspan=2)
    
    
def clear_result_label():
    result_label.config(text=f'', font='Helvetica, 25',bd=0, bg='#292929', fg='silver')
    from_currency_entry.delete(0, END)
    to_currency_entry.delete(0, END)
    amount_entry.delete(0, END)
    
frame = Frame(master=root, width=200, height=300)
frame.pack(padx=20, pady=20)

label_frame = Frame(master=root, width=350, height=300)
label_frame.pack(pady=10)

from_currency_label = ttk.Label(frame, text='From Currency')
from_currency_label.grid(row=1, column=0, pady=10)

to_currency_label = ttk.Label(frame, text='To Currency')
to_currency_label.grid(row=2, column=0, pady=10)
 
amount_label = ttk.Label(frame, text='Amount')
amount_label.grid(row=3, column=0, pady=10)

from_currency_entry = ttk.Entry(frame, font="Helvetica, 15")
from_currency_entry.grid(row=1, column=1, stick=W+E+N+S, pady=10)

to_currency_entry = ttk.Entry(frame, font="Helvetica, 15")
to_currency_entry.grid(row=2, column=1, stick=W+E+N+S,pady=10)
 
amount_entry = ttk.Entry(frame, font="Helvetica, 15")
amount_entry.grid(row=3, column=1, stick=W+E+N+S, pady=10)

button = ttk.Button(frame, text="Convert", command=currency_convertion)
button.grid(row=4, column=0, pady=20, padx=35)

button = ttk.Button(frame, text="Clear", command=clear_result_label)
button.grid(row=4, column=1, pady=20)

root.mainloop()

Would each theme need to install individually? or change the themes only the way to change the style.theme_use that will affect every widget? How could I apply the select theme to the app style?

I tried your code and below is the resulting theme. I think it has worked, especially when comparing it to the non-themed UI under that. I know that I don't pose an answer but I thought I'd just let you know that your theme works on my computer(linux). Maybe try reinstalling ttkthemes?

生成的用户界面

没有主题界面

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM