1

I have this code for TKinter and CustomTkinter to create a file upload button:

def import_file_data():
    v = tk.StringVar()
    global file_data
    csv_file_path = askopenfilename()
    v.set(csv_file_path)
    file_data = pd.read_csv(csv_file_path) 

file_button = ctk.CTkButton(root, text = "File Upload", command=import_file_data)
file_button.grid(row=5,column=0, padx=buttonx, pady=buttony)

This button is blue with white text by default and I want to make it so it turns green on the click action of the button to signify that a file was successfully uploaded.

I know Tkinter offers a .after function which might help. Does anyone know how to do this?

Tristan
  • 31
  • 4
  • -1: Why is this question even upvoted? Only a simple line to change the color is required, which almost anyone who's used Tkinter should know, and it should come up while searching/googling it, too. – The Amateur Coder Aug 17 '23 at 12:57
  • @TheAmateurCoder because I am new to Tkinter and couldn't find it online with what exactly I wanted. Not every one is advanced like you. – Tristan Aug 18 '23 at 14:03

1 Answers1

2

Certainly! You can modify the appearance of the button by altering its properties, such as the background color. In your specific case, you want to change the button's color to green when it's clicked to indicate that a file was successfully uploaded.

You can achieve this by using the config method to modify the button's appearance within the import_file_data function. Here's how you might do it:

import pandas as pd
import tkinter as tk
from tkinter.filedialog import askopenfilename
import CustomTkinter as ctk

def import_file_data():
    v = tk.StringVar()
    global file_data
    csv_file_path = askopenfilename()
    v.set(csv_file_path)
    file_data = pd.read_csv(csv_file_path)
    file_button.config(bg="green") # Change the button's background color to green

root = tk.Tk()

buttonx, buttony = 10, 10 # Assuming you have some values for these
file_button = ctk.CTkButton(root, text="File Upload", command=import_file_data)
file_button.grid(row=5, column=0, padx=buttonx, pady=buttony)

root.mainloop()

Note that the ability to change the button's appearance might depend on the specific implementation of CTkButton within the CustomTkinter library. If the provided code doesn't work as expected, please provide more details about the CTkButton class, and I'll do my best to assist you further!

YaBek
  • 83
  • 10