Python Forum
[Tkinter] Two text files in Text widget (python 3)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Two text files in Text widget (python 3)
#1
Hi;
I'm trying to display the contents of 02 text files in a Tkinter (Text) widget, but I didn'tt arrived.
File contents (loc1.txt):

Quote:21/06/18 -0.7
22/06/18 -0.5
06/07/18 -0.7
13/07/18 -0.6
20/07/18 -0.3
27/07/18 -0.5
10/08/18 -1.2
17/08/18 -0.5
24/08/18 -0.3
21/09/18 -0.2
28/09/18 -0.4
17/10/18 -0.1

Content of the second text file (loc2.txt):

Quote:21/06/18 -0.7
22/06/18 -0.5
06/07/18 -0.8
13/07/18 -0.7
20/07/18 -0.3
27/07/18 -0.7
10/08/18 -1.0
17/08/18 -0.5
24/08/18 -0.3
21/09/18 -0.2
28/09/18 -0.5
17/10/18 -0.3

Here is the code I made to display the contents of a text file:

sous_generalites=Frame(generalites,bg='powder blue')
sous_generalites.pack(side=BOTTOM)
 
s_generalites= Scrollbar(sous_generalites)
T_generalites= Text(sous_generalites,bg='powder blue',width=750,height=550,font=('arial',14,'bold'),padx=20)
 
s_generalites.pack(side=RIGHT, fill=Y)
T_generalites.pack(side=LEFT, fill=Y)
s_generalites.config(command=T_generalites.yview)
T_generalites.config(yscrollcommand=s_generalites.set)
 
filename =('archives/loc1.txt')
fichier = open(filename, "r")
content_generalites= fichier.read()
fichier.close()
 
T_generalites.insert(END, content_generalites)
Each of the two files has two columns (the first column is the same for both files, it represents the date)
here is the image of the desired concept:
[Image: p5pt.jpg]

thanks or help
Reply
#2
this code:
filename =('archives/loc1.txt')
fichier = open(filename, "r")
content_generalites= fichier.read()
fichier.close()
  
T_generalites.insert(END, content_generalites)
can be simplified to:
with open(filename, "r") as fichier:
    for line in fichire:
        T_generalites.insert(END, line.strip())
I don't see a second file, but just add a similar phrase for the second file

also, if you wish to intermingle data from both files, you can use:
with open(file1, 'r') as f1, open(file2, 'r') as f2:
    ...
Reply
#3
hi;
you don't see the second files because i don't knew how to do for adding tne content of two files in the same widget 'TEXT'

I try this code but it doesn't work like I want

with open('data_monitor1_axe_loc35L.txt', "r") as fichier1,open('data_monitor2_axe_loc35L.txt', "r") as fichier2:
	for line in fichier1:
		T1_paned_f7_bottom_35L_left.insert(END, line.strip())

	for line in fichier2:
		T1_paned_f7_bottom_35L_left.insert(END, line.strip())
Reply
#4
Quote:but it doesn't work like I want
and that is?
Reply
#5
I think that the problem I encountered was not well understood. for that I explain again to have a good idea about the problem : there are two text files that I want to integrate into a widgte (TEXT)
the content of two files (loc2.txt,loc2.txt)

loc1.txt:

Quote:21/06/18 -0.7
22/06/18 -0.5
06/07/18 -0.7
13/07/18 -0.6
20/07/18 -0.3
27/07/18 -0.5
10/08/18 -1.2
17/08/18 -0.5
24/08/18 -0.3
21/09/18 -0.2
28/09/18 -0.4
17/10/18 -0.1

loc2.txt:

Quote:21/06/18 -0.7
22/06/18 -0.5
06/07/18 -0.8
13/07/18 -0.7
20/07/18 -0.3
27/07/18 -0.7
10/08/18 -1.0
17/08/18 -0.5
24/08/18 -0.3
21/09/18 -0.2
28/09/18 -0.5
17/10/18 -0.3

the first column of two txt files(loc1.txt and loc2.txt)is the same (It describes date)

I want to display the contents of these two files in a widget (TEXT) in this way:

* the first column displayed in the widget (TEXT) would be the column of the date (which is already the first common column for the two text files loc1.txt, and loc2.txt)

* the second column displayed in the widget TEXT would be the second column of the text file (loc1.txt)

* the third column displayed in the widget TEXT would be the second column of the text file (loc2.txt)

here is the image of the desired concept:

[Image: p5pt.jpg]

currently, I can only display the contents of a single text file in the widget by this code :

from tkinter import *
import tkinter.ttk as ttk
from tkinter.ttk import Notebook

container=Tk()

container.geometry('450x350')
generalites=Frame(container,bg='powder blue')
generalites.pack(side=BOTTOM)
  
s_generalites= Scrollbar(generalites)
T_generalites= Text(generalites,bg='powder blue',width=350,height=350)
  
s_generalites.pack(side=RIGHT, fill=Y)
T_generalites.pack(side=LEFT, fill=Y)
s_generalites.config(command=T_generalites.yview)
T_generalites.config(yscrollcommand=s_generalites.set)
  
filename =('loc1.txt')
fichier = open(filename, "r")
content_generalites= fichier.read()
fichier.close()
  
T_generalites.insert(END, content_generalites)

container.mainloop()
I hope that I have successfully explained the problem
Reply
#6
import os

# make sure in code directory
os.chdir(os.path.dirname(__file__))

with open('loc1.txt') as loc1, open('loc2.txt') as loc2:
    head = ['Date', 'Value 1', 'Value2']
    print(f'{head[0]:15} {head[1]:10} {head[2]:10}')
    while True:
        line1 = loc1.readline()
        if len(line1) == 0:
            break
        line1 = line1.strip().split()
        line2 = loc2.readline()
        line2 = line2.strip().split()
        print(f'{line1[0]:15} {line1[1]:10} {line2[1]:10}')
results:
Output:
Date Value 1 Value2 21/06/18 -0.7 -0.7 22/06/18 -0.5 -0.5 06/07/18 -0.7 -0.8 13/07/18 -0.6 -0.7 20/07/18 -0.3 -0.3 27/07/18 -0.5 -0.7 10/08/18 -1.2 -1.0 17/08/18 -0.5 -0.5 24/08/18 -0.3 -0.3 21/09/18 -0.2 -0.2 28/09/18 -0.4 -0.5 17/10/18 -0.1 -0.3
you can add the gui code to insert into your widget
This code uses f-string (python 3.6 and newer)
Reply
#7
I try to put this code in widget tkinter but i have problem

there is my code :

import os
from tkinter import *
root=Tk()
root.geometry('450x350')

# make sure in code directory
os.chdir(os.path.dirname(__file__))
 
with open('archives/ILS_35L/loc35L/loc1.txt') as loc1, open('archives/ILS_35L/loc35L/loc2.txt') as loc2:
    head = ['Date', 'Monitor 1', 'Monitor 2']

    lb_date=Label(root,text=f'{head[0]:15}',bg='powder blue')
    lb_date.place(x=10,y=30)

    lb_monitor1=Label(root,text=f'{head[1]:10}',bg='powder blue')
    lb_monitor1.place(x=60,y=30)

    lb_monitor2=Label(root,text=f'{head[2]:10}',bg='powder blue')
    lb_monitor1.place(x=120,y=30)

    # lb=Label(root, text=f'{head[0]:15}{head[1]:10}{head[2]:10}')
    # lb.pack()
    generalites=Frame(root,bg='powder blue')
    generalites.pack(side=BOTTOM)

    s_generalites= Scrollbar(generalites)
    T_generalites= Text(generalites,bg='powder blue',width=350,height=350)
  
    s_generalites.pack(side=RIGHT, fill=Y)
    T_generalites.pack(side=LEFT, fill=Y)
    s_generalites.config(command=T_generalites.yview)
    T_generalites.config(yscrollcommand=s_generalites.set)
    while True:
        line1 = loc1.readline()
        if len(line1) == 0:
            break
        line1 = line1.strip().split()
        line2 = loc2.readline()
        line2 = line2.strip().split()		
        content_generalites=f'{line1[0]:15}{line1[1]:10}{line2[1]:10}'
        T_generalites.insert(END, content_generalites)
      
root.mainloop()
Reply
#8
(Nov-06-2018, 08:55 PM)atlass218 Wrote: but i have problem
Is there an error? What problem are you having, and why do you want us to guess what that problem is?
Reply
#9
The code indicates by Larz60 + works perfectly in console, so I tried to modify it so that it gives me the same result with widget tkinter (TEXT with scrolling)
with my last code I have this picture :

[Image: c6d3.jpg]

I wish to correct my code to have in TEXT widget of tkinter (python 3) as this result :

[Image: p153.jpg]

thanks for help
Reply
#10
Try:
content_generalites = f'{line1[0]:15} {line1[1]:10} {line2[1]:10}'.strip()
or better, remove that line altogether and replace next with:
T_generalites.insert(END, f'{line1[0]:15} {line1[1]:10} {line2[1]:10}'.strip())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Transparent window background, but not text on it muzicman0 7 2,876 Feb-02-2024, 01:28 AM
Last Post: Joically
  [Kivy] Create a function to store text fields and drop downs selection in KivyMD floxia 0 1,671 Dec-18-2022, 04:34 AM
Last Post: floxia
  [Tkinter] Updating tkinter text BliepMonster 5 6,045 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 14,798 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,827 Nov-04-2022, 09:04 PM
Last Post: malonn
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 1,523 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 990 Oct-27-2022, 09:29 PM
Last Post: random_nick
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,921 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,915 Jun-26-2022, 06:26 PM
Last Post: menator01
  [PyQt] Determining the format attributes on text in a QTextEdit object DrakeSoft 7 3,573 Apr-18-2022, 06:40 PM
Last Post: DrakeSoft

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020