当我在 gpiozero 库的按钮语句之后添加代码时,为什么 tkinter 会导致错误?
Why does tkinter cause an error when I add the code after a button statement from gpiozero library?
我正在制作一个防护程序并尝试使用 tkinter gui 库。我程序的原始代码使用 gpiozero 库将引脚连接到按钮。这段代码本身工作得很好,但是当我添加 tkinter 代码时,如下面的代码段所示(特别是在按钮语句之后),python shell 抛出一个 AttributeError。此外,当我在按钮语句之前放置以“main = Tk()”开头的代码时,tkinter gui 会继续 运行 并打开 gui window。最终我遇到的问题是,出于某种原因,当与 tkinter 相关的代码是 运行ning 时,防护代码似乎受到阻碍并且似乎没有 运行.
from gpiozero import LED, Button
from tkinter import *
left_score = 0
game_left = Button(16)
main = Tk()
ourMessage str(left_score)
messageVar = Label(main, text = ourMessage)
messageVar.config(anchor = S, bg ="lightgreen", bd = "800”, font = ("Courier”, 70))
messageVar.pack()
main.mainloop()
这里是错误:
>>>
Traceback (most recent call last):
File "/home/pi/fence_tkinter .py”, line 62, in <module>
game_left = Button(16)
File "/usr/lib/python3.4/tkinter/__init__.py”, line 2195, in __init__
Widget.__init__(self, master, 'button' , cnf, kw)
File "/usr/lib/python3.4/tkinter/__init__.py”, line 2118, in __init__
BaseWidget._setup(self, master, cnf)
File "/usr/lib/python3.4/tkinter/__init__.py”, line 2096, in _setup
self. tk = master. tk
AttributeError: 'int' object has no attribute 'tk'
因为您正在进行通配符导入,tkinter Button
class 正在覆盖 gpiozero class。这就是不鼓励通配符导入的原因。并且因为您已经覆盖了 gpiozero class,您正在将一个整数传递给 tkinter Button
class 它需要一个小部件。
您应该以不同的方式导入 tkinter:
from gpiozero import LED, Button
import tkinter as tk
left_score = 0
game_left = Button(16)
main = tk.Tk()
ourMessage str(left_score)
messageVar = tk.Label(main, text = ourMessage)
messageVar.config(anchor = S, bg ="lightgreen", bd = "800”, font = ("Courier”, 70))
messageVar.pack()
main.mainloop()
我正在制作一个防护程序并尝试使用 tkinter gui 库。我程序的原始代码使用 gpiozero 库将引脚连接到按钮。这段代码本身工作得很好,但是当我添加 tkinter 代码时,如下面的代码段所示(特别是在按钮语句之后),python shell 抛出一个 AttributeError。此外,当我在按钮语句之前放置以“main = Tk()”开头的代码时,tkinter gui 会继续 运行 并打开 gui window。最终我遇到的问题是,出于某种原因,当与 tkinter 相关的代码是 运行ning 时,防护代码似乎受到阻碍并且似乎没有 运行.
from gpiozero import LED, Button
from tkinter import *
left_score = 0
game_left = Button(16)
main = Tk()
ourMessage str(left_score)
messageVar = Label(main, text = ourMessage)
messageVar.config(anchor = S, bg ="lightgreen", bd = "800”, font = ("Courier”, 70))
messageVar.pack()
main.mainloop()
这里是错误:
>>>
Traceback (most recent call last):
File "/home/pi/fence_tkinter .py”, line 62, in <module>
game_left = Button(16)
File "/usr/lib/python3.4/tkinter/__init__.py”, line 2195, in __init__
Widget.__init__(self, master, 'button' , cnf, kw)
File "/usr/lib/python3.4/tkinter/__init__.py”, line 2118, in __init__
BaseWidget._setup(self, master, cnf)
File "/usr/lib/python3.4/tkinter/__init__.py”, line 2096, in _setup
self. tk = master. tk
AttributeError: 'int' object has no attribute 'tk'
因为您正在进行通配符导入,tkinter Button
class 正在覆盖 gpiozero class。这就是不鼓励通配符导入的原因。并且因为您已经覆盖了 gpiozero class,您正在将一个整数传递给 tkinter Button
class 它需要一个小部件。
您应该以不同的方式导入 tkinter:
from gpiozero import LED, Button
import tkinter as tk
left_score = 0
game_left = Button(16)
main = tk.Tk()
ourMessage str(left_score)
messageVar = tk.Label(main, text = ourMessage)
messageVar.config(anchor = S, bg ="lightgreen", bd = "800”, font = ("Courier”, 70))
messageVar.pack()
main.mainloop()