我有一段代码,之后随便输入什么数字(当然是范围内的数字)最后总是报错:'int' object has no attribute 'one_res_1'
实在是不明白位什么?
class MyApp(App):
x=1
y=0
sum
def build(self):
layout = FloatLayout()#浮动布局
self.one_res_mianshu=Label(text="输入面数",color=(0,2,0,2),size_hint=(0.22, 0.19), pos=(1,525))
self.one_res_geshu=Label(text="输入个数",color=(0,2,0,2),size_hint=(0.22, 0.19), pos=(70,525))
self.text1=TextInput(size_hint=(0.12, 0.088), pos=(10,511), multiline=False)
self.text11=TextInput(size_hint=(0.12, 0.088), pos=(110,511), multiline=False)
btn1 = Button(text="第一个按钮",size_hint=(0.12, 0.09), pos=(210, 509))
btn2 = Button(text="第二个按钮",size_hint=(0.12, 0.09), pos=(310, 509))
btn1.bind(on_press=self.getEntry1)
btn1.bind(on_press=self.callback2)
layout.add_widget(self.one_res_mianshu)
layout.add_widget(self.one_res_geshu)
layout.add_widget(self.text1)
layout.add_widget(self.text11)
layout.add_widget(btn1)
layout.add_widget(btn2)
self.one_res_1=Label(text="0",color=(0,1,0,1),size_hint=(0.12, 0.09), pos=(380, 510))
self.one_res_2=Label(text="0",color=(0,1,0,1),size_hint=(0.12, 0.09), pos=(410, 510))
self.one_res_3=Label(text="0",color=(0,1,0,1),size_hint=(0.12, 0.09), pos=(440, 510))
self.one_res_4=Label(text="0",color=(0,1,0,1),size_hint=(0.12, 0.09), pos=(470, 510))
self.one_res_5=Label(text="0",color=(0,1,0,1),size_hint=(0.12, 0.09), pos=(500, 510))
self.one_res_6=Label(text="0",color=(0,1,0,1),size_hint=(0.12, 0.09), pos=(530, 510))
layout.add_widget(self.one_res_1)
layout.add_widget(self.one_res_2)
layout.add_widget(self.one_res_3)
layout.add_widget(self.one_res_4)
layout.add_widget(self.one_res_5)
layout.add_widget(self.one_res_6)
return layout
def callback2(self,*args):
if MyApp.r == 0 or MyApp.x1 == 1:
print('请重新输入骰子的面数和个数')
exit
else:
if MyApp.x1 == 2:
MyApp.shaizi_1(MyApp.y1,a)
elif MyApp.x1 == 3:
MyApp.shaizi_1(MyApp.y1,b)
elif MyApp.x1 == 4:
MyApp.shaizi_1(MyApp.y1,c)
elif MyApp.x1 == 5:
MyApp.shaizi_1(MyApp.y1,d)
elif MyApp.x1 == 6:
MyApp.shaizi_1(MyApp.y1,e)
elif MyApp.x1 == 7:
MyApp.shaizi_1(MyApp.y1,f)
elif MyApp.x1 == 8:
MyApp.shaizi_1(MyApp.y1,g)
def shaizi_1(self,aaa):
print(MyApp.y1,aaa)
if MyApp.y1 == 1:
sum=ran.sample(aaa,1)
self.one_res_1.text=str(sum)
elif MyApp.y1 == 2:
sum=ran.sample(aaa,1)
self.one_res_1.text=str(sum)
sum=ran.sample(aaa,1)
self.one_res_2.text=str(sum)
elif MyApp.y1 == 3:
sum=ran.sample(aaa,1)
self.one_res_1.text=str(sum)
sum=ran.sample(aaa,1)
self.one_res_2.text=str(sum)
sum=rand.sample(aaa,1)
self.one_res_3.text=str(sum)
def getEntry1(self,*args):
MyApp.x1 = int(self.text1.text)
MyApp.y1 = int(self.text11.text)
mianshufanwei=[2,3,4,5,6,7,8,9,10,12,17,20,24,30,36]
if MyApp.x1 not in mianshufanwei:
print('骰子的面数不符合要求')
MyApp.r = 0
exit
if MyApp.y1 == 0 or MyApp.y1 < 0 or MyApp.y1 >6:
print('骰子的个数不符合要求')
MyApp.r = 0
exit
print('getentry1',MyApp.x1,MyApp.y1)
print(MyApp.r)
if name == 'main':
MyApp().run()
代码不全。
self指向的是class的一个实例
那么你这个class什么时候初始化的,怎么初始化的,代码没有
既然报错说int不包含one_res_1,那么很有可能你的class被赋值给一个int了