我正在使用tkinter开发图形界面。当我打开文件并发送它时,我想看到标签中的滚动指令(Commande)
更准确地说,我不知道如何将此说明print('Sending: ' + l)
与此说明Label(Frame1, text="Commande", background='NavajoWhite2').pack(padx=165, pady=10)
感谢所有
from tkinter import *
from tkinter import filedialog
from tkinter import ttk, Text
#nom de la fenetre
moha = Tk()
moha.title('Emetteur G-code Universel')
moha.geometry("2500x2500")
moha['bg']= 'NavajoWhite2'
##################
##################
#le chemin de recherche #
def file(tk_event=None, *args, **kw):
fiile = filedialog.askopenfilename(filetypes=[('txt files','.txt'),('all files','.*')])
file_path.set(fiile)
fichier = open(fiile, "r")
print(fiile)
content_bis = fichier.readlines()
for line in content_bis:
T.insert(END, line)
fichier.close()
# Créer les différentes Frame
Outil = LabelFrame(moha, text="Outil")
Outil.place(x=500)
Outil['bg']= 'NavajoWhite2'
label = Label(Outil, text='Fichier : ', background='NavajoWhite2')
label.place(x=10, y=114)
file_path = StringVar()
entry = Entry(Outil, textvariable=file_path)
entry.place(x=60, y=114)
################
def moh():
# Open grbl serial port
s = serial.Serial('/dev/ttyS0',115200)
# Open g-code file
f = open(file_path.get(), "r");
# Wake up grbl
s.write("\r\n\r\n".encode('utf8'))
time.sleep(2) # Wait for grbl to initialize
s.flushInput() # Flush startup text in serial input
# Stream g-code to grbl
for line in f:
l = line.strip() # Strip all EOL characters for consistency
print ('Sending: ' + l)
s.write((l + '\n').encode("utf8")) # Send g-code block to grbl
grbl_out = s.readline().decode("utf8") # Wait for grbl response with carriage return
print (' : ' + grbl_out)
# Wait here until grbl is finished to close serial port and file.
raw_input(" Press <Enter> to exit and disable grbl.")
# Close file and serial port
f.close()
s.close()
#Créer la barre de recherche
#################
o = LabelFrame(moha, text="Fase des commandes")
o.place(y=400)
o['bg']= 'NavajoWhite2'
Label(o, background='NavajoWhite2').pack(padx=670,pady=120)
Frame1 = Frame(o, borderwidth=3, relief=GROOVE, background='NavajoWhite2')
Frame1.place(x=0, y=5)
Label(Frame1, text="Commande",background='NavajoWhite2').pack(padx=165, pady=10)
#################
b1= Button(Outil, text ="Commande", background='White').place(x=10, y=20)
b2= Button(Outil, text ="Fichier", background='White').place(x=110, y=20)
b3= Button(Outil, text ="Côntrole de la machine",
background='White').place(x=190, y=20)
b4= Button(Outil, text ="Selectionner un fichier",background='White',
command=file).place(x=235, y=112)
b5= Button(Outil, text ="Envoyer",background='White', command=moh).place(x=30, y=150)
b6= Button(Outil, text ="Pause",background='White').place(x=110, y=150)
b7= Button(Outil, text ="Annuler",background='White').place(x=180, y=150)
Label(Outil, text="", background='NavajoWhite2').pack(padx=300, pady=150)
################
################
S = Scrollbar(Outil, background='NavajoWhite2')
T = Text(Outil, height=19, width=28)
S.place(x=380, y=10)
T.place(x=400, y=0)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
答案 0 :(得分:0)
其次,要在for
循环中在GUI中显示标签文字实时,您需要在主窗口上调用.update()
。
以下是显示其工作原理的Minimal, Complete, and Verifiable example:
import tkinter as tk
from time import sleep
def change_label():
for i in range(5):
label.config(text="iteration " + str(i))
# Update the GUI in real time
root.update()
sleep(1)
root = tk.Tk()
# Create & align a label in 2 steps
label = tk.Label(root, text="hello")
label.pack()
button = tk.Button(root, text="click me", command=change_label)
button.pack()
# Launch the GUI
root.mainloop()