How do I get my labels in the open_file methods to stay and not disappear?

Viewed 36

In my code I'm very confused why on button press my labels disappear after the next button press. What I'm trying to do is get the file path + file name to stay on the interface after button click

The first part goes over the open_file commands that will operate on button click. The next part goes over the file labels, and the final part are the actual buttons.

# Import the required libraries
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import tkinter.filedialog as fd
import pandas as pd
import csv
import os

# Create an instance of tkinter frame or window
win = Tk()

# Set the geometry of tkinter frame
win.geometry("700x350")

# Choose a Trace File window
def open_file():
    file = fd.askopenfilename(parent=win, title='Choose a Trace File')
    print(win.splitlist(file))
# Display file path
    file_label.config(text=file)
    file_label.place(relx = 0.59, rely = 0.77)

    # Display file name
    fullpath = file
    just_the_name = os.path.basename(fullpath)
    file_label2.config(text=just_the_name)
    file_label2.place(relx = 0.7, rely = 0.8)
    
# Choose a Supplementary File window
def open_file2():
   file = fd.askopenfilename(parent=win, title='Choose a Supplementary File')
   print(win.splitlist(file))
   file_label.config(text=file)
   file_label.place(relx = 0.59, rely = 0.77)
      
   fullpath = file
   just_the_name = os.path.basename(fullpath)
   file_label2.config(text=just_the_name)
   file_label2.place(relx = 0.7, rely = 0.8)

# File Path and File Name labels
file_label = Label(win, text="")
file_label.pack()
file_label2 = Label(win, text="")
file_label2.pack()

# Add a Button Widget
# Add file path and file name next to button when clicked
btn1 = ttk.Button(win, text="Select the Trace File", command=open_file).pack()
btn2 = ttk.Button(win, text="Select the Supplementary File", command=open_file2).pack()
btn3 = ttk.Button(win, text="Select the Output File", command=convert_file).pack()

win.mainloop()

I would appreciate any help in trying to figure out how to get these labels to stick.

1 Answers

Both buttons use the same labels to display path - so second button replace previous value.

If you want to keep text then every button needs own labels.

import tkinter as tk  # PEP8: `import *` is not preferred
from tkinter import ttk
import tkinter.filedialog as fd
import os

# --- functions ---

def open_file1():
    filename = fd.askopenfilename(parent=win, title='Choose a Trace File')

    if filename:  # check if pressed button `Cancel`
        full_path = os.path.basename(filename)

        file1_name.config(text=filename)
        file1_full_path.config(text=full_path)
    
def open_file2():
    filename = fd.askopenfilename(parent=win, title='Choose a Supplementary File')

    if filename:  # check if pressed button `Cancel`
        full_path = os.path.basename(filename)

        file2_name.config(text=filename)
        file2_full_path.config(text=full_path)

# --- main ---

win = tk.Tk()

#win.geometry("700x350")

# --- labels for first button ---

file1_name = tk.Label(win, text="")
file1_name.pack()

file1_full_path = tk.Label(win, text="")
file1_full_path.pack()

# --- labels for second button ---

file2_name = tk.Label(win, text="")
file2_name.pack()

file2_full_path = tk.Label(win, text="")
file2_full_path.pack()

# --- buttons ---

ttk.Button(win, text="Select the Trace File",         command=open_file1).pack(fill='x')
ttk.Button(win, text="Select the Supplementary File", command=open_file2).pack(fill='x')

win.mainloop()

enter image description here


Eventually you should get previous text from label, add new text to this text, and set new text in label.

    previous_text = file_name.cget("text")
    new_text = previous_text + filename + '\n'
    file_name.config(text=new_text)
    
    previous_text = file_full_path.cget("text")
    new_text = previous_text + full_path + '\n'
    file_full_path.config(text=new_text)

or shorter

    file_name["text"] += filename + '\n'
    file_full_path["text"] += full_path + '\n'

But this way it will display all selected files - and always new file at the end (and it doesn't matter which button you use) - so it could be more useful as history of all selected files.

import os
import tkinter as tk  # PEP8: `import *` is not preferred
from tkinter import ttk
import tkinter.filedialog as fd

# --- functions ---

def open_file1():
    filename = fd.askopenfilename(parent=win, title='Choose a Trace File')

    if filename:
        full_path = os.path.basename(filename)

        previous_text = file_name.cget("text")
        new_text = previous_text + filename + '\n'
        file_name.config(text=new_text)
        
        previous_text = file_full_path.cget("text")
        new_text = previous_text + full_path + '\n'
        file_full_path.config(text=new_text)

        # shorter        
        #file_name["text"] += filename + '\n'
        #file_full_path["text"] += full_path + '\n'
    
# Choose a Supplementary File window
def open_file2():
    filename = fd.askopenfilename(parent=win, title='Choose a Supplementary File')

    if filename:
        full_path = os.path.basename(filename)

        previous_text = file_name.cget("text")
        new_text = previous_text + filename + '\n'
        file_name.config(text=new_text)
        
        previous_text = file_full_path.cget("text")
        new_text = previous_text + full_path + '\n'
        file_full_path.config(text=new_text)

        # shorter        
        #file_name["text"] += filename + '\n'
        #file_full_path["text"] += full_path + '\n'

# --- main ---

win = tk.Tk()

#win.geometry("700x350")

# --- labels ---

file_name = tk.Label(win, text="")
file_name.pack()

file_full_path = tk.Label(win, text="")
file_full_path.pack()

# --- buttons ---

ttk.Button(win, text="Select the Trace File",         command=open_file1).pack(fill='x')
ttk.Button(win, text="Select the Supplementary File", command=open_file2).pack(fill='x')

win.mainloop()

enter image description here

Related