我正在尝试创建一个需要用户输入坐标的游戏。但是,我意识到即使单击“ Enter”按钮并更改了屏幕,也不会清除这些字段。通常,我通常使用普通的textinput字段插入python行self.shootdepthinput.text=''
,这将清除字段中的文本输入,因为它是字符串。但是,对于MDTextField,输入为不可变的元组形式。有人知道我可以清除MDtextfield的功能吗?谢谢!
这是我的kivy文件的摘录,其中在以下位置找到了我的MDTextField:
<ComputerScreen>:
shootxinput: shootxinput_id
shootyinput: shootyinput_id
shootdepthinput: shootdepthinput_id
id: "computerscreen_id"
name: "computer"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: "battleshipgame.png"
GridLayout:
cols: 3
id: s_input
pos_hint: {"x":0.03,"y":0.72}
size_hint : 0.7, 0.1
MDTextField:
id: shootxinput_id
hint_text: "X-Coordinate"
required: True
helper_text_mode: "on_focus"
color_mode: "custom"
helper_text: "Enter a number from 1 to 10"
line_color_focus: 0, 0, 0, 1
MDTextField:
id: shootyinput_id
hint_text: "Y-Coordinate"
required: True
color_mode: "custom"
helper_text_mode: "on_focus"
helper_text: "Enter an alphabet from A to J"
line_color_focus: 0, 0, 0, 1
MDTextField:
id: shootdepthinput_id
hint_text: "Depth"
required: True
color_mode: "custom"
helper_text_mode: "on_focus"
helper_text: "0: Surface Level, 1: Sub-sea Level"
line_color_focus: 0, 0, 0, 1
FloatLayout:
id: buttonarea
Button:
id: shootsubmitbtn
size_hint:0.1,0.05
on_press: root.shoot()
text: "Enter"
pos_hint:{"x": 0.75, "y": 0.75}
答案 0 :(得分:0)
创建一条语句,清除shoot
方法内的输入字段
def shoot(self, *args):
shootxinput_id.text, shootyinput_id.text = ""
# do some other things