I have been trying to make "Drag and drop" and "select a file" butoon in one tkinter window. I have two separate codes for both the functions. I need to combine them in a single window.
I need to make a single window where the drag and drop button and select a file button are below one another.
THIS IS THE CODE FOR DRAG AND DROP
from tkinter import *
from tkinterdnd2 import *
def path_listbox(event):
listbox.insert("end", event.data)
window = TkinterDnD.Tk()
window.title('Delftstack')
window.geometry('400x300')
window.config(bg='blue')
frame = Frame(window)
frame.pack()
window.wm_attributes('-transparentcolor', '#ab23ff')
#Create a Label
Label(window, text= "Drag and drop", font= ('Helvetica 18'), bg= '#ab23ff').pack(ipadx= 50, ipady=50, padx= 20)
listbox = Listbox(
frame,
width=40,
height=10,
selectmode=SINGLE,
)
listbox.pack(fill=X, side=LEFT)
listbox.drop_target_register(DND_FILES)
listbox.dnd_bind('<<Drop>>', path_listbox)
scrolbar= Scrollbar(
frame,
orient=VERTICAL
)
scrolbar.pack(side=RIGHT, fill=Y)
# displays the content in listbox
listbox.configure(yscrollcommand=scrolbar.set)
# view the content vertically using scrollbar
scrolbar.config(command=listbox.yview)
window.mainloop()
THIS IS THE CODE FOR SELECT A FILE.
# Import the required Libraries
from tkinter import *
from tkinter import ttk, filedialog
from tkinter.filedialog import askopenfile
# Create an instance of tkinter frame
window = Tk()
# Set the geometry of tkinter frame
window.geometry("700x350")
def open_file():
file = filedialog.askopenfile(mode='r', filetypes=[('Python Files', '*.py')])
if file:
content = file.read()
file.close()
print("%d characters in this file" % len(content))
# Add a Label widget
label = Label(window, text="Click the Button to browse the Files", font=('Georgia 13'))
label.pack(pady=10)
# Create a Button
ttk.Button(window, text="Browse", command=open_file).pack(pady=20)
window.mainloop()
PLEASE HELP HOW DO I MERGE IN ONE WINDOW?
I tried merging the two but I need it one window.
The two button should be below one another.
DRAG AND DROP
OR
SELECT A FILE