Home > OS >  Tkinter: print (.txt) filepath to text widget AND (.txt) file content to scrolledtext widget in the
Tkinter: print (.txt) filepath to text widget AND (.txt) file content to scrolledtext widget in the

Time:08-28

As a follow-up question to this: the closest I can get, so far...

CodePudding user response:

For second part of your question

The problem with the incomplete path appears because of wrap setting. To solve this just change this string:

mypathtext = Text(mylabelframe, width=10, height=1)

A feature with backslashes and forward slashes you can fix by using .replace() method.

CodePudding user response:

How about this:

from tkinter import *
from tkinter import filedialog
from tkinter import scrolledtext
import os

def browsefunc():
    global content
    filename = filedialog.askopenfilename()
    infile = open(filename, 'r')
    content = infile.read()
    pathadd = filename

    pathtext.delete(0.0, END)
    pathtext.insert(0.0, pathadd)

    contenttext.delete(0.0, END)
    contenttext.insert(0.0, content)

w = Tk()
w.geometry('600x370')

lb1 = Label(text="Select text file")
lb1.place(x=0, y=0)

bt1 = Button(text="Process", command=browsefunc)
bt1.place(x=0, y=40)

pathtext = Text()
pathtext.place(x=0, y=85, width=450, height=20)

contenttext = scrolledtext.ScrolledText()
contenttext.place(x=0, y=120, width=450)

After reading the file your data will be inside content variable.

CodePudding user response:

After looking at the debugger, and seeing that the TextIO variable "fork", comprises two parts: "<_io.Textwrapper" and "name = C:\blah\blah\blah"

enter image description here

I got it to 99% work like this:

#import os
#import io
from tkinter import *
#from tkinter import ttk
from tkinter import filedialog
from tkinter import scrolledtext
import tkinter as tk
from pathlib import Path
from typing import TextIO


def forkscroll():
    mylabelframe = tk.LabelFrame(myframe, text='My Long Text:').pack()
    scroll_w = 30
    scroll_h = 10
    myscrolledtext = scrolledtext.ScrolledText(mylabelframe, width=scroll_w, height=scroll_h, wrap=tk.WORD)
    myscrolledtext.vbar.config(width=20)
    myscrolledtext.grid(sticky='news', row=6, column=0, rowspan=1, columnspan=1, padx=5, pady=5)

# Open a dialogue; select and load a text file; read the text file directly into the scrolledtext widget (box)
    fork: TextIO = open(filedialog.askopenfilename(), 'r') 
    myscrolledtext.insert(tk.END, fork.read()) ### This reads the content of "fork" into the scrolledtext widget

    forkname = fork.name # works
    mypathtext = Text(mylabelframe, width=10, height=1, wrap=tk.WORD)
    mypathtext.grid(sticky='news', row=5, column=0, rowspan=1, columnspan=1, padx=5, pady=5)
    mypathtext.insert(tk.END, forkname)  # works

#GUI

root = Tk()
root.title('Root Title')
root.geometry('600x300')

myframe = tk.Frame(root, width=300, height=300)
myframe.winfo_toplevel().title('MyFrame Title')
myframe.grid(column=0, row=0, sticky='news')

myforkpath = StringVar()

f1 = Frame(myframe, width=900, height=150) #file1
f1.pack(fill=X, side =TOP)
f2 = Frame(myframe, width=900, height=150) #file2
f2.pack(fill=X, side=BOTTOM)

Label(f1, text="Select text file").grid(row=0, column=0, sticky='e')  # select file button label
run_button = Button(myframe, text="Process", command=forkscroll).pack()
Label(f2, text="put the content of the file in the text scroll box below").grid(row=1, column=0, sticky='s')

myframe.mainloop()

...with one problem... that 1%

My laptop is named "Crappy Laptop" (not really, but it has a space in the name in that part of the path) and so the path in name that is printed to the Text widget field is:

"C://Users/Crappy"

...I don't get the whole path... which should be "C://Users/Crappy Laptop/Desktop/My/Python/Code/mylongtextfile.txt"

in fact I get forward slashes, and not backslashes... in the Pycharm console window it displays the paths with slashes in each direction! Like this: C:\......\python.exe C://.../.../myfile.py

How to handles the space?

Well I've done this so far...

    forkname = fork.name # works
# fix the "path with spaces problem"
    newforkname1 = forkname.replace(' ', '_')
    print(newforkname1)
    newforkname2 = newforkname1.replace('/', '\\')
    print(newforkname2)

    mypathtext = Text(mylabelframe, width=10, height=1, wrap=tk.WORD)
    mypathtext.grid(sticky='news', row=5, column=0, rowspan=1, columnspan=1, padx=5, pady=5)
    #mypathtext.insert(tk.END, forkname)  # works
    mypathtext.insert(tk.END, newforkname2)  # works

haven't sussed out how to display a space instead of an underscore yet.

This doesn't quite do it...

    newforkname1 = forkname.replace(' ', ' '.join(forkname))
  • Related