gpt4 book ai didi

python - Tkinter 对象在 Linux 上不工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:06:06 25 4
gpt4 key购买 nike

所以,我最近从 Windows 切换到 Linux Ubuntu。我的规范是使用 Tkinter 进行 GUI 应用程序。但从开关中我也意识到 Tkinter 对象无法识别。

IE

#!/usr/bin/env python
import Tkinter
Window = Tkinter.Tk()
Welcome = Label(Window,"Welcome to Dragive")
Welcome.pack()
Window.mainloop()

给我以下错误:

NameError: name 'Label' is not defined

看到如此简单的脚本如何无法被解释,这感觉太荒谬了。

是的,在意识到 Tkinter 没有预装 linux python 后,我安装了 python-tk。 (其实很烦人)

我已经尝试过

from Tkinter import *

但这给了我这个错误:

    Welcome = Label(Window,"Welcome to Dragive")
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2595, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2089, in __init__
for k in cnf.keys():
AttributeError: 'str' object has no attribute 'keys'

这一切都非常令人恼火。当然,在任何其他情况下,这都会在 Windows 中运行。到目前为止,Linux 在这个例子中除了麻烦之外什么也没给我带来。

如有任何帮助,我们将不胜感激,谢谢。

最佳答案

这些错误与 Linux 和 Windows 无关。相同的代码在任一平台上都会产生相同的错误。

如果使用 import Tkinter 导入 Tkinter,则必须为所有 tkinter 对象添加 Tkinter. 前缀:

Welcome = Tkinter.Label(...)

这比 from Tkinter import * 好得多,不过我更喜欢 import Tkinter as tk 这样前缀会短一些(例如:tk.Label(...))

至于错误消息AttributeError: 'str' object has no attribute 'keys',这是因为您忽略了告诉 tkinter 将文本应用到哪个属性。您需要将代码更改为如下所示(注意“text =”):

Welcome = Label(Window,text="Welcome to Dragive")

关于python - Tkinter 对象在 Linux 上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37632362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com