1

I am calling a function(Kleben_Tabelle.Taula_G3110()). The code of this function is in another file. In the function I send binary data to turn on a series of LEDS (code in Arduino).

Everything works if I don't use any call to the function, but of course putting all this code in a function and calling it makes the code cleaner, correct and less lines of code.

When the function is called I get this error:

arduino.write(bytes(b'T'))
NameError: name 'arduino' is not defined

This is the main code:

from tkinter import *
from PIL import Image, ImageTk
from tkinter import ttk
from tkinter import messagebox
import serial
import time
import PIL.Image
import Kleben_Tabelle
#from Kleben_Tabelle import *




root = Tk()
root.geometry("1000x600")
# root.resizable (False,False)
root.title("Combobox")

arduino = serial.Serial("COM7", 9600)
time.sleep(0.1)  # reducir el uso de la CPU.Prueba con diferentes valores (0.01 a 0.1)


def cambiar():
    mes = combo.get()
    if mes == "G3110":
        label_resultat.configure(text=txt)
        Kleben_Tabelle.Taula_G3110()

    if mes == "G3111":
        label_resultat.configure(text=txt2)
        Kleben_Tabelle.Taula_G3111()
    if mes == "G3112":
        messagebox.showinfo("Mes", "Marzo")



def apagarLED():
    arduino.write(b'4')
    time.sleep(1)


def cerrarInterfaz():
    # cerrar comunicación Serial
    global raiz
    arduino.close()
    # cerrar ventana
    root.destroy()

image = Image.open ('xxxxxxx.png')
photo_image = ImageTk.PhotoImage(image)
label = Label(root, image=photo_image)
label.pack()

frame_resultat = Frame(root, width=400, height=100, relief="flat", highlightbackground="blue", highlightthickness=1)
frame_resultat.place(x=250, y=200)

label_resultat = Label(root, text="", bg="yellow", borderwidth=0, relief="groove", width=20, height=2, justify='left',
                       highlightbackground="blue", highlightthickness=1)
label_resultat.place(x=80, y=200)

etiqueta = Label(root, text="Zelle: Kleben")
etiqueta.place(x=100, y=40)

combo = ttk.Combobox(root, state="readonly")
combo.place(x=100, y=70)
combo["values"] = ("G3110", "G3111", "G3112", "1")
combo.current(0)

boton = Button(root, text="Cambiar mes", command=cambiar)
boton.place(x=100, y=100)

# boton de apagado del LED
btnApagar = ttk.Button(root, text="Reset", command=apagarLED)
# btnApagar = ttk.Button(raiz,text ="Reset",command = clearTextInput)
btnApagar.place(x=420, y=450)
# boton de Cerrar interfaz
btnCerrar = ttk.Button(root, text="Cerrar", command=cerrarInterfaz)
btnCerrar.place(x=420, y=480)

txt = ("G3110 Frontabdeckung")
txt2 = ("G3111 Frontabdeckung")

root.mainloop()

And this is the code of the function to call (in another *.py file : Kleben_Tabelle.py)

def Taula_G3110():
    arduino.write(bytes(b'T'))
    time.sleep(1)
    arbol = ttk.Treeview(frame_resultat,columns=("Bauteile","Regal","Lager"))
    arbol.column ('#0',width=100)
    arbol.column ('Bauteile',width=100)
    arbol.column ('Regal',width=80)
    arbol.column ('Lager',width=80)
    arbol.insert("",END,text="G3110",values=("P6400","K2.0001.01","Regal 2"))
    arbol.insert("",END,text="G3110",values=("P6406XA","K1.0004.01"))
    arbol.insert("",END,text="G3110",values=("P6403XA","K1.0003.01"))
    arbol.heading("#0",text="Model")
    arbol.heading("Bauteile",text="Bauteile")
    arbol.heading("Regal",text="Regal")
    arbol.place(x=100,y=70)
    arbol.pack()


def Taula_G3111():

    arbol = ttk.Treeview(frame_resultat,columns=("Bauteile","Regal","Lager"))
    arbol.column ('#0',width=100)
    arbol.column ('Bauteile',width=100)
    arbol.column ('Regal',width=80)
    arbol.column ('Lager',width=80)
    arbol.insert("",END,text="G3110",values=("P6401","K2.0002.02","Regal 2"))
    arbol.insert("",END,text="G3110",values=("P6404XA","K2.0003.02","Regal 2"))
    arbol.insert("",END,text="G3110",values=("",""))
    arbol.heading("#0",text="Model")
    arbol.heading("Bauteile",text="Bauteile")
    arbol.heading("Regal",text="Regal")
    arbol.place(x=100,y=70)
    arbol.pack()
3
  • NONE of the variables that your functions are using are defined in the second file, they are only meaningful in your main script - arduino simply happened to be the first one encountered. Keeping the functions in the main script file would be the easiest solution. Commented Jan 26, 2022 at 17:23
  • Hallo Jasonharper, What do you mean by "Keeping" the functions in the main Script file? Commented Jan 26, 2022 at 17:34
  • I mean, don't put the functions in a separate file. Commented Jan 26, 2022 at 17:42

1 Answer 1

1

The module you import the Taula_G3110() function from does not have access to the variables defined in your main script. If you want that function to have access to them, you need to pass them through function arguments,

Make sure you're importing everything you need from the Python modules in your second file and pass the arduino variable through a function argument.

import time
from tkinter import ttk

def Taula_G3110(arduino, frame_resultat):
    arduino.write(bytes(b'T'))
    time.sleep(1)
    arbol = ttk.Treeview(frame_resultat,columns=("Bauteile","Regal","Lager"))
    arbol.column ('#0',width=100)
    arbol.column ('Bauteile',width=100)
    arbol.column ('Regal',width=80)
    arbol.column ('Lager',width=80)
    arbol.insert("",END,text="G3110",values=("P6400","K2.0001.01","Regal 2"))
    arbol.insert("",END,text="G3110",values=("P6406XA","K1.0004.01"))
    arbol.insert("",END,text="G3110",values=("P6403XA","K1.0003.01"))
    arbol.heading("#0",text="Model")
    arbol.heading("Bauteile",text="Bauteile")
    arbol.heading("Regal",text="Regal")
    arbol.place(x=100,y=70)
    arbol.pack()

Then in your main code, you can call this function and pass through the arduino variable for the function to reference:

...

arduino = serial.Serial("COM7", 9600)

...

def cambiar():
    mes = combo.get()
    if mes == "G3110":
        label_resultat.configure(text=txt)
        Kleben_Tabelle.Taula_G3110(arduino, frame_resultat)

    if mes == "G3111":
        label_resultat.configure(text=txt2)
        Kleben_Tabelle.Taula_G3111(arduino, frame_resultat)
    if mes == "G3112":
        messagebox.showinfo("Mes", "Marzo")

...

It's not pretty, but it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Brian!! You are great!! I will try it today...looks like gut!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.