我是基维的新手,我要写一个游戏应用程序。
File "c:\Users\sorus\OneDrive\Desktop\New folder (2)\dear_pygui\update_label.kv", line 24, in <module>
on_press : root.sang()
TypeError: 'kivy.weakproxy.WeakProxy' object is not callablemain.py:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.lang import Builder
import random
from kivy.uix.widget import Widget
game_list = ["Sang" , "Kaqaz" , "Qeichi"]
user_score = 0
computer_score = 0
random_choose = random.choice(game_list)
Builder.load_file("update_label.kv")
class MainPage(Widget):
def sang(self):
global computer_score
global user_score
global random_choose
random_choose = random.choice(game_list)
choose = "Sang"
if random_choose == "Kaqaz" and choose == "Sang":
computer_score += 1
elif random_choose == "Qeichi" and choose == "Sang":
user_score += 1
else:
pass
def kaqaz(self):
global computer_score
global user_score
global random_choose
random_choose = random.choice(game_list)
choose = "Kaqaz"
if random_choose == "Qeichi" and choose == "Kaqaz":
computer_score += 1
elif random_choose == "Sang" and choose == "Kaqaz":
user_score +=1
else:
pass
def qeichi(self):
global computer_score
global user_score
global random_choose
random_choose = random.choice(game_list)
choose = "Qeichi"
if random_choose == "Kaqaz" and choose == "Qeichi":
user_score += 1
elif random_choose == "Sang" and choose == "Qeichi":
computer_score += 1
else:
pass
def restart(self):
global computer_score
global user_score
user_score = 0
computer_score = 0
def update(self):
global computer_score
global user_score
global random_choose
self.random_choose.text = ("Bot :" + random_choose)
self.your_label.text = ("Your Score :" + str(user_score))
self.computer_label.text = ("Computer Score : " + str(computer_score))
def on_touch_up(self, touch):
self.update()
class Myapp(App):
def build(self):
return MainPage()
if __name__ == "__main__":
Myapp().run()我使用.kv文件运行了这段代码并获得了以下错误:
TypeError:“kivy.weakproxy.WeakProxy”对象不可调用
update_label.kv:
<MainPage>
sang : sang
kaqaz : kaqaz
qeichi : qeichi
restart : restart
random_choose : random_choose
user_score : user_score
computer_score : computer_score
GridLayout:
cols : 1
size : root.width , root.height
Label:
id : random_choose
text : "Hi"
GridLayout:
cols : 1
Button:
id : sang
text : "Sang"
on_press : root.sang()
Button:
id : kaqaz
text : "Kaqaz"
on_press : root.kaqaz()
Button:
id : qeichi
text : "Qeichi"
on_press : root.qeichi()
Label :
id : user_score
text : "Your Score :"
Label :
id : computer_score
text : "Computer Score :"
Button:
id : restart
text : "Restart"
on_press : root.restart()请帮我弄清楚。
如果你知道这个话题,请评论我该怎么做。
关于这个话题,没有人有很好的解决方案,
发布于 2021-02-16 14:40:40
在您的kv中,行:
sang : sang将root.sang设置为id sang而不是sang方法对Button的引用。当您单击该Button时会产生错误。
修复方法是更改ids或函数名。确保您没有为不同的事物使用相同的名称。
https://stackoverflow.com/questions/66219501
复制相似问题