0

this is the code , i get always a 0 as a returned value, can anyone help me to extract that value and use it for further calculations , i'm new in python espessially software i just need your help and ill be gratefull , thank you very much

from tkinter import *
import re
from tkinter import messagebox
from tkinter import filedialog


class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)   
        self.master = master
        self.init_window()
    def init_window(self):
     self.master.title("gcode Data")
     self.pack(fill=BOTH, expand=1)
     menu = Menu(self.master)
     self.master.config(menu=menu)
     file = Menu(menu)
     file.add_command(label="Exit", command=self.client_exit)
     menu.add_cascade(label="File", menu=file)
     quitButton = Button(self, text="Load GCODE",command=self.read_gcode)
     quitButton.place(x=60, y=50)

   
    def get_filament_value(self, fileName):
        with open(fileName, 'r') as f_gcode:
            data = f_gcode.read()
            re_value = re.search('filament used = .*? \(([0-9.]+)', data)

            if re_value:
                value = float(re_value.group(1))
                print('filament value is {}'.format(value))
            
            else:
                  value = 0.0
                  print('filament not found in {}'.format(fileName))
        return value

    def read_gcode(self):
        root.fileName = filedialog.askopenfilename(filetypes = (("GCODE files", "*.gcode"), ("All files", "*.*")))
        self.value = self.get_filament_value(root.fileName)

    def client_exit(self):
        exit()
root = Tk()
root.geometry("220x300")
app = Window(root)
root.mainloop() 
  • Are you sure the regular expression is matching? Can you add a sample of file contents? – Barmar May 06 '22 at 16:57
  • Are you getting the "filament not found" message? – Barmar May 06 '22 at 16:59
  • yeah im getting filament not found and yeah i will add a sample file – Fares Montasser May 06 '22 at 17:23
  • sorry but i guess i cant upload a simple file but you can use any gcodefile i guess they are all the same – Fares Montasser May 06 '22 at 17:26
  • We don't need to see the whole file, just the line you're trying to match with the regexp. The problem is that it doesn't match. – Barmar May 07 '22 at 00:07
  • this is the line ; filament used [mm] = 141.27 , and i don't think that matching the line is the problem beacause i tried serching for it as a only a string and puting it on a text file and it works perfectly but if i want to take only the value and use it for other calculation the value is always 0 and it say that there is no match – Fares Montasser May 07 '22 at 09:11
  • Your regular expresssion says there's a `(` before the number. But there isn't. Get rid of `\(` – Barmar May 07 '22 at 11:35
  • Why don't you think matching is the problem? "filament not found" is printed when `if re_value` is None, which happens when the RE isn't matched. – Barmar May 07 '22 at 11:38
  • i get rid like you said of \( but its the same result i alway have 0 as a value , i think cause if i search the line of 'filament used [mm]' as a text and put it in a list it finds it and print all of the line as a string and then i can not use the number of filament used for calculation , i can upload the other code if it's needed and you can try it , thank you for your effort – Fares Montasser May 07 '22 at 18:25
  • The regexp says `filament value` but the line has `filament used` – Barmar May 08 '22 at 00:50
  • yeah so what do i have to do to get my value from that search , or what do i have to add or change ? – Fares Montasser May 08 '22 at 17:21
  • Change `value` to `used` in the regular expression, so it matches the line in the text. Why isn't this obvious? – Barmar May 09 '22 at 04:25
  • sorry i had a lot of work to do , yeah i did what you said and also i tried changing the line i searched for and changing everything in the code as you said but nothing happened , the value is always 0.0 – Fares Montasser May 17 '22 at 15:40

0 Answers0