python新手提问 谢谢大神了
fromtkinterimport*importrandomimporttimeclassBall:def__init__(self,canvas,color):self...
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 展开
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 展开
展开全部
# 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()
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()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询