Piepan
Piepan

Reputation: 1

Why does the tkinter font create an attribute error?

I'm creating a simple UI Tkinter to toggle some relais and parallel show some sensor data in a label; currently I want to display that data in the console as well. When running the code I get an attribute error that the "Nonetype object has no attribute call".

The error looks like this:

Traceback (most recent call last):
  File "/home/pi/corect.py", line 14, in <module>
    FONT = tkinter.font.Font(family="Helvetica", size=12, weight="bold")
  File "/usr/lib/python3.7/tkinter/font.py", line 93, in __init__
    tk.call("font", "create", self.name, *font)
AttributeError: 'NoneType' object has no attribute 'call'

Code looks like this:


from functools import partial
import tkinter as tk
import tkinter.font

from adafruit_ads1x15.ads1115 import ADS1115, P0
from adafruit_ads1x15.analog_in import AnalogIn
import board
import busio
from gpiozero import DigitalOutputDevice
from RPi import GPIO


FONT = tkinter.font.Font(family="Helvetica", size=12, weight="bold")


def toggle_releais_a(relais, widget):

    widget["text"] = "Turn Valve Up" if relais.is_active else "Stop"


def toggle_relais_b(relais, widget):
    widget["text"] = "Turn Valve Up" if relais.is_active else "Stop"
    relais.toggle()


def main():
    try:
        GPIO.setmode(GPIO.BCM)
        ads = ADS1115(busio.I2C(board.SCL, board.SDA))
        channel = AnalogIn(ads, P0)
        print(channel.value, channel.voltage)

        relais_a = DigitalOutputDevice(23)
        relais_b = DigitalOutputDevice(24)

        window = tk.Tk()
        window.title("Valve Toggler")

        options = dict(font=FONT, bg="bisque2")
        tk.Button(
            window, text="Exit", command=window.quit, width=12, **options
        ).grid(row=1, column=2)

        options["width"] = 24
        tk.Label(window, text="Value Valve 1", **options).grid(row=1, column=1)

        relais_a_button = tk.Button(window, text="Turn Valve Up", **options)
        relais_a_button.grid(row=2, column=1)

        relais_b_button = tk.Button(window, text="Turn Valve down", **options)
        relais_b_button.grid(row=3, column=1)


        relais_a_button["command"] = partial(
            toggle_releais_a, relais_a, relais_a_button
        )
        relais_b_button["command"] = partial(
            toggle_relais_b, relais_b, relais_b_button
        )

        window.mainloop()
    finally:
        GPIO.cleanup()


if __name__ == "__main__":
    main()


Upvotes: 0

Views: 272

Answers (1)

en_lorithai
en_lorithai

Reputation: 1260

FONT = tkinter.font.Font(family="Helvetica", size=12, weight="bold")

This thing needs a master (root)

This should work

window = tk.Tk()
FONT = tkinter.font.Font(root=window,family="Helvetica", size=12, weight="bold")

Upvotes: 1

Related Questions