Python Forum

Full Version: Two text files in Text widget (python 3)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
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
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')
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)
Please use a bit of initiative.
How do you suppose you would add that?
I want to have the result like that:
[Image: p153.jpg]
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 :)
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?
(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.
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
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
Pages: 1 2 3