[Python] I keep getting this message when running the code: TypeError: add_node() missing 1...

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to code a map that shows a graph of nodes and paths. I should be able to add a new node by clicking a button ('Add node') and then the graph, but the only message that pops up is this: TypeError: add_node() missing 1 required positional argument: 'self'. I would be glad if someone could help me.

    This is a part of the code:

    def add_node(self):
    if not self.graph:
    self.graph = Graph()
    name = self.node_name_entry.get().strip()
    try:
    x = float(self.node_x_entry.get())
    y = float(self.node_y_entry.get())
    node = Node(name, x, y)
    self.graph.AddNode(node)
    self.output_text.insert(tk.END, f"Node '{name}' added.\n")
    self.draw_graph()
    except ValueError:
    messagebox.showerror("Error", "Invalid coordinates.")
    root=tk.Tk()
    root.geometry('800x400')
    root.title('Routes')
    root.columnconfigure(0, weight=1)
    root.columnconfigure(1, weight=10)
    root.rowconfigure(0, weight=1)
    root.rowconfigure(1, weight=1)
    root.rowconfigure(2, weight=1)

    button_mod_frame = tk.LabelFrame(root, text='Buttons')
    button_mod_frame.grid(row=1, column=0, padx=5, pady=5, sticky=tk.N+tk.E+tk.W+tk.S)

    button_mod_frame.columnconfigure(0, weight=1)
    button_mod_frame.rowconfigure(0, weight=1)
    button_mod_frame.rowconfigure(1, weight=1)
    button_mod_frame.rowconfigure(2, weight=1)

    button4 = tk.Button(button_mod_frame, text="Add node", command=add_node)
    button4.grid(row=0, column=0, padx=5, pady=5, sticky="nsew")

    root.mainloop()

    Continue reading...

Compartilhe esta Página