没有这样的文件或目录'fiile'

时间:2017-04-22 14:04:51

标签: python

我有一个问题,我想在python 3中从srollbar运行一个文件,当我运行代码时它向我显示一个错误:没有这样的文件或目录'fiile' 我想通过按发送按钮发送滚动条中的文件, 我已经指定了发送按钮的命令

import serial
import time
from tkinter import filedialog
from tkinter import *


moha = Tk()
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)


def moh():
    # Open grbl serial port
    s = serial.Serial('/dev/ttyS0',115200)

    # Open g-code file
    f = open('fiile', '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 streaming
        print ('Sending: ' + l)
        s.write(l + '\n') # Send g-code block to grbl
        grbl_out = s.readline() # Wait for grbl response with carriage return
        print( ' : ' + grbl_out.strip())

    # 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()
file_path = StringVar()
entry = Entry(moha, textvariable=file=path)
entry.place(x=155, y=114)
b1= Button(moha, text="Selectionner un fichier", background='White', command=file).place(x=290, y=112)
b2= Button(moha, text="Send", background='White', command=moh).place(x=120, y=170)

全部谢谢

1 个答案:

答案 0 :(得分:0)

您将file_path变量设置为filedialog的文件路径。因此,您只需调用file_path.get()即可获取文件路径。 在函数moh:

#Open g-code file
f = open(file_path.get(),"r")