Home > front end >  How can I get a variable that is in a function and a class from another .py file
How can I get a variable that is in a function and a class from another .py file

Time:02-03

I'm starter for python and now trying to create a bot by using tkinter. I have 2 .py files one is "Arayuz.py" and other one is "scrollimage.py".

I want to get the self.start.x and self start.y value from "scrollimage.py" and use those in "arayuz.py".How can i do that?

Thanks.

scrollimage.py :


import tkinter
import random
class ScrollableImage(tkinter.Frame):

    def __init__(self, master=None, **kw):

        self.image = kw.pop('image', None)
        sw = kw.pop('scrollbarwidth', 10)
        super(ScrollableImage, self).__init__(master=master, **kw)
        self.cnvs = tkinter.Canvas(self, highlightthickness=0, **kw)
        self.cnvs.create_image(0, 0, anchor='nw', image=self.image)
        # Vertical and Horizontal scrollbars
        self.v_scroll = tkinter.Scrollbar(self, orient='vertical', width=15)
        self.h_scroll = tkinter.Scrollbar(self, orient='horizontal', width=15)
        # Grid and configure weight.
        self.cnvs.grid(row=0, column=0,  sticky='nsew')
        self.h_scroll.grid(row=1, column=0, sticky='ew')
        self.v_scroll.grid(row=0, column=1, sticky='ns')
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        # Set the scrollbars to the canvas
        self.cnvs.config(xscrollcommand=self.h_scroll.set, 
                           yscrollcommand=self.v_scroll.set)
        # Set canvas view to the scrollbars
        self.v_scroll.config(command=self.cnvs.yview)
        self.h_scroll.config(command=self.cnvs.xview)
        # Assign the region to be scrolled 
        self.cnvs.config(scrollregion=self.cnvs.bbox('all'))
        self.cnvs.bind_class(self.cnvs, "<MouseWheel>", self.mouse_scroll)

        self.cnvs.bind("<Button-1>", self.on_button_pressed)



    def mouse_scroll(self, evt):
        if evt.state == 0 :
            self.cnvs.yview_scroll(-1*(evt.delta), 'units') # For MacOS
            self.cnvs.yview_scroll(int(-1*(evt.delta/120)), 'units') # For windows
        if evt.state == 1:
            self.cnvs.xview_scroll(-1*(evt.delta), 'units') # For MacOS
            self.cnvs.xview_scroll(int(-1*(evt.delta/120)), 'units') # For windows


    def on_button_pressed(self,event):

       self.start_x = self.cnvs.canvasx(event.x)
       self.start_y = self.cnvs.canvasy(event.y)
       print("start_x, start_y =", self.start_x, self.start_y)

       print("x=",(int(self.start_x/10) 1),"y=",(int(self.start_y/10) 1))

       self.cnvs.create_oval(self.start_x, self.start_y, self.start_x, self.start_y, fill="blue", width=5)


I want to use those self.start_x and self.start_y values in Arayuz.py.

CodePudding user response:

Since they are public instance attributes you can use without problems.

from scrollableimage import ScrollableImage

scrollable_image_instance = ScrollableImage() # create an instance 
print(scrollable_image_instance.start_x)
print(scrollable_image_instance.start_y)
  • Related