简体   繁体   English

在文本小部件tkinter中显示内容文件并进行更新

[英]display content file in Text widget tkinter with update

I try to made Text widgte in tkinter (python 3.6) that display the content of the text file.the problem for me is to have update of the Text widget automatically each 01 second. 我尝试在tkinter(python 3.6)中使Text widgte显示文本文件的内容。我的问题是每隔01秒自动更新Text小部件。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

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

root=Tk()
root.geometry('540x420')
root.title('Text update')

ILS=ttk.Notebook(root,width=520,height=450)

releves_loc35R = Frame(ILS,bg="powder blue")
ILS.add(releves_loc35R, text = 'releves')

data_loc35R = Frame(ILS,bg="powder blue")
ILS.add(data_loc35R, text = 'data')

ent_mon=StringVar()

#pour stocker les valeurs des mesures du LOC 35R
def archiver():

    mes_archives={
                  "jour" : time.strftime('%d/%m/%y  à  %H:%M:%S', time.localtime()),                
                  "lecture_monitor": ent_mon.get(),                               
                  } 
    textInsert=("\n"+str(mes_archives["jour"])+"\n"+"\nddm lue avant la correction au Monitor 1 :"+str(mes_archives["lecture_monitor"])+"\n\n=======================================\n")

    mon_fichier=open("archives/file.txt", "r")
    #ouverture du fichier texte (data_loc35R)
    text=mon_fichier.read()
    mon_fichier.close()

    fileW = open("archives/file.txt", "w")
    fileW.write(textInsert+text)
    fileW.close()

canvas=Canvas(releves_loc35R)
canvas.place(x=4,y=70)
monitor = LabelFrame(canvas, text="Lecture Monitor 1",padx=5, pady=5,font=('arial',12,'bold'))
monitor.pack(padx=6, pady=6)

entry_monitor = Entry(monitor,font=('arial',10,'bold'),textvariable=ent_mon)
entry_monitor.pack(pady=5)

#bouton d'archivage de la correction
bt_archiver=Button(releves_loc35R,text='Archiver',command=archiver)
bt_archiver.pack(side=LEFT,anchor=SW)

##########################
def affichage_update():
    with open("archives/file.txt","r") as f:
        data = f.read()
        s = Scrollbar(data_loc35R)
        T = Text(data_loc35R)
        s.pack(side=RIGHT, fill=Y)
        T.pack(side=LEFT, fill=Y)
        s.config(command=T.yview)
        T.config(bg='powder blue',font=('arial',14,'bold'),pady=14, yscrollcommand=s.set)
        T.insert(END,data)
    T.after(1000,affichage_update)

affichage_update()

ILS.pack(expand=1, fill='both', padx=5, pady=5)

root.mainloop()

when I introduce information via the entry interface of the labelframe. 当我通过labelframe的输入界面介绍信息时。 they are not displayed at the same time in Text widget, but are stored in the text file i don't knew why the update fuction that I created is not worked I want help to resolve this problem 它们不会在“文本”窗口小部件中同时显示,但是存储在文本文件中,我不知道为什么我创建的更新功能无法正常工作,我需要帮助来解决此问题

You are creating a new Text() widget every time affichage_update() runs, and for some reason the first one stays on top. 每次运行affichage_update() ,您都在创建一个新的Text()小部件,由于某种原因,第一个仍停留在顶部。

Instead create only one Text() widget and change the contents in the function: 而是仅创建一个Text()小部件并更改函数中的内容:

# Building the data tab text widget
s = Scrollbar(data_loc35R)
T = Text(data_loc35R)
s.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
s.config(command=T.yview)
T.config(bg='powder blue', font=('arial',14,'bold'),
         pady=14, yscrollcommand=s.set)

##########################
def affichage_update():
    with open("file.txt","r") as f:
        data = f.read()
        T.delete('1.0', END)  # Remove previous content 
        T.insert(END,data)    # Insert text from file
    T.after(1000,affichage_update)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM