我正在测试我的知识。在我的脚本中,我使用画布,标签和鼠标。 我的python(kivy)脚本在pyscripter的环境中运行。当我使用kivy启动器时,程序在加载过程中会掉下来。我将我的脚本与kivy示例进行了比较,我没有找到问题的根源。请向我解释一下问题。 谢谢。
from kivy.app import App
from kivy.graphics import Color, Rectangle, Ellipse
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from enum import Enum
import collections
Palete = collections.namedtuple('Palete',
['Pos', 'Fig', 'Select'])
pal = Palete((1,1,0), (1,0,0), (0,0,1))
class TypPos(Enum):
FIGURE = 1
POSITION = 2
FICT = 10
class Status(Enum):
QUIET = 1
SELECT = 2
JUMP = 3
class Position:
def __init__(self):
self.i =0
self.j =0
self.x =0
self.y =0
self.color = (0, 0, 0)
self.cont = None
def SetIJ(self, i, j):
self.i =i
self.j =j
def SetXY(self, x, y):
self.x =x
self.y =y
def SetCont(self, cont):
self.cont = cont
def SetColor(self, color):
self.color = color
def GetColorXY(self):
return self.x, self.y, self.color
def GetIJ(self):
return self.i, self.j
class Board:
def __init__(self,size,radius, offsetX, offsetY):
self.size = size
self.count = 9*size * size
self.dist = 2 * radius + radius * 0.5
self.radius = radius
self.liPlan = []
self.ind = 0
self.offsetX = offsetX
self.offsetY = offsetY
self.PrepareBoard()
self.status = Status.QUIET
self.select = 0
self.countfig = size*size*5-1
def PrepareBoard(self):
for i in range(0,self.size):
for j in range(0, 3*self.size):
pos = Position()
if j>=self.size and j<2*self.size:
cont = TypPos.FIGURE
else:
cont = TypPos.FICT
pos.SetIJ(i, j)
pos.SetCont(cont)
self.liPlan.append(pos)
for i in range(self.size, 2*self.size):
for j in range(0, 3*self.size):
pos = Position()
cont = TypPos.FIGURE
pos.SetIJ(i, j)
pos.SetCont(cont)
self.liPlan.append(pos)
for i in range(2*self.size, 3*self.size):
for j in range(0, 3*self.size):
pos = Position()
if j>=self.size and j<2*self.size:
cont = TypPos.FIGURE
else:
cont = TypPos.FICT
pos.SetIJ(i, j)
pos.SetCont(cont)
self.liPlan.append(pos)
self.liPlan[self.size].SetCont(TypPos.POSITION)
return
def GetRGB(self, col):
if col == TypPos.FIGURE:
rgb = pal.Fig
elif col == TypPos.POSITION:
rgb = pal.Pos
else:
rgb = None
return rgb
def XYFromInd(self,ind):
print(str(ind))
pos = self.liPlan[ind]
if pos.cont == TypPos.FICT:
# rgb = (0, 1, 0)
pos.SetXY(0,0)
pos.SetColor(None)
return
else:
rgb = self.GetRGB(pos.cont)
red = ind+1
i = red//(3*self.size)
j =red%(3*self.size)
if j==0:
i = i-1
j = 3 *self.size-1
else:
j = j-1
y = self.dist*3*self.size - (i+1) * self.dist + self.offsetY
x = (j+1)* self.dist + self.offsetX
self.liPlan[ind].SetXY(x,y)
self.liPlan[ind].SetColor(rgb)
return
def IndFromXY(self, x, y):
x= x - self.offsetX - self.dist/2
y= -(y - self.offsetY - self.dist*3*self.size) + self.dist/2
ii = x/self.dist
jj = y/self.dist
i = int(round(ii))-1
j = int(round(jj))-1
if j < 0:
j = 0
ind = j* self.size*3 + i
return ind
def CoordinatePos(self):
# for i in range(0,self.count):
for i in range(0,len(self.liPlan)):
self.XYFromInd(i)
return
def SelectTask(self, x, y):
ind = self.IndFromXY(x, y)
pos = self.liPlan[ind]
x, y, rgb = pos.GetColorXY()
if self.status == Status.QUIET:
if pos.cont ==TypPos.FIGURE:
color = self.MarkFig(ind)
self.liPlan[ind].SetColor(color)
return
else:
return
if self.status == Status.SELECT:
if ind == self.select:
self.UnMarkFig(ind)
return
if self.liPlan[ind].cont ==TypPos.POSITION:
self.Jump(ind)
return
return
def MarkFig(self, ind):
col = pal.Select
self.status = Status.SELECT
self.select = ind
return col
def UnMarkFig(self, ind):
self.status = Status.QUIET
self.liPlan[ind].cont = TypPos.FIGURE
self.liPlan[ind].SetColor(pal.Fig)
return
def Jump(self,ind):
out = self.IndexOut(ind, self.select)
if self.liPlan[out].cont == TypPos.POSITION:
return
self.status= Status.QUIET
self.liPlan[ind].cont = TypPos.FIGURE
self.liPlan[ind].SetColor(pal.Fig)
self.liPlan[self.select].cont = TypPos.POSITION
self.liPlan[self.select].SetColor(pal.Pos)
self.liPlan[out].cont = TypPos.POSITION
self.liPlan[out].SetColor(pal.Pos)
self.countfig = self.countfig-1
return
def IndexOut(self, ind, select):
i1, j1 = self.liPlan[ind].GetIJ()
i2, j2 = self.liPlan[select].GetIJ()
i = (i1 + i2)//2
j = (j1 + j2)//2
out = i*self.size*3 + j
return out
class CustomLayout(Widget):
def __init__(self, label, wid, **kwargs):
super(CustomLayout, self).__init__(**kwargs)
self.board = Board(5, 10, 30, 40)
self.wid = wid
label.text = 'Count: ' + str(self.board.countfig)
self.label = label
self.board.CoordinatePos()
for i in range(0,self.board.count):
pos = self.board.liPlan[i]
x, y, rgb = pos.GetColorXY()
if rgb == None:
continue
with self.canvas:
Color(rgb[0], rgb[1], rgb[2])
size1=(self.board.radius*2, self.board.radius*2)
pos1 = (x, y)
Ellipse(size= size1, pos=pos1)
return
def on_touch_down(self, touch):
self.count = self.board.countfig
self.board.SelectTask(touch.x, touch.y)
self.label.text = 'Count: ' + str(self.board.countfig)
for i in range(0,self.board.count):
pos = self.board.liPlan[i]
x, y, rgb = pos.GetColorXY()
if rgb == None:
continue
with self.canvas:
Color(rgb[0], rgb[1], rgb[2])
size1=(self.board.radius*2, self.board.radius*2)
pos1 = (x, y)
Ellipse(size= size1, pos=pos1)
class JumperApp(App):
def build(self):
parent = Widget()
label = Label(pos=(60, 50))
paint = CustomLayout(label, parent)
parent.add_widget(label)
parent.add_widget(paint)
return parent
if __name__ == '__main__':
JumperApp().run()
android.text:
title=JumperApp.py
author=Black
orientation=landscape