python - Tkinter 绘制问题

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

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

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

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

编辑:

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

此外,我正在运行 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()

最佳答案

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

http://bugs.python.org/issue19373


编辑:

通过 ActiveTcl8.6.1.1.297588 安装/重新安装 ActiveTcl8.5.15.1.297588 正在运行!我的 Tkinter GUI 再次响应。

注意:我使用的是 Python 3.3.3。

关于python - Tkinter 绘制问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20416051/

相关文章:

python - 使用 pandas 或 numpy 将不规则列数据读入 python 3.X

Python - 计算有错误的趋势线

python - SQLAlchemy:触发器不在 INSERT 上触发

Python:仅当值是元组/列表而不是字符串时如何迭代

javascript - node.js 应用程序中的系统托盘图标

Java GUI 开发替代 swt 或 swing

python - 在 python 中解析 Robots.txt

jquery - 子 div 的宽度应为 100%

python-3.x - 使用 BeautifulSoup 和 ftlib 访问 ftp 网站时出错

python - 嵌入 Python Zip 文件会抛出错误?