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 8 7,299 Feb-13-2025, 06:16 AM
Last Post: elonnmusk
  Trying to update label text using a grid button. Edward_ 7 1,774 Dec-18-2024, 03:05 AM
Last Post: Edward_
  [PyQt] Rotake rect, image or text at its center LavaCreeperKing 8 2,563 Oct-24-2024, 09:42 PM
Last Post: deanhystad
  [Tkinter] Text input OK on Windows, not working on linux Ota 3 1,267 Sep-19-2024, 12:02 AM
Last Post: Ota
  popup, text input with two readable buttons ethompson 7 2,085 Aug-28-2024, 03:40 AM
Last Post: deanhystad
  [PyQt] Populate QComboBox with "text" and "userData" from database. carecavoador 0 1,556 Jun-19-2024, 02:01 PM
Last Post: carecavoador
  update text variable on label with keypress knoxvilles_joker 5 7,752 May-31-2024, 02:09 PM
Last Post: menator01
  Button to +1 in text box everytime it's clicked martyloo 1 1,426 May-01-2024, 02:32 PM
Last Post: Axel_Erfurt
  [Kivy] Create a function to store text fields and drop downs selection in KivyMD floxia 0 2,804 Dec-18-2022, 04:34 AM
Last Post: floxia
  [Tkinter] Updating tkinter text BliepMonster 5 12,135 Nov-28-2022, 01:42 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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