【问题标题】:Python 3 Tkinter Program not launching without IDLEPython 3 Tkinter 程序在没有 IDLE 的情况下无法启动
【发布时间】:2018-03-26 03:04:18
【问题描述】:

我目前正在尝试创建一个 Python3 Tkinter 程序,我可以通过双击或从其他 python 脚本打开来启动它,而不是通过空闲。

到目前为止,我运气不佳,因为当我尝试启动脚本时,控制台会打开一会儿然后崩溃。

编辑:删除标志代码段允许程序运行。任何想法为什么以及如何解决它?另外,在控制台运气不佳之前,我不必通过控制台运行程序。

from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Kinematics")

Logo_frame = LabelFrame(root, bg = "white")
Logo_frame.grid(row=0, column=12, sticky = "NSEW")

#Logo
Logo = Image(file="Logo-S.gif")
image_label = ttk.Label(Logo_frame, image=Logo)
image_label.grid(row=0, column=0, rowspan = 3)

root.mainloop()

【问题讨论】:

  • 针对“控制台打开片刻然后崩溃”问题的一般调试技术:从控制台运行程序,这样您就可以看到错误消息的实际内容。
  • 请不要发布工作代码。发布导致错误的代码,然后在您的问题中包含错误。
  • 我试图在不使用 IDLE 的情况下启动它,但当我尝试它不起作用时。那里的代码,特别是#Logo 部分似乎破坏了它。

标签: python python-3.x tkinter


【解决方案1】:

您当前代码的错误是:

Exception ignored in: <bound method Image.__del__ of <tkinter.Image object at 0x7f1aa91df2e8>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/__init__.py", line 3357, in __del__
    if self.name:
AttributeError: 'Image' object has no attribute 'name'
Traceback (most recent call last):
  File "/home/sunbear/Coding/python35/tkinter/stackoverflow_questions/test52.py", line 22, in <module>
    Logo = Image(file="Logo-S.gif")
TypeError: __init__() missing 1 required positional argument: 'imgtype'

第 22 行的命令中有错字(编辑前的原始代码)。您需要以下更正来克服此错误,即:

#Logo
#Logo = Image(file="Logo-S.gif")
Logo = PhotoImage(file="Logo-S.gif") #you should change to this.

您可以通过我给您的链接了解更多关于何时使用PhotoImage and Image 的信息。

【讨论】:

  • 感谢您的回复,代码现在可以在 IDLE 中运行,但如果不通过 IDLE 就无法启动它。
  • 在我的linux终端中,输入$ python3 kinematics.py后,你的GUI就会出现。当你做同样的事情时会发生什么?你得到什么错误消息?我已将您的脚本命名为kinematics.py。您必须在与脚本相同的目录中执行此命令。
  • 我目前正在运行 Windows 10,这就是发生的情况。 '$' 不是内部或外部命令、可运行程序或批处理文件。
  • 在 linux 中,$ 指的是命令提示符。最后,只需输入python3 kinematics.py。有什么错误吗?
  • 太棒了。简而言之,您的情况是由两个问题引起的。一个与 tkinter 命令错误有关,另一个与 Windows 上的 python 设置有关。请点击“勾选”标记接受此答案。谢谢。
【解决方案2】:

我只是尝试使用我在网上找到的随机 gif 运行程序,一切似乎都按计划进行。我正在运行 Windows 10 专业版...也许可以尝试重新保存/下载 gif。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2015-01-28
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多