1

I have a Python script that creates three Tkinter windows. I want to add an image as the background and display all other widgets on top of it. Can anyone suggest how to achieve this in Tkinter?

import tkinter as tk, pandas as pd, io
from tkinter import ttk, filedialog

class DataWizard:
    def __init__(self, root):
        self.root = root
        self.root.title("Data Wizard: Automated Data Preprocessing")
        self.root.geometry("800x600")

        # Top layout with title
        self.title_label = tk.Label(self.root, text="Data Wizard: Automated Data Preprocessing", font=("Arial", 18))
        self.title_label.pack(pady=20)

        # Middle layout with table frame
        self.table_frame = tk.Frame(self.root, bd=2, relief=tk.GROOVE)
        self.table_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)

        # Create a treeview widget to display table
        self.tree = ttk.Treeview(self.table_frame, selectmode='browse')
        self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        # Add scrollbar to the treeview widget
        scrollbar = ttk.Scrollbar(self.table_frame, orient="vertical", command=self.tree.yview)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        self.tree.configure(yscrollcommand=scrollbar.set)

        # Bottom layout with Upload CSV file label and button
        self.bottom_frame = tk.Frame(self.root, bd=2, relief=tk.GROOVE)
        self.bottom_frame.pack(fill=tk.X, padx=20, pady=10)

        self.upload_label = tk.Label(self.bottom_frame, text="Upload CSV file", font=("Arial", 12))
        self.upload_label.pack(side=tk.LEFT, padx=10, pady=10)

        self.upload_button = tk.Button(self.bottom_frame, text="Browse", command=self.upload_csv)
        self.upload_button.pack(side=tk.RIGHT, padx=10, pady=10)

        
        self.new_frame = tk.Frame(self.root, bd=0, relief=tk.GROOVE)
        self.new_frame.pack(fill=tk.X, padx=20, pady=10)
        
        # Add button to open a new window
        self.new_window_button = tk.Button(self.new_frame, text="Next", command=self.open_new_window)
        self.new_window_button.pack(side=tk.RIGHT, padx=10, pady=10)

    def upload_csv(self):
        # Open file dialog to select a CSV file
        file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])

        if file_path:
            # Read CSV file as pandas dataframe
            self.df = pd.read_csv(file_path)

            # Set the columns of the treeview widget based on the dataframe columns
            self.tree["columns"] = list(self.df.columns)

            # Set column headings and widths based on the dataframe
            for col in self.df.columns:
                self.tree.heading(col, text=col.title())
                self.tree.column(col, width=len(str(self.df[col].max())), anchor = tk.CENTER)

            # Insert data into treeview widget
            for i, row in self.df.iterrows():
                self.tree.insert("", "end", values=list(row))

            # Change the upload label to the filename
            self.upload_label.config(text=file_path)

            # Add horizontal scrollbar to the treeview widget
            horizontal_scrollbar = ttk.Scrollbar(self.table_frame, orient="horizontal", command=self.tree.xview)
            horizontal_scrollbar.pack(side=tk.BOTTOM, fill=tk.X)
            self.tree.configure(xscrollcommand=horizontal_scrollbar.set)

    def open_new_window(self):
        new_window = tk.Toplevel()
        new_window.title("New Window")
        
        # Set the window size to full screen
        # Set the window size to full screen
        new_window.state('zoomed')

        # Create a text widget to display the info
        text_widget = tk.Text(new_window, bd=2, relief=tk.GROOVE, width=70, height=20)
        text_widget.grid(row=0, column=0, padx=15, pady=10, sticky="nw")

        # Redirect the output of df.info() to the text widget
        with io.StringIO() as stream:
            self.df.info(buf=stream)
            output = stream.getvalue()
            text_widget.insert(tk.END, output)
            
        # Make the text widget uneditable
        text_widget.config(state="disabled")
                
        # Create a text widget to display the info
        text_widget2 = tk.Text(new_window, bd=2, relief=tk.GROOVE, width=70, height=22)
        text_widget2.grid(row=1, column=0, padx=15, pady=5, sticky="sw")
             
        f = tk.Frame(new_window)
        f.grid(row=0, column=1, padx=15, pady=10, rowspan=2, sticky="nsew")

        text_widget3 = tk.Text(f, bd=2, relief=tk.GROOVE, width=80, height=3)
        inputs_frame = tk.Frame(f, bd=2, relief=tk.GROOVE, pady=20, width=text_widget3["width"])
        text_widget4 = tk.Text(f, bd=2, relief=tk.GROOVE, width=80, height=24)
        preprocess_button = tk.Button(f, relief=tk.GROOVE, text="Perform Preprocessing", width=10, command=self.preprocess_data)

        text_widget3.pack(side="top", fill="x")
        inputs_frame.pack(side="top", fill="x", pady=10)
        text_widget4.pack(side="top", fill="x", pady=10)
        preprocess_button.pack(side="top", fill="x", pady=10)

        # Add labels and text inputs to the inputs frame
        label1 = tk.Label(inputs_frame, text="Label 1:")
        label1.grid(row=0, column=0, padx=5, pady=5, sticky="w")
        input1 = tk.Entry(inputs_frame)
        input1.grid(row=0, column=1, padx=5, pady=5, sticky="w")
        
        label2 = tk.Label(inputs_frame, text="Label 2:")
        label2.grid(row=1, column=0, padx=5, pady=5, sticky="w")
        input2 = tk.Entry(inputs_frame)
        input2.grid(row=1, column=1, padx=5, pady=5, sticky="w")
        
        add_button = tk.Button(inputs_frame, text="Add", width=10)        
        add_button.grid(row=2, column=0, columnspan=2, pady=10)
        
    def preprocess_data(open_new_window):
        preprocessed_window = tk.Toplevel()
        preprocessed_window.title("Preprocessed Data")
        preprocessed_window.geometry("800x600")
               
        #Use tocsv methods to convert preprocessed dataframe of pandas into a csv file and then also seek how to make it downloadable
                
if __name__ == "__main__":
    root = tk.Tk()
    app = DataWizard(root)
    root.mainloop()

The output of the code is: Main Window TopLevel Window named new_window

I have tried putting the canvas, then the image in it, ad then other widgets in it, but that won't also work.

6
  • 2
    Does this answer your question? How to use an image for the background in tkinter?
    – Marijn
    Apr 1 at 10:46
  • Which window main or toplevel? Apr 1 at 12:37
  • @Marijn I tried the label method as well, but even that won't work! Apr 1 at 15:31
  • 1
    @toyotaSupra All the windows, including the one main window and other two TopLevel Windows! Apr 1 at 15:31
  • 2
    Note that tkinter widgets don't support transparent background, so those frames will cover the background image.
    – acw1668
    Apr 2 at 1:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.