为什么textvariable tkinter确实写了我的文件,

时间:2019-04-04 21:40:41

标签: python tkinter

我正在做一个小的脚本,该脚本从文件读取并在屏幕上显示您已阅读的内容,如果文件被修改,则Label被修改。很好,该应用程序对我而言可以很好地用作脚本,但是当我在主程序中将其作为线程启动时,它可以正常工作,但标签不会将它们编写出来。 你知道会是什么吗?谢谢

import _thread
from tkinter import *
from time import sleep

def AccionRecomendada(name, read):
    fin = False
    while not fin:
        archivo = open(name,"r")
        lineas = archivo.readlines()
        if len(lineas) > 1:
            recomendada = lineas[-2].replace("\n","")
        elif len(lineas) == 1:
            recomendada = lineas[0]
        else:
            recomendada = "Esperando"
        read.set(recomendada)
        sleep(0.1)


def AccionRead(name, read, dato):
    fin = False
    while not fin:
        archivo = open(name,"r")
        lineas = archivo.readlines()
        if len(lineas) > 1:
            estrategia = lineas[:-2]
            for i in range(len(estrategia)):
                estrategia[i] = estrategia[i].replace("\n","")
            temp = estrategia[dato].split(" ")
            if len(temp) >1:
                cadena1 = f"{temp[0].ljust(0)}"
                cadena2 = f"{temp[1].rjust(32)}"
                cadena = cadena1 + cadena2
            elif len(temp[0]) == 0:
                cadena = ""
            else:
                cadena = ""
        else:
            cadena = ""
        read.set(cadena)
        #print(name, cadena)
        sleep(0.1)     

def EstrategiaGUI(name, mesa):
# ----------------------------Creamos GUI para cargar la imagen----------------     
    root = Tk()
# ----------------------------Atributos siempre encima-------------------------
    root.wm_attributes("-topmost", 1)
# --------------------------------Título----------------------------------------
    root.title(mesa)
# -----------------------------Variables----------------------------------------
    readA=StringVar()
    readB=StringVar()
    readC=StringVar()
    readD=StringVar()
    readE=StringVar()
    readF=StringVar()
    readG=StringVar()
    readH=StringVar()


    readA.set(_thread.start_new_thread(AccionRecomendada,(name, readA)))
    readB.set(_thread.start_new_thread(AccionRead,(name, readB, 0)))
    readC.set(_thread.start_new_thread(AccionRead,(name, readC, 1)))
    readD.set(_thread.start_new_thread(AccionRead,(name, readD, 2)))
    readE.set(_thread.start_new_thread(AccionRead,(name, readE, 3)))
    readF.set(_thread.start_new_thread(AccionRead,(name, readF, 4)))
    readG.set(_thread.start_new_thread(AccionRead,(name, readG, 5)))
# ------------------------------- Frames----------------------------------------
# --------------------------------Labels----------------------------------------
    miFrame = Frame(root)
    miFrame.grid(row = 0, column = 0, columnspan = 2)
    miFrame.config(bd = 2)
    miFrame.config(relief = "groove")
# --------------------------------miFrame--------------------------------------
    LabelA=Label(miFrame, textvariable=readA)
    LabelA.grid(row=0,column=0, columnspan = 2)
    LabelA.config(anchor="center",background="#45B39D",justify="center",width="20",height="1",fg="black",
    cursor="hand2",font=("helvetica", "14", "bold"))

    LabelB=Label(miFrame, textvariable=readB)
    LabelB.grid(row=1,column=0, columnspan = 2)
    LabelB.config(anchor="w",background="#F1948A",justify="center",width="20",height="1",fg="black",
    cursor="hand2",font=("helvetica", "14", "bold"))

    LabelC=Label(miFrame, textvariable=readC)
    LabelC.grid(row=2,column=0, columnspan = 2)
    LabelC.config(anchor="w",background="#F1948A",justify="center",width="20",height="1",fg="black",
    cursor="hand2",font=("helvetica", "14", "bold"))

    LabelD=Label(miFrame, textvariable=readD)
    LabelD.grid(row=3,column=0, columnspan = 2)
    LabelD.config(anchor="w",background="#F1948A",justify="center",width="20",height="1",fg="black",
    cursor="hand2",font=("helvetica", "14", "bold"))

    LabelE=Label(miFrame, textvariable=readE)
    LabelE.grid(row=4,column=0, columnspan = 2)
    LabelE.config(anchor="w",background="#F1948A",justify="center",width="20",height="1",fg="black",
    cursor="hand2",font=("helvetica", "14", "bold"))

    LabelF=Label(miFrame, textvariable=readF)
    LabelF.grid(row=5,column=0, columnspan = 2)
    LabelF.config(anchor="w",background="#F1948A",justify="center",width="20",height="1",fg="black",
    cursor="hand2",font=("helvetica", "14", "bold"))

    LabelG=Label(miFrame, textvariable=readG)
    LabelG.grid(row=6,column=0, columnspan = 2)
    LabelG.config(anchor="w",background="#F1948A",justify="center",width="20",height="1",fg="black",
    cursor="hand2",font=("helvetica", "14", "bold"))  
# ---------------------------------Fin GUI ------------------------------------    
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

出现问题的原因可能是您将StringVar直接设置为您启动的线程。

根据this页,其中介绍了“ _thread”模块:

  

_thread.start_new_thread(function,args [,kwargs])¶

     

启动一个新线程并返回其标识符。

这意味着您要将字符串var设置为返回值,即线程标识符。

最重要的是,tkinter不是线程安全的,因此其他更改GUI的线程可能会阻塞主线程(运行tkinter的线程)。

要解决您的问题,建议您使用Queues,以便将数据传递到您的主线程,并从那里更改GUI元素。

您还应该删除启动新线程的“集合”行。