-1

Very simple, don't be impressed by the size of the code.

I want to do something very simple (well, not for me, since I'm asking for help here) put the location of the open file in the red square on the screen:

Screen


import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter import messagebox

def OpenFile_AntiDuplicate():

    global antiduplicate_file
    mainframe = tk.Frame(bg='#1c2028')

    antiduplicate_file = askopenfilename(initialdir="/",
                        filetypes =(("Text file", "*.txt"),("All files","*.*")),
                        title = "Open text file"
                        )

    fichier_dir = tk.Label(mainframe, text=antiduplicate_file).pack()

    try:

        with open(antiduplicate_file,'r') as UseFile:
            print(antiduplicate_file)

    except:

        print("Non-existent file")


def RUN_AntiDuplicate():

    try:
        with open(antiduplicate_file,'r') as UseFile:
            print(antiduplicate_file)

    except:

        error1 = tk.messagebox.showerror("ERROR", "No files exist!")

#----------------------------------------------------------

class HoverButton(tk.Button):
    def __init__(self, master, **kw):
        tk.Button.__init__(self,master=master,**kw)
        self.defaultBackground = self["background"]
        self.bind("<Enter>", self.on_enter)
        self.bind("<Leave>", self.on_leave)

    def on_enter(self, e):
        self['background'] = self['activebackground']

    def on_leave(self, e):
        self['background'] = self.defaultBackground

#----------------------------------------------------------

def Anti_Duplicate():

   mainframe = tk.Frame(bg='#1c2028')
   mainframe.grid(row=0, column=0, sticky='nsew')

   bouton_1 = HoverButton(mainframe, font=("Arial", 10), text="Back",
                          background='#000000', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#CF3411',
                          relief='ridge', command=mainframe.destroy)

   bouton_1.place(x=520, y=300)

   open_button = HoverButton(mainframe, font=("Arial", 10), text="Open File..",
                             background='#000000', fg='white', borderwidth=2,
                             activebackground='#202124', activeforeground='#1195cf',
                             relief='ridge', command = OpenFile_AntiDuplicate)
   open_button.place(x=284.3, y=200, anchor='n')

   run_button = HoverButton(mainframe, font=("Arial", 20), text="RUN",
                             background='#000000', fg='white', borderwidth=2,
                             activebackground='#202124', activeforeground='#11CF6D',
                             relief='ridge', command = RUN_AntiDuplicate)
   run_button.place(x=50, y=330, anchor='s')


   bouton_2 = tk.Button(mainframe, font=("Arial", 10),
                        text="The purpose of this tool is to remove duplicate lines from a text file.",
                        background='#202124', fg='#1195cf', borderwidth=2,
                        activebackground= '#202124', activeforeground='#1195cf', relief='sunken')
   bouton_2.place(relx=.5, y=50, anchor='n')

   bouton_1 = tk.Button(mainframe, font=("Arial", 15), text="Anti-Duplicate",
                        background='#202124', fg='#1195cf',  borderwidth=2,
                        activebackground='#202124', activeforeground='#1195cf', relief='sunken')
   bouton_1.pack(side= "top", padx= 5, pady=5, ipadx= 30, anchor="n")


#----------------------------------------------------------

def main_menu():

   root = tk.Tk()
   screenn_x = int(root.winfo_screenwidth())
   root.config(background='#1c2028')
   screenn_y = int(root.winfo_screenheight()) 
   root.title("ComboKit v0.0.1")
   root.minsize(570, 340)
   root.resizable(0,0)

   windowss_x = 570
   windowss_y = 340

   possX = (screenn_x // 2) - (windowss_x // 2)
   possY = (screenn_y // 2) - (windowss_y // 2)

   geoo = "{}x{}+{}+{}".format(windowss_x, windowss_y, possX, possY)
   root.geometry(geoo)

   root.rowconfigure(0, weight=1)
   root.columnconfigure(0, weight=1)

   mainframe = tk.Frame(root, bg='#1c2028')
   mainframe.grid(row=0, column=0, sticky='n')

   main_fusion_bouton = HoverButton(mainframe, font=("Arial", 15), text="Fusion",
                          background='#000000', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=None)
   main_fusion_bouton.pack(side= "left", padx= 5, pady=5, ipadx= 10, anchor="n")

   main_antiduplicate_bouton = HoverButton(mainframe, font=("Arial", 15), text="Anti-Duplicate",
                          background='#000000', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=Anti_Duplicate)

   main_antiduplicate_bouton.pack(side= "left", padx= 5, pady=5, ipadx= 30)

   main_split_button = HoverButton(mainframe, font=("Arial", 15), text="Split",
                          background='#000000', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=None)

   main_split_button.pack(side= "left", padx= 5, pady=5, ipadx= 19, anchor="n")

   root.mainloop()

main_menu()

So here's my code allows you to use 3 tools:

"Split" / "Anti-Duplicate" / "Merge"

At the moment I'm working on the "Anti-Duplicate" so it's on this Frame() where the text should be displayed.

I've already done everything, even the button to open the file explorer, but for the moment the location of the file is only displayed in the cmd.

Thank you very much!

  • 1
    This is an awful lot of code. Please try to condense it down to a [mcve]. – Bryan Oakley Mar 12 '20 at 22:56
  • As an aside, using a bare `except:` like that is bad practice, see https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except. – AMC Mar 13 '20 at 00:33

1 Answers1

1

The location of the file does not show up because you created a new frame to hold the label fichier_dir inside OpenFile_AntiDuplicate() and you did not call any layout function on the frame, so the frame will not be shown.

Better create the label fichier_dir inside Anti_Duplicate() and pass it to OpenFile_AntiDuplicate() function:

def Anti_Duplicate():
   ...    
   fichier_dir = tk.Label(mainframe, bg='#1c2028', fg='white')
   fichier_dir.place(relx=0.5, y=170, anchor='n')

   open_button = HoverButton(mainframe, font=("Arial", 10), text="Open File..",
                             background='#000000', fg='white', borderwidth=2,
                             activebackground='#202124', activeforeground='#1195cf',
                             relief='ridge', command = lambda: OpenFile_AntiDuplicate(fichier_dir))
   ...

And update OpenFile_AntiDuplicate(...):

def OpenFile_AntiDuplicate(fichier_dir):
    global antiduplicate_file

    antiduplicate_file = askopenfilename(initialdir="/",
                         filetypes =(("Text file", "*.txt"),("All files","*.*")),
                         title = "Open text file"
                         )

    fichier_dir['text'] = antiduplicate_file
    ... 
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Perfect, exactly what I wanted. Thanks for the explanation, I finally understood! –  Mar 13 '20 at 03:39