I have this simple function for my raspberry pi and I made a python file it does work
factory = PiGPIOFactory(host='raspberry pi ip address')
Switch_1 = LED(18, pin_factory=factory)
Switch_2 = LED(23, pin_factory=factory)
Switch_3 = LED(24, pin_factory=factory)
Switch_1.on()
Switch_2.on()
Switch_3.on()
I tried to put it into another python file
and put it under a function which I called
def raspberry_pi():
but for whatever reason it doesn’t work I’m having a hard time understanding why
import requests
import webbrowser
from tkinter import *
import tkinter as tk
from tkinter import ttk
from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory
root = tk.Tk()
root.geometry("484x380+1300+200") # Size of the window
# admin.resizable(False, False) # disables resizing only
# admin.attributes("-toolwindow", True) # True=disables maximum and minimum not x
root.title('Administrative Tools')
statusbar = tk.StringVar()
label_statusbar = Label(root, borderwidth=8, height=1, fg='#000000', bd=1, relief=SUNKEN,
anchor=W, textvariable=statusbar, font=('Arial', 15))
label_statusbar.pack(side=BOTTOM, fill=X)
def admin_on_closing():
# this will ask are you sure yes or no
# if yes it will terminate the window
root.destroy()
def esp_easy_0():
http = "ip address /login" # this is the HTTP we are calling
tab = ", new=2" # this will add a new tab the 2 stands for second tab
try:
requests.get(http)
except requests.ConnectionError:
statusbar.set("Can't connect to the Device")
root.after(3000, lambda: statusbar.set(''))
else:
webbrowser.open(http + tab) # this opens up the web browser
def esp_easy_1():
http = "ip address/login" # this is the HTTP we are calling
tab = ", new=2" # this will add a new tab the 2 stands for second tab
try:
requests.get(http)
except requests.ConnectionError:
statusbar.set("Can't connect to the Device")
root.after(3000, lambda: statusbar.set(''))
else:
webbrowser.open(http + tab) # this opens up the web browser
def raspberry_pi():
factory = PiGPIOFactory(host='raspberry pi ip address')
Switch_1 = LED(18, pin_factory=factory)
Switch_2 = LED(23, pin_factory=factory)
Switch_3 = LED(24, pin_factory=factory)
Switch_1.on()
Switch_2.on()
Switch_3.on()
# Listbox1
label_header1 = Label(root, height=1, width=45, text='Administrative Tools',
background="#00FFFF", font=('Arial bold', 13))
label_header1.place(x=10, y=10)
my_listbox1 = tk.Listbox(root, selectmode=SINGLE,
height=10, width=50, justify="left",
bg='#cccccc', font=('Arial Bold', 13))
my_listbox1.place(x=10, y=37) # x=justify left to right
# Listbox2 no header and no x y location
# it is not needed here
# ( this is part of Maintenance_admin_Tools.py window
# it allows the calling of 'list_item_values2'
# this could be considered an invisible window )
my_listbox2 = tk.Listbox(root) # options are not needed here
# when the X is used to close the window it will call on_closing
root.protocol('WM_DELETE_WINDOW', admin_on_closing)
# path = r'C:WindowsSystem32'
# the * is multiplication so *1=1 *2=2
list_Sonoff_config = [r"ESP_EASY_0", r"ESP_EASY_1", r"ESP_EASY_",
r"ESP_EASY_", r"ESP_EASY_", r"ESP_EASY_", r"---",
r"raspberry_ i", ] * 1
my_listbox1.insert(END, *list_Sonoff_config)
def selecteditem():
selection = my_listbox1.curselection()
if selection == (0,):
esp_easy_0() # esp_easy_0()
elif selection == (1,):
esp_easy_1()
elif selection == (2,):
pass
elif selection == (3,):
pass
elif selection == (4,):
pass
elif selection == (5,):
pass
elif selection == (6,):
pass
elif selection == (7,):
raspberry_pi()
# when selected above the button will take you there
btn = Button(root, text='Select', width=8, borderwidth=8, font=('Arial Bold', 13), command=selecteditem)
btn.place(x=180, y=250)
my_listbox1.bind('<Double-1>', lambda event: selecteditem())
# sets the scrollbar vertically
verscrlbar = ttk.Scrollbar(root, orient='vertical', command=my_listbox1.yview)
# places the scrollbar on the right
verscrlbar.place(x=459 + 3, y=38, height=182 + 20) # the number after the + is fine-tuning
my_listbox1.configure(yscrollcommand=verscrlbar.set)
root.mainloop()
Under that function I can call the file and it will work
I just can’t put everything under the function I don’t understand what’s going on.
Well first off I posted the whole code because I was requested to
if I didn’t make myself clear I apologize for that
what I’m trying to do just for testing right now is turn on three LEDs on my raspberry pi
so I made this simple code that does so in python and called it lights_on and another one lights_off
and it works perfectly
so I made a another python file a menu and in that menu I called it raspberry pi and generated a function
I called the function raspberry_pi():
I put the contents of lights_on in that function
but for whatever reason it will not work
but if I use a simple command like open(lights_on) to call the lights_on file it will work
so I’m trying to find out why I can’t put the contents of lights_on under the function of raspberry_pi():