2023-12-15

why do i recieve the output of an else statement yet the if is true

the problem is that i created an app to compile .c and .py files but whenever i open a pre written file it gives the output of the else which would mean that no file is found this is part of the code

        
    def new_file(self):
        text_area = tk.Text(self.tab_manager)
        text_area.pack()
        self.tab_manager.add(text_area, text="Untitled")
        self.files.append(None)

this is the open function

    def run_file(self):
        current_file_index = self.tab_manager.index(self.tab_manager.select())
        current_file = self.files[current_file_index]
        if current_file:
            self.output_bar.config(state='normal')
            self.output_bar.delete("1.0", tk.END)
            if current_file.endswith(".c"):
                file_path = self.tab_manager.tab(current_file_index)["text"]
                try:
                    subprocess.run(["gcc", file_path, "-o", "a.out"], check=True)
                    output = subprocess.run(["./a.out"], capture_output=True).stdout.decode()
                    self.output_bar.delete("1.0", tk.END)
                    self.output_bar.insert('1.0',output)
                except subprocess.CalledProcessError as e:
                    error = e.stderr.decode() if e.stderr else str(e)
                    self.output_bar.delete("1.0", tk.END)
                    self.output_bar.insert('1.0', error)

            elif current_file.endswith(".py"):
                file_path = self.tab_manager.tab(current_file_index)["text"]
                try:
                    output = subprocess.check_output(["python3", file_path], stderr=subprocess.STDOUT).decode()
                    self.output_bar.delete("1.0", tk.END)
                    self.output_bar.insert('1.0', output)
                except subprocess.CalledProcessError as e:
                    error = e.output.decode() if e.output else str(e)
                    self.output_bar.delete("1.0", tk.END)
                    self.output_bar.insert('1.0', error)
            else:
                messagebox.showerror("Error", "Only C and Python files can be executed!")
                self.output_bar.delete("1.0", tk.END)
                self.output_bar.config(state='disabled')
        else:
            self.output_bar.config(state='normal')
            self.output_bar.delete("1.0", tk.END)
            self.output_bar.insert("1.0", "No file found")
            self.output_bar.config(state='disabled')

i tried moving the text_area outside the function new_file and puting it on the same level as the other widgets but that didnt work then i tried a bunch of other stuff i forgot about



No comments:

Post a Comment