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)
#11
after correction of the preceding 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()		
        T_generalites.insert(END, f'{line1[0]:15} {line1[1]:10} {line2[1]:10}'.strip())
      
root.mainloop()
[Image: c6d3.jpg]

I try it but it gives me the same result
Reply
#12
OK, I see now that the problem is a missing line terminator, so change:
T_generalites.insert(END, f'{line1[0]:15} {line1[1]:10} {line2[1]:10}'.strip())
to:
T_generalites.insert(END, f'{line1[0]:15} {line1[1]:10} {line2[1]:10}\n')
Reply
#13
the content display has improved, but the header is not displayed

[Image: nb59.jpg]

I wonder why the header did not show up. and yet I created the 'Label' widgets as follows in the previous code:

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)
Reply
#14
Please use a bit of initiative.
How do you suppose you would add that?
Reply
#15
I want to have the result like that:
[Image: p153.jpg]
Reply
#16
1) You have text that you're displaying.
2) You have more text that you'd like to display.

Seems like you can solve that issue, if you try just a little more :)
Reply
#17
I already indicated the contents of the header like this:
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)
but it's not displayed
I don't knew why?
Reply
#18
(Nov-07-2018, 07:27 PM)atlass218 Wrote: but it's not displayed
I don't knew why?

It is displayed. But then you put a big text box on top of it.
Don't mix different widget placement types. If you're going to .pack(), then pack. If you're going to .place(), then place. If you're going to .grid() then grid. But don't mix and match.
Reply
#19
I modify some lines of my code like this :

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

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

generalites_top=Frame(root,bg='powder blue')
generalites_top.pack(side=TOP)

generalites_bottom=Frame(root,bg='powder blue')
generalites_bottom.pack(side=BOTTOM)

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(generalites_top,text=f'{head[0]:15}',bg='powder blue')
    lb_date.pack(side=LEFT)

    lb_monitor1=Label(generalites_top,text=f'{head[1]:10}',bg='powder blue')
    lb_monitor1.pack(side=LEFT)

    lb_monitor2=Label(generalites_top,text=f'{head[2]:10}',bg='powder blue')
    lb_monitor2.pack(side=LEFT)

    # lb=Label(root, text=f'{head[0]:15}{head[1]:10}{head[2]:10}')
    # lb.pack()

    s_generalites_bottom= Scrollbar(generalites_bottom)
    T_generalites_bottom= Text(generalites_bottom,bg='powder blue',width=350,height=350)
  
    s_generalites_bottom.pack(side=RIGHT, fill=Y)
    T_generalites_bottom.pack(side=LEFT, fill=Y)
    s_generalites_bottom.config(command=T_generalites_bottom.yview)
    T_generalites_bottom.config(yscrollcommand=s_generalites_bottom.set)
    while True:
        line1 = loc1.readline()
        if len(line1) == 0:
            break
        line1 = line1.strip().split()
        line2 = loc2.readline()
        line2 = line2.strip().split()
        T_generalites_bottom.insert(END, f'{line1[0]:15} {line1[1]:10} {line2[1]:10}\n')
      
      
root.mainloop()
the result is like that :
[Image: uofi.jpg]
unfortunately, I have to do some editing to get the desired result
Reply
#20
I corrected the code like that :

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

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

generalites_top=Frame(root,bg='powder blue',width=450)
generalites_top.pack(side=TOP,fill=X)

generalites_bottom=Frame(root,bg='powder blue')
generalites_bottom.pack(side=BOTTOM)


head = ['Date', 'Monitor 1', 'Monitor 2']

lb_date=Label(generalites_top,text=f'{head[0]:15}',bg='powder blue',font=('arial',14,'bold'))
lb_date.pack(side=LEFT,anchor=NW)

lb_monitor1=Label(generalites_top,text=f'{head[1]:10}',bg='powder blue',font=('arial',14,'bold'))
lb_monitor1.pack(padx=5, side=LEFT)

lb_monitor2=Label(generalites_top,text=f'{head[2]:10}',bg='powder blue',font=('arial',14,'bold'))
lb_monitor2.pack(padx=30, side=LEFT)

with open('archives/ILS_35L/loc35L/loc1.txt') as loc1, open('archives/ILS_35L/loc35L/loc2.txt') as loc2:
    

   

    s_generalites_bottom= Scrollbar(generalites_bottom)
    T_generalites_bottom= Text(generalites_bottom,bg='powder blue',width=450,height=350,font=('arial',14,'bold'))
  
    s_generalites_bottom.pack(side=RIGHT, fill=Y)
    T_generalites_bottom.pack(side=LEFT, fill=Y)
    s_generalites_bottom.config(command=T_generalites_bottom.yview)
    T_generalites_bottom.config(yscrollcommand=s_generalites_bottom.set)
    while True:
        line1 = loc1.readline()
        if len(line1) == 0:
            break
        line1 = line1.strip().split()
        line2 = loc2.readline()
        line2 = line2.strip().split()
        T_generalites_bottom.insert(END, f'{line1[0]:15}\t {line1[1]:10}\t\t {line2[1]:10}\n')
      
      
root.mainloop()
and I got this result:

[Image: 4y0z.jpg]

Apparently that's what I'm looking for, but if there are other improvements to perform on this code it would be better for me
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Transparent window background, but not text on it muzicman0 7 2,889 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,673 Dec-18-2022, 04:34 AM
Last Post: floxia
  [Tkinter] Updating tkinter text BliepMonster 5 6,071 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 14,815 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,830 Nov-04-2022, 09:04 PM
Last Post: malonn
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 1,529 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 993 Oct-27-2022, 09:29 PM
Last Post: random_nick
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,926 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,934 Jun-26-2022, 06:26 PM
Last Post: menator01
  [PyQt] Determining the format attributes on text in a QTextEdit object DrakeSoft 7 3,585 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