简体   繁体   中英

Python: calling function inside if statement from another File

I have an error in my program: "Taula_G3110 () NameError: name 'Taula_G3110' is not defined"

My question is: I want to call a function that is inside an IF loop.This function is in another file. I have tried everything with imports: "from Kleben_Tabelle import Taula_G3110, Taula_G3111 . import Kleben_Tabelle ", but there is no way.

Does anyone have an idea what I'm doing wrong?

Code:

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


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)
        Taula_G3110()

    if mes == "G3111":
        label_resultat.configure(text=txt2)
        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 ('Edifici_Knauer_blau.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 ist my other File with this Module-Function:(File: Kleben_Tabelle.py)

def Taula_G3110():
    arduino.write(bytes(b'T'))
    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()

In order to call a function in a module that you've imported, you need to reference the module and function like so:

import Kleben_Tabelle

...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM