【问题标题】:Python 3.6, using tkinter, gives me the spinning beachball forever. (mac)Python 3.6,使用 tkinter,给了我永远旋转的沙滩球。 (苹果)
【发布时间】:2018-02-08 09:07:49
【问题描述】:

我在 mac 上使用 python 3.6。我最近刚刚开始使用 GUI,并从一个简单的 messagebox.showinfo 弹出窗口开始。但是,当我运行程序时,会显示消息框,但是当我单击“确定”时,它并没有消失,我只是被旋转的沙滩球困住,直到我手动关闭程序。

我知道这不是我的代码的问题,因为它在 windows 操作系统上运行良好,因为我也在 windows 计算机上运行了代码。

这是弹出消息框的函数:

def greeting():
    greetingFile = open("greetingFile.txt", "w")
    greetingFile.write(messagebox.showinfo("Welcome to the board game!","In this game, you will navigate"\
                        " across a 7x7 grid and the first player to reach square 49 wins."\
                        " Both players start on space 1."))
greetingFile.close()
return

这是我调用函数的代码的主要部分:

greeting()
greetingFile = open("greetingFile.txt", "r")
greetingFile.close()

这是我编写的新代码,它应该做同样的事情:显示一个消息框,然后关闭。

import tkinter
from tkinter import messagebox

root = tkinter.Tk() 
root.withdraw()

def greeting():
    greetingFile = open("greetingFile.txt", "w")
    greetingFile.write(messagebox.showinfo("Welcome to the board game!", "In this game, you will navigate"\
                        " across a 7x7 grid and the first player to reach square 49 wins."\
                        " Both players start on space 1."))
    greetingFile.close()
    return

greeting()
greetingFile = open("greetingFile.txt", "r")
greetingFile.close()

当我运行它时,它可以工作,但是当我单击“确定”关闭消息框时,它会冻结并出现旋转的沙滩球。当我在 Windows pc 上运行它时不会发生这种情况

【问题讨论】:

  • 请将代码发布为格式化文本。
  • 有代码,但我不认为代码有什么问题,而是操作系统有问题
  • 看,macOS 是由至少 数十 名专业程序员在 16 年 中构建的。因此,您的代码中更有可能存在问题。另外,这不是完整代码,请提供minimal reproducible example
  • 我提供了一个新代码,这是我正在尝试做的,但它仍然不会让我关闭消息框。
  • 你为什么要将消息框写入文件?

标签: python macos python-3.x tkinter


【解决方案1】:

还有一些和你类似的问题。

这可能是由于在程序结束时没有调用mainloop() 造成的。

您的示例代码并未向我们展示您正在使用root.mainloop(),因此请尝试将其添加到您的程序末尾,看看是否有帮助。

如果这没有帮助,您可以在消息框对话框之前添加root.update()

这是您的代码的修改版本,添加了 update()mainloop()。让我知道这是否有帮助。

import tkinter
from tkinter import messagebox

root = tkinter.Tk() 
root.withdraw()

def greeting():
    greetingFile = open("greetingFile.txt", "w")

    root.update() # added the update method to force an update of the events
    greetingFile.write(messagebox.showinfo("Welcome to the board game!", "In this game, you will navigate"\
                        " across a 7x7 grid and the first player to reach square 49 wins."\
                        " Both players start on space 1."))
    greetingFile.close()
    return

greeting()
greetingFile = open("greetingFile.txt", "r")
greetingFile.close()

root.mainloop() # needed for all tkinter programs to work properly.

另外,我们可以从 write 方法中单独调用 messagebox。这可能是程序挂断的一部分。

所以改为将 showinfo 设置为一个变量,然后在 write 方法中使用该变量。

代码:

import tkinter
from tkinter import messagebox

root = tkinter.Tk() 
root.withdraw()

def greeting():
    greetingFile = open("greetingFile.txt", "w")
    my_var = messagebox.showinfo("Welcome to the board game!", "In this game, you will navigate"\
                        " across a 7x7 grid and the first player to reach square 49 wins."\
                        " Both players start on space 1.")
    greetingFile.write(my_var)
    greetingFile.close()
    # I changed as little code as possible but just to let you know the use
    # of return here does nothing for your program.
    return

greeting()
greetingFile = open("greetingFile.txt", "r")
greetingFile.close()

如果您无法解决您所拥有的消息框和 mac 问题,最后一个选择是创建您自己的消息框。

我们可以使用Toplevel() 完成此操作,并且您可以按照自己想要的方式格式化所有内容,这与功能有限的消息框不同。

下面有一个例子。让我知道这是否有帮助。

import tkinter as tk

root = tk.Tk() 
root.withdraw()

def save_greeting_verification(top):
    greetingFile = open("greetingFile.txt", "w")
    greetingFile.write("Ok")
    greetingFile.close()
    top.destroy()

def greeting():
    top = tk.Toplevel(root)
    top.geometry("350x80")
    top.resizable(False, False)
    top.protocol("WM_DELETE_WINDOW", lambda: save_greeting_verification(top))
    top.title("Welcome to the board game!")
    tk.Label(top, text = "In this game, you will navigate\nacross a 7x7 grid and the first player to reach square 49 wins.\nBoth players start on space 1.").pack()
    tk.Button(top, text = "ok", command = lambda: save_greeting_verification(top)).pack()

greeting()
greetingFile = open("greetingFile.txt", "r")
print(greetingFile.read())
greetingFile.close()

root.mainloop()

【讨论】:

  • 感谢您的回复。我已经尝试过了,但消息框仍然不会关闭并给我沙滩球。这就是为什么我认为这是操作系统或我的计算机的问题。
  • @A.Dedich 这可能是 python 或 tkinter 的版本错误。
  • @A.Dedich 我更新了我的示例,因为我认为需要在消息框对话框之前调用更新。
  • 我已尝试运行您的代码的更新版本(感谢您的提示),但同样的问题仍然存在。我曾尝试切换到不同版本的 python,但没有任何变化。
  • @A. Dedich 接缝很奇怪 2 我。许多人在 mac 中遇到过不同消息框的问题,问题通常是 mainloop()update()。如果我找到了我会更新我的答案的东西,我会继续搜索。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多