Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
self function problem
#1
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?
Reply
#2
Add self to the graph method
def graph(self):
Reply
#3
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'
Reply
#4
(May-08-2020, 02:17 PM)Agusben Wrote: 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'


(May-08-2020, 01:19 PM)Yoriz Wrote: Add self to the graph method
def graph(self):
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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'
Reply
#6
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)
Reply
#7
thank you so much.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020