python 如何从另一个.py文件中获取函数和类中的变量

jmo0nnb3  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(162)

我是python的初学者,现在尝试使用tkinter创建一个机器人。我有2个. py文件,一个是"www.example.com",另一个是"www.example.com"。 Arayuz.py " and other one is " scrollimage.py ".
我想从"www.example.com"中获取self.start.x和selfstart.y的值,并在"www.example.com"中使用它们。我该怎么做? scrollimage.py " and use those in " arayuz.py ".How can i do that?
谢谢。
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)

我想在Arayuz.py中使用这些self.start_xself.start_y值。

yhived7q

yhived7q1#

因为它们是公共示例属性,所以可以毫无问题地使用。

from scrollableimage import ScrollableImage

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

相关问题