Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
self def
#1
I have a problem with a function and the "self" statement.

from tkinter import *
import turtle
from math import cos,sin,pi

root = Tk()
root.title("Mi app")
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)
  
class graph:

  def __init__(self, a, b):
    self.a = a_entry.get()
    self.b = b_entry.get()

  def graph (self):
    pen.penup()
    angle = 0
    theta = 0.01
    steps = int ((100*pi/theta))
    for t in range(0,steps):
        angle+=theta
        x=(cos(self.a*angle))*(200)
        y=(sin(self.b*angle))*(200)
        pen.goto(x,y)
        pen.pendown()

a_label = Label(root, text="A:")
a_label.grid(row=0, column=1)
a_entry = Entry(root)
a_entry.grid(row=0, column=2)

b_label = Label(root, text="B:")
b_label.grid(row=1, column=1)
b_entry = Entry(root)
b_entry.grid(row=1, column=2)

graph(a_entry.get(), b_entry.get())

graph_button = Button(root, text="Graph", command=graph)
graph_button.grid(row=2, column=2)

root.mainloop()
How can I make the graph function be activated from outside the Graph class?
Reply


Messages In This Thread
self def - by Agusben - May-05-2020, 05:28 AM
RE: self def - by Larz60+ - May-05-2020, 06:05 AM
RE: self def - by bowlofred - May-05-2020, 06:11 AM

Forum Jump:

User Panel Messages

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