Python中引用自定義類的方法

2021-10-05 19:21:02 字數 608 閱讀 8215

自定義類如下

from tkinter import *

import random

import time

class ball:

def __init__(self,canvas, color):

self.canvas = canvas

self.id = canvas.create_oval(10,10,25,25,fill = color)

self.canvas.move(self.id, 245,100)

def draw(self):

pass

類的檔名是balls.py, 儲存在名為playball的包下面

重要一點:playball包裡面要有乙個檔案__init__.py,  這個檔案用來表示乙個包,內容為空就可以。

下面是引用到它的程式:

from playball.balls import ball

ball = ball(canvas,'red')

引用的格式應是這樣:

from 包名.檔名 import 類名

Python中引用自定義類的方法

自定義類如下 from tkinter import import random import time class ball def init self,canvas,color self.canvas canvas self.id canvas.create oval 10,10,25,25,f...

Python中引用自定義模組

學習了一小段時間的python,始終停滯在簡單的語法上,不能真正體會到python的強大之處。今天學習了自定義模組的引用方法。當引用模組時,編譯器會先在當前目錄,接著去sys.path python的安裝目錄去尋找你引用的模組,如果沒有的話,就會報錯。第一種情況,你的模組和你要使用的模組在同一目錄下...

Python中自定義模組的引用

前兩天去面試啦 今天雙學位上了一天的課,晚上回來就拿起新到的head first 開始啃,書上有乙個用函式print lol建立的模組nester.py,我所有的py檔案都放在了d program files x86 sublime text 3 workspace裡,不想每次都把寫好的模組放到預設...