Tkinter在使用文本小部件时出现问题。

huangapple go评论95阅读模式
英文:

Tkinter problem while using the text widget

问题

我有这段代码来使每个文本小部件(Text() widget)可点击,该文本小部件使用了tkinter。
然而,当我运行程序并单击文本时,我会收到每个相应文本行的错误消息:

_tkinter.TclError: bad text index "This is the first string."
_tkinter.TclError: bad text index "This is the second string."
_tkinter.TclError: bad text index "This is the third string."

如果有人知道解决此问题的方法,请告诉我,我将永远感激不尽。以下是完整的代码:

  1. import tkinter as tk
  2. def text_click(event):
  3. index = text_widget.tag_names(tk.CURRENT)[0]
  4. clicked_text = text_widget.get(index)
  5. print(clicked_text)
  6. # 创建主窗口
  7. window = tk.Tk()
  8. # 创建一个Text小部件
  9. text_widget = tk.Text(window, wrap="word")
  10. text_widget.pack()
  11. # 字符串列表
  12. texto = ["This is the first string.", "This is the second string.", "This is the third string."]
  13. # 插入列表中的每个字符串
  14. for string in texto:
  15. text_widget.insert(tk.END, string + "\n")
  16. # 为每个插入的字符串应用一个标签
  17. tag_start = f"{text_widget.index(tk.END)}-{len(string) - 1}c"
  18. tag_end = f"{text_widget.index(tk.END)}-1c"
  19. text_widget.tag_add(string, tag_start, tag_end)
  20. # 将点击事件绑定到标签
  21. text_widget.tag_bind(string, "<Button-1>", text_click)
  22. # 使文本小部件为只读
  23. text_widget.configure(state="disabled")
  24. # 启动tkinter事件循环
  25. window.mainloop()

我期望在单击每行文本时打印每行的文本。

英文:

I have this code to make each text "clickleable", from the Text() widget that has tkinter.
However, when I run the program and click over one text I get this error message witch each corresponding
text line:

_tkinter.TclError: bad text index &quot;This is the first string.&quot;
_tkinter.TclError: bad text index &quot;This is the second string.&quot;
_tkinter.TclError: bad text index &quot;This is the third string.&quot;

If anyone may now a way to solve the issue, I be forever grateful. Here is the complete code:

  1. import tkinter as tk
  2. def text_click(event):
  3. index = text_widget.tag_names(tk.CURRENT)[0]
  4. clicked_text = text_widget.get(index)
  5. print(clicked_text)
  6. # Create the main window
  7. window = tk.Tk()
  8. # Create a Text widget
  9. text_widget = tk.Text(window, wrap=&quot;word&quot;)
  10. text_widget.pack()
  11. # List of strings
  12. texto = [&quot;This is the first string.&quot;, &quot;This is the second string.&quot;, &quot;This is the third string.&quot;]
  13. # Insert each string from the list
  14. for string in texto:
  15. text_widget.insert(tk.END, string + &quot;\n&quot;)
  16. # Apply a tag to each inserted string
  17. tag_start = f&quot;{text_widget.index(tk.END)}-{len(string) - 1}c&quot;
  18. tag_end = f&quot;{text_widget.index(tk.END)}-1c&quot;
  19. text_widget.tag_add(string, tag_start, tag_end)
  20. # Bind the click event to the tag
  21. text_widget.tag_bind(string, &quot;&lt;Button-1&gt;&quot;, text_click)
  22. # Make the text widget read-only
  23. text_widget.configure(state=&quot;disabled&quot;)
  24. # Start the tkinter event loop
  25. window.mainloop()

I am expecting to print the text of each line when clicked over it.

答案1

得分: 2

If you print out index, you'll see it's the string in the error: &quot;This is the first string.&quot;. That is not a valid index. An index needs to be something like line.character or @x,y, and a few other special strings (eg: &quot;end&quot;, &quot;current&quot;, etc).

> I am expecting to print the text of each line when clicked over it.

You can use an index of the form @x,y, and you can use the x and y attributes of the event to create that index. Another alternative is to use the index &quot;current&quot; which represents the character closest to the mouse.

You can use &quot;linestart&quot; and &quot;lineend&quot; to translate that index to the start and end of the line.

Example:

  1. def text_click(event):
  2. start_index = text_widget.index(f&quot;@{event.x},{event.y} linestart&quot;)
  3. end_index = text_widget.index(f&quot;@{event.x},{event.y} lineend&quot;)
  4. clicked_text = text_widget.get(start_index, end_index)
  5. print(clicked_text)
英文:

If you print out index, you'll see it's the string in the error: &quot;This is the first string.&quot;. That is not a valid index. An index needs to be something like line.character or @x,y, and a few other special strings (eg: &quot;end&quot;, &quot;current&quot;, etc).

> I am expecting to print the text of each line when clicked over it.

You can use an index of the form @x,y, and you can use the x and y attributes of the event to create that index. Another alternative is to use the index &quot;current&quot; which represents the character closest to the mouse.

You can use &quot;linestart&quot; and &quot;lineend&quot; to translate that index to the start and end of the line.

Example:

  1. def text_click(event):
  2. start_index = text_widget.index(f&quot;@{event.x},{event.y} linestart&quot;)
  3. end_index = text_widget.index(f&quot;@{event.x},{event.y} lineend&quot;)
  4. clicked_text = text_widget.get(start_index, end_index)
  5. print(clicked_text)

答案2

得分: 0

你可以像这样修改你的 text_click 函数:

  1. def text_click(event):
  2. index = text_widget.index(tk.CURRENT)
  3. clicked_text = text_widget.get(index + " linestart", index + " lineend")
  4. print(clicked_text)

这会使用 text_widget.index(tk.CURRENT) 获取被点击文本的索引,然后通过添加适当的 linestartlineend 修饰符来检索实际文本。

英文:

You can modify your text_click function like this:

  1. def text_click(event):
  2. index = text_widget.index(tk.CURRENT)
  3. clicked_text = text_widget.get(index + &quot; linestart&quot;, index + &quot; lineend&quot;)
  4. print(clicked_text)

This retrieves the index of the clicked text using text_widget.index(tk.CURRENT), then it uses that index to retrieve the actual text by adding the appropriate linestart and lineend modifiers to the index.

huangapple
  • 本文由 发表于 2023年5月26日 12:16:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定