I am making a fencing program and am trying to utilize the tkinter gui library. The original code for my program uses the gpiozero library to connect pins to the button. This code works perfectly fine on its own, however when I add the tkinter code as shown in the code segment below (specifically after the button statement), python shell throws me an AttributeError. Furthermore, when I place the code starting with "main = Tk()" before the button statement, the tkinter gui proceeds to run and open the gui window. Ultimately the issue I am having is that for some reason, when the tkinter related code is running, the fencing code seems to be hindered and doesn't appear to run.
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()
Here is the error:
>>>
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'
Because you are doing a wildcard import, the tkinter Button
class is overwriting the gpiozero class. This is why wildcard imports are discouraged. And because you have overwritten the gpiozero class, you are passing an integer to the tkinter Button
class where it expects a widget.
You should import tkinter in a different way:
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()