【问题标题】:Tkinter Draw ProblemsTkinter 绘图问题
【发布时间】:2013-12-23 08:11:08
【问题描述】:

我刚刚为编程课编写了基于 Tkinter 的棋盘游戏奥赛罗 GUI。一切似乎都正常工作,但奇怪的事情仍在发生:游戏板 GUI 上发生的任何更改只有在我在窗口外单击时才会更新。

这不仅发生在我的应用程序中,而且发生在我的讲师编写的其他基于 Tkinter 的 GUI 应用程序中——仅在我的机器上运行时发生。我已经看到这些应用程序在其他机器上正常工作,这让我相信我的机器有一些特定的东西导致了这个问题,因此,我没有在我的问题中包含任何代码。

有人对此有任何见解吗?如果我没有提供足够的细节,我很抱歉;我不确定要包括什么。我正在使用 Python 3.3.2 和 Tkinter 8.5

编辑:

这是我的讲师编写的 GUI 代码。这是一个简单的应用程序,可以在用户单击的任何位置在画布上创建椭圆。它在学校的机器上运行良好,但在我的电脑上,当我点击画布时,直到我点击 tkinter 窗口之外什么都没有发生。在窗口外点击后,点绘制正确。

另外,我正在运行 OS X Mavericks (10.9)。

import coordinate
import spots_engine
import tkinter

class SpotsApplication:
    def __init__(self, state: spots_engine.SpotsState):
        self._state = state
        self._root_window = tkinter.Tk()
        self._canvas = tkinter.Canvas(
            master = self._root_window, width = 500, height = 450,
            background = '#006000')
        self._canvas.grid(
            row = 0, column = 0, padx = 10, pady = 10,
            sticky = tkinter.N + tkinter.S + tkinter.E + tkinter.W)
        self._canvas.bind('<Configure>', self._on_canvas_resized)
        self._canvas.bind('<Button-1>', self._on_canvas_clicked)
        self._root_window.rowconfigure(0, weight = 1)
        self._root_window.columnconfigure(0, weight = 1)


    def start(self) -> None:
        self._root_window.mainloop()


    def _on_canvas_resized(self, event: tkinter.Event) -> None:
        self._redraw_all_spots()


    def _on_canvas_clicked(self, event: tkinter.Event) -> None:
        width = self._canvas.winfo_width()
        height = self._canvas.winfo_height()
        click_coordinate = coordinate.from_absolute(
            (event.x, event.y), (width, height))
        self._state.handle_click(click_coordinate)
        self._redraw_all_spots()


    def _redraw_all_spots(self) -> None:
        self._canvas.delete(tkinter.ALL)
        canvas_width = self._canvas.winfo_width()
        canvas_height = self._canvas.winfo_height()
        for spot in self._state.all_spots():
            center_x, center_y = spot.center_coordinate().absolute(
                (canvas_width, canvas_height))
            radius_x = spot.radius_frac() * canvas_width
            radius_y = spot.radius_frac() * canvas_height

            self._canvas.create_oval(
                center_x - radius_x, center_y - radius_y,
                center_x + radius_x, center_y + radius_y,
                fill = '#ffff00', outline = '#000000')


if __name__ == '__main__':
    SpotsApplication(spots_engine.SpotsState()).start()

【问题讨论】:

  • 代码可能有帮助,所以只需编辑并添加到最后。此外,您的机器/系统详细信息 (OS)。
  • 将代码添加到您的答案中,显示一个最小的应用程序,说明您的系统上出现的问题,而不是其他系统上出现的问题(并告诉我们您的系统运行的操作系统平台)。
  • 确定那是你的真实代码吗?有很多语法错误。例如-&gt; None:event: tkinter.Event
  • 这些是类型注释。在 Python 3 中它们是允许的。

标签: python user-interface python-3.x tkinter


【解决方案1】:

如果我没记错的话,我们在这里讨论的问题是一个已知的错误:

http://bugs.python.org/issue19373


编辑:

ActiveTcl8.6.1.1.297588 上安装/重新安装ActiveTcl8.5.15.1.297588 正在工作!我的 Tkinter GUI 再次响应。

注意:我使用的是 Python 3.3.3。

【讨论】:

  • 谢谢,成功了。在 32 位模式下运行 python 也可以解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多