2017-05-03 79 views
-2

我正在用tkinter開發一個圖形界面。當我打開文件,並把它,我想看看在標籤滾動指令(COMMANDE)tkinter接口需要幫助

更準確地說我不知道​​如何將這種指令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

您可以更新的文字或使用'config'方法標籤的任何屬性:'label.config(文=「發送:」 + L)'。 – Novel

+0

@Novel我不明白:/ –

+0

如果你用'label.config(text ='Sending:'+ l)'替換'print('Sending:'+ l)',你會在標籤中看到更新比終端。這不是你要問的嗎? – Novel

回答

0

,以便適當地存儲參考到窗口小部件,則應該從插件對準分離插件創建(也參見here)。

其次,在實時您的標籤在你的GUI要顯示的文字而在for循環,你將需要調用.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()