![]() |
self function problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: self function problem (/thread-26646.html) |
self function problem - Agusben - May-08-2020 I have a problem with a program that uses a button that is created in a self function. from tkinter import * from tkinter import ttk import turtle from math import cos,sin,tan,pi from threading import Event root = Tk() root.title("Mi app") stop = Event() class Graph: def __init__(self, events): self.graph_button=Button(root, text="Graph", command=Graph.graph) self.graph_button.grid(row=3,column=1,padx=5,pady=5) self.graph_param1_label=Label(root, text="Parameter 1:") self.graph_param1_label.grid(row=4,column=1,padx=5,pady=5) self.graph_param1_entry=Entry(root) self.graph_param1_entry.grid(row=4,column=2,columnspan=3,padx=5,pady=5) self.graph_param2_label=Label(root, text="Parameter 2:") self.graph_param2_label.grid(row=5,column=1,padx=5,pady=5) self.graph_param2_entry=Entry(root) self.graph_param2_entry.grid(row=5,column=2,columnspan=3,padx=5,pady=5) self.graph_param3_label=Label(root, text="Parameter 3:") self.graph_param3_label.grid(row=6,column=1,padx=5,pady=5) self.graph_param3_entry=Entry(root) self.graph_param3_entry.grid(row=6,column=2,columnspan=3,padx=5,pady=5) def graph(): # reset stop event, if it was already set stop.clear() pen.penup() angle = 0 theta = 0.01 steps = int ((100*pi/theta)) a = 1 ; b = 1 ; c = 1 ; d = 1 ; e = 0 ; f = 0 g = 1 ; h = 1 ; i = 1 ; j = 1 ; k = 0 ; l = 0 m = 1 ; n = 1 ; o = 1 ; p = 1 ; q = 0 ; r = 0 s = 1 ; t = 1 for t in range(0,steps): if stop.is_set(): break try: a = int(self.graph_param1_entry.get()) except ValueError: a = 1 try: b = int(self.graph_param2_entry.get()) except ValueError: b = 1 try: c = int(self.graph_param3_entry.get()) except ValueError: c = 1 angle+=theta x=(cos(a*angle)+cos(b*angle)+cos(c*angle))*(100) y=(sin(a*angle)+sin(b*angle)+sin(c*angle))*(100) pen.goto(x,y) pen.pendown() canvas=turtle.Canvas(master=root, width=650, height=650) canvas.grid(row=0, column=0,columnspan=1, rowspan=20,padx=5,pady=5) pen=turtle.RawTurtle(canvas) pen.width(3) choose_label=Label(root, text="Choose what you want to draw:") choose_label.grid(column=1, row=1, columnspan=5, padx=5) box = ttk.Combobox (root, values = ["Hypocycloid", "Hypotrochoid", "Epicycloid", "Epitrochoid"]) box.grid(column = 1, columnspan=5, row = 2) box.current() box.bind('<<ComboboxSelected>>',Graph) stop_button=Button(root, text="Stop", command=stop.set) stop_button.grid(row=3,column=2,padx=5,pady=5) root.mainloop()How can i make the graph function work with the parameters I set up in the __init__ self function? RE: self function problem - Yoriz - May-08-2020 Add self to the graph method def graph(self): RE: self function problem - Agusben - May-08-2020 That is not the problem. I keep getting this error and I don't know how to solve it TypeError: graph() missing 1 required positional argument: 'self' RE: self function problem - buran - May-08-2020 (May-08-2020, 02:17 PM)Agusben Wrote: That is not the problem. (May-08-2020, 01:19 PM)Yoriz Wrote: Add self to the graph method RE: self function problem - Agusben - May-08-2020 I added self to the graph method and an error pops up. from tkinter import * from tkinter import ttk import turtle from math import cos,sin,tan,pi from threading import Event root = Tk() root.title("Mi app") stop = Event() class Graph: def __init__(self, events): self.graph_button=Button(root, text="Graph", command=Graph.graph) self.graph_button.grid(row=3,column=1,padx=5,pady=5) self.graph_param1_label=Label(root, text="Parameter 1:") self.graph_param1_label.grid(row=4,column=1,padx=5,pady=5) self.graph_param1_entry=Entry(root) self.graph_param1_entry.grid(row=4,column=2,columnspan=3,padx=5,pady=5) self.graph_param2_label=Label(root, text="Parameter 2:") self.graph_param2_label.grid(row=5,column=1,padx=5,pady=5) self.graph_param2_entry=Entry(root) self.graph_param2_entry.grid(row=5,column=2,columnspan=3,padx=5,pady=5) self.graph_param3_label=Label(root, text="Parameter 3:") self.graph_param3_label.grid(row=6,column=1,padx=5,pady=5) self.graph_param3_entry=Entry(root) self.graph_param3_entry.grid(row=6,column=2,columnspan=3,padx=5,pady=5) def graph(self): # reset stop event, if it was already set stop.clear() pen.penup() angle = 0 theta = 0.01 steps = int ((100*pi/theta)) a = 1 ; b = 1 ; c = 1 for t in range(0,steps): if stop.is_set(): break try: a = int(self.graph_param1_entry.get()) except ValueError: a = 1 try: b = int(self.graph_param2_entry.get()) except ValueError: b = 1 try: c = int(self.graph_param3_entry.get()) except ValueError: c = 1 angle+=theta x=(cos(a*angle)+cos(b*angle)+cos(c*angle))*(100) y=(sin(a*angle)+sin(b*angle)+sin(c*angle))*(100) pen.goto(x,y) pen.pendown() canvas=turtle.Canvas(master=root, width=650, height=650) canvas.grid(row=0, column=0,columnspan=1, rowspan=20,padx=5,pady=5) pen=turtle.RawTurtle(canvas) pen.width(3) choose_label=Label(root, text="Choose what you want to draw:") choose_label.grid(column=1, row=1, columnspan=5, padx=5) box = ttk.Combobox (root, values = ["Hypocycloid", "Hypotrochoid", "Epicycloid", "Epitrochoid"]) box.grid(column = 1, columnspan=5, row = 2) box.current() box.bind('<<ComboboxSelected>>',Graph) stop_button=Button(root, text="Stop", command=stop.set) stop_button.grid(row=3,column=2,padx=5,pady=5) root.mainloop()The error that appears after I select a value from the combobox is: TypeError: graph() missing 1 required positional argument: 'self' RE: self function problem - deanhystad - May-08-2020 There are multiple ways you can use class in python and you are mixing two of them up. You can use class to define a namespace. I could write a class named Graph which has a function named graph(args) and call this function using Graph.graph(args). You can use class to define a new type and create instances of this type. I can define Graph that has a METHOD named graph(self, args) and call that method using obj = Graph(), obj.graph(args). You are mostly using class to create a new type. You will want to bind your button to call a method. self.graph_button=Button(root, text="Graph", command=self.graph) RE: self function problem - Agusben - May-08-2020 thank you so much. |