zoukankan      html  css  js  c++  java
  • 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,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 类名

  • 相关阅读:
    P4149 [IOI2011]Race dsu on tree
    CodeForces
    2020牛客国庆集训派对day2 CHEAP DELIVERIES
    Hero In Maze
    topo排序
    今年暑假不AC
    小国的游戏
    Stall Reservations
    博弈论
    Reversed Words
  • 原文地址:https://www.cnblogs.com/derekhan/p/12245721.html
Copyright © 2011-2022 走看看