【问题标题】:Set focus for python turtle textinput为 python turtle textinput 设置焦点
【发布时间】:2023-02-20 12:37:50
【问题描述】:

在我的 python 项目(游戏)中,我有一个获取玩家 (2) 名称的函数:

def get_names(self):
    self.lp_name = turtle.textinput("Left Player", "Enter your Name")
    self.rp_name = turtle.textinput("Right Player", "Enter your Name")

enter image description here

对于第一个(左侧玩家),文本输入字段处于活动状态 - 具有焦点但对第二个(右侧玩家)没有

【问题讨论】:

  • 请提供足够的代码,以便其他人可以更好地理解或重现问题。
  • 我在我的系统上对此进行了测试,它工作正常,请提供有关运行此代码的环境的详细信息。
  • 它是 Windows 10,家庭版,x64;蟒蛇 3.10.0

标签: python focus turtle-graphics textinput


【解决方案1】:

文本输入字段未激活,因为当您单击“确定”时焦点从主窗口转移到弹出窗口。(尝试单击主窗口,您可以看到文本输入字段变为活动状态。)

因此,您可以使用 screen.tracer() 函数在每次输入后更新主屏幕。

样本就像:

screen = turtle.Screen()

def get_names(self):

    self.lp_name = turtle.textinput("Left Player", "Enter your Name")

    screen.tracer(1) #Refreshes the turtle window and sets focus to the main window.

    self.rp_name = turtle.textinput("Right Player", "Enter your Name")

这应该工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2017-12-18
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多