python新手提问 谢谢大神了

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
starts = [-3, -2, -1, 1, 2, 3]
random.shuffle(starts)
self.x = starts[0]
self.y = -3
self.canvas_height = self.canvas.winfo_height
self.canvas_width = self.canvas.winfo_width

def draw(self):
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos(1) <= 0:
self.y = 3
if pos(3) >= self.canvas_height:
self.y = -3
if pos(0) <= 0:
self.x = 3
if pos(2) >= self.canvas_width:
self.x = -3
tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

ball = Ball(canvas, 'red')

while 1:
ball.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)

错误如下
C:\Users\admin\AppData\Local\Programs\Python\Python35\python.exe C:/Users/admin/PycharmProjects/untitled3/ball.py
Traceback (most recent call last):
File "C:/Users/admin/PycharmProjects/untitled3/ball.py", line 42, in <module>
ball.draw()
AttributeError: 'Ball' object has no attribute 'draw'

Process finished with exit code 1

这个报错的原因是你的缩进少了,导致 draw() 方法不在 Ball类 里面, 办法是从17到27行全部再往后缩进一个单位。不幸的是,我刚调试了一下,你这段代码不光是有这一个问题。靠你自己慢慢找了。 参考技术A # coding: utf-8
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

from Tkinter import *
import turtle
import random
import time

class Ball:
def __init__(self,canvas,color):
self.canvas=canvas
self.id=canvas.create_oval(10,10,50,50,fill=color) # oval 椭圆形
self.canvas.move(self.id,245,100) # 球开始所处位置
self.x=0 #两边不移动
self.y=-1 #一开始向上移动
self.canvas_height=self.canvas.winfo_height()

def draw(self):
self.canvas.move(self.id,self.x,self.y)
pos=self.canvas.coords(self.id)
if pos[1]<=0: #判断小球的顶部是否小于等于0,如果是,把对象变量设置为1,这样它就不会继续向上移动了
self.y=1
if pos[3]>=self.canvas_height: #判断小球的底部是否大于等于变量canvas_height,即画布高度,
self.y=-1 #如果是,把对象变量y设置回-1

tk=Tk()
tk.title("Game")
tk.resizable(0,0)
#tk.wm_attributes("-topmost",1)
canvas=Canvas(tk,width=700,height=350,bd=0,highlightthickness=0)
canvas.pack()
tk.update()

ball=Ball(canvas,'#f36c21')
while 1:
ball.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)

turtle.done()

#raw_input()
#运行闪退问题
# python2. * 后面加上:raw_input()
# python3. * 后面加上:input()

以上是关于python新手提问 谢谢大神了的主要内容,如果未能解决你的问题,请参考以下文章

[提问]账表输出成excel提示:找不到可安装的isam???谢谢了,大神帮忙啊

本人python新手,现在在看python基础教程,这几天敲了一下后面的项目5,出现了很奇怪的错误,大神帮看看

为啥python3编了一个文件比对程序运行结果出来以后中文变成问号了.哪里的问题求大神?

请问在用swiper做手机页面的时候如何设置页面高度随着显示内容而变化?谢谢了,大神帮忙啊

用arcmap编辑shp格式文件时面积不会变化,是啥原因谢谢了,大神帮忙啊

sql时间只取年月日时分不要秒?谢谢了,大神帮忙啊