MacOS错误与Tkinter代码导致分段错误(SIGSEGV)

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

MacOS error with Tkinter code causing segmentation fault (SIGSEGV)

问题

我理解你需要翻译英文代码段。以下是你提供的Python代码的翻译:

  1. 我正在开发一个Python脚本使用TkinterText小部件来显示文本并查找隐藏字符当我在MacOS上运行我的脚本时它会崩溃并显示分段错误
  2. 我已经将问题缩小到Extension类的find_and_mark方法具体来说崩溃发生在这一行`self.locations_and_markers[index].place(x=x, y=y, anchor="nw")`
  3. 在调试时代码显示在崩溃之前成功运行了三次当去除place方法时代码可以正常运行但没有它的话其他代码就没有意义了
  4. 这是我一直在测试的最小可重现示例
  5. ```python
  6. from __future__ import annotations
  7. from tkinter import Event, Label
  8. from tkinter import Tk, Text
  9. root = Tk()
  10. text = Text(root)
  11. text.pack(fill="both", expand=True)
  12. # 插入带有隐藏字符的文本
  13. text.insert("1.0", "Hello\u00a0 World")
  14. class Marker(Label):
  15. """用于表示隐藏字符的标记"""
  16. def __init__(self, master: Text) -> None:
  17. # 创建标签
  18. self.text = master
  19. super().__init__(master)
  20. def __repr__(self) -> str:
  21. return f"Marker-{self._w}"
  22. class Extension:
  23. """隐藏字符查找器"""
  24. def __init__(self, master: Tk) -> None:
  25. # 设置主窗口和主应用程序
  26. self.master = master
  27. # 设置其他变量
  28. self.locations_and_markers: dict[str, Marker] = {}
  29. # 绑定事件
  30. self.master.bind("<Configure>", self.find_and_mark)
  31. def find_and_mark(self, _: Event | None = None) -> None:
  32. """查找所有隐藏字符并标记它们"""
  33. del _
  34. # 获取文本小部件
  35. # 查找所有隐藏字符
  36. hidden_char_indexes: list[str] = [text.search(
  37. "\u00a0", "1.0", "end", regexp=True, nocase=True
  38. )]
  39. # 移除所有标记
  40. for marker in self.locations_and_markers.values():
  41. marker.destroy()
  42. self.locations_and_markers = {}
  43. if hidden_char_indexes == [""]:
  44. return
  45. # 添加标记
  46. for index in hidden_char_indexes:
  47. # 创建标记
  48. self.locations_and_markers[index] = Marker(text)
  49. # 获取字符的坐标
  50. bbox = text.bbox(index)
  51. # 检查bbox不为None
  52. if bbox is None:
  53. continue
  54. # 检查字符是否可见并在视图中
  55. start: str = text.index("@0,0")
  56. end: str = text.index(f"@{text.winfo_width()},{text.winfo_height()}")
  57. in_view: bool = text.compare(start, "<=", index) and text.compare(
  58. index, "<=", end
  59. )
  60. if not in_view:
  61. continue
  62. # 获取字符的坐标
  63. x: int = bbox[0]
  64. y: int = bbox[1]
  65. # 放置标记
  66. self.locations_and_markers[index].place(x=x, y=y, anchor="nw")
  67. Extension(root)
  68. root.mainloop()

希望这可以帮助你找出导致分段错误的原因并解决问题。谢谢!

英文:

I am working on a Python script that uses the Tkinter Text widget to display text and find hidden characters. When I run my script on MacOS, it crashes with a segmentation fault.

I have narrowed down the issue to the find_and_mark method of the Extension class. Specifically, the crash occurs on this line: self.locations_and_markers[index].place(x=x, y=y, anchor=&quot;nw&quot;).

The code, when debugging, is shown to tun thrice successfully before crashing. The code runs fine when the place is removed but there's no point to any of the other code without it.

Here is the minimum reproducible example I've been testing with:

  1. from __future__ import annotations
  2. from tkinter import Event, Label
  3. from tkinter import Tk, Text
  4. root = Tk()
  5. text = Text(root)
  6. text.pack(fill=&quot;both&quot;, expand=True)
  7. # Insert text with hidden characters
  8. text.insert(&quot;1.0&quot;, &quot;Hello\u00a0 World&quot;)
  9. class Marker(Label):
  10. &quot;&quot;&quot;A marker for a hidden character&quot;&quot;&quot;
  11. def __init__(self, master: Text) -&gt; None:
  12. # Make the label
  13. self.text = master
  14. super().__init__(master)
  15. def __repr__(self) -&gt; str:
  16. return f&quot;Marker-{self._w}&quot;
  17. class Extension:
  18. &quot;&quot;&quot;Hidden character finder&quot;&quot;&quot;
  19. def __init__(self, master: Tk) -&gt; None:
  20. # Set master and mainapp
  21. self.master = master
  22. # Set other variables
  23. self.locations_and_markers: dict[str, Marker] = {}
  24. # Make binds
  25. self.master.bind(&quot;&lt;Configure&gt;&quot;, self.find_and_mark)
  26. def find_and_mark(self, _: Event | None = None) -&gt; None:
  27. &quot;&quot;&quot;Finds all hidden characters and marks them&quot;&quot;&quot;
  28. del _
  29. # Get the text widget
  30. # Find all hidden characters
  31. hidden_char_indexes: list[str] = [text.search(
  32. &quot;\u00a0&quot;, &quot;1.0&quot;, &quot;end&quot;, regexp=True, nocase=True
  33. )]
  34. # Remove all markers
  35. for marker in self.locations_and_markers.values():
  36. marker.destroy()
  37. self.locations_and_markers = {}
  38. if hidden_char_indexes == [&quot;&quot;]:
  39. return
  40. # Add markers
  41. for index in hidden_char_indexes:
  42. # Create the marker
  43. self.locations_and_markers[index] = Marker(text)
  44. # Get the coordinates of the character
  45. bbox = text.bbox(index)
  46. # Check that the bbox is not None
  47. if bbox is None:
  48. continue
  49. # Check that the character is visible and in view
  50. start: str = text.index(&quot;@0,0&quot;)
  51. end: str = text.index(f&quot;@{text.winfo_width()},{text.winfo_height()}&quot;)
  52. in_view: bool = text.compare(start, &quot;&lt;=&quot;, index) and text.compare(
  53. index, &quot;&lt;=&quot;, end
  54. )
  55. if not in_view:
  56. continue
  57. # Get the coordinates of the character
  58. x: int = bbox[0]
  59. y: int = bbox[1]
  60. # Place the marker
  61. self.locations_and_markers[index].place(x=x, y=y, anchor=&quot;nw&quot;)
  62. Extension(root)
  63. root.mainloop()

Can anyone help me figure out what is causing this segmentation fault and how to fix it? Thank you!

答案1

得分: 1

请小心绑定根或顶级窗口。事件处理程序将处理报告给子窗口的事件。

所以,请这样过滤事件。

  1. ...
  2. def find_and_mark(self, e: Event) -> None:
  3. if e.widget != self.master:
  4. return
  5. ...

不管怎样,段错误似乎是Tkinter的一个错误。在我的Linux系统中,它会导致X11 BadWindow错误。关于报告这个问题,您可以在这里报告它。

英文:

You should be careful when binding a root or top level window. The event handler will be called for events which were reported to child widgets.

So, filter events like this.

  1. ...
  2. def find_and_mark(self, e: Event) -&gt; None:
  3. if e.widget != self.master:
  4. return
  5. ...

Anyway, the segmentation fault seems to be a bug of Tkinter. In my Linux box, it causes an X11 BadWindow error. What about reporting it here?

答案2

得分: 0

奇怪的是,它只是在没有after_idle()时进行投诉。添加后修复了它。

英文:

Oddly enough, it was simply complaining over no after_idle() in the bind. Adding that fixed it.

huangapple
  • 本文由 发表于 2023年5月15日 03:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249393.html
匿名

发表评论

匿名网友

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

确定