【问题标题】:How to move cursor into a newly created text box upon creation in tkinter? [duplicate]如何在 tkinter 中创建时将光标移动到新创建的文本框中? [复制]
【发布时间】:2020-09-29 04:01:15
【问题描述】:

我目前正在编写一些代码,这些函数在按下 Enter 键时创建一个新的文本框,并在按下 Tab 键时将文本框跳出。这一切都很好,可以在下面的函数中看到,但是我希望在创建文本框后立即自动单击它,这样我就可以立即开始输入,而不必手动单击它然后开始输入,因为这会变得很慢,尤其是使用选项卡功能,其中新框被删除并创建了几次。我想知道是否有人知道一种将光标自动放入文本框中的方法,或者这是否可能?

 def makeLine(self,event):
    self.x=0
    self.newbox=Text(self.root,width=20, height=3)
    self.newbox.place(x=self.x, y=self.y)
    self.newbox.bind('<Tab>', self.increaseCol)
    self.newbox.bind('<Return>', self.makeLine)
    self.y=self.y+55
    


def increaseCol(self, event):
    event.widget.destroy()
    self.x=self.x+30
    self.tabbedbox=Text(self.root, width=20, height=3)
    self.y=self.y-55
    self.tabbedbox.place(x=self.x, y=self.y)
    self.tabbedbox.bind('<Tab>', self.increaseCol)
    self.y=self.y+55
    self.tabbedbox.bind('<Return>', self.makeLine)

谢谢!

【问题讨论】:

标签: python tkinter


【解决方案1】:

(移动评论回答)

您可以使用focus()focus_set() 将焦点设置在小部件上:

self.newbox.focus()  # also try focus_set()

http://effbot.org/tkinterbook/entry.htm

Setting focus to specific TKinter entry widget

【讨论】:

  • 应该是focus_set() 而不是set_focus()
  • 感谢您的关注。答案已更新。
猜你喜欢
  • 2018-07-28
  • 2013-11-26
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
相关资源
最近更新 更多