0

I am new to programming in python, and I need to make a Tkinter menu that has 3 buttons, each one should run a specific python script when pressed, these scripts were done with opencv, they perform face recognition, lets call them find1.py, find2.py and detect.py. find1 and find2 are irrelevant to my issue. Detect.py searches for a specific face in a video, if it does not recognize the face it will edit a .txt file and write "0" in it, if it does recognize it, it will write a "1". My Tkinter menu needs to have the 3 buttons (find1, find2 and Detect) in the upper part of the window and a button containing a "green.png" image in the center-bottom, if I press the button that runs Detect.py the it should run the program and check constantly the .txt file, in case it reads a "1" the "green.png" image will stay, however if it reads a "0" the image of the button has to change to a "red.png" image.

I did manage to make the buttons run my scripts using os.system, however I dont believe I have the knowledge to perform the image swap and the monitoring of the .txt file, I followed a couple of posts of people trying to open a file and read its contents but I could not manage to apply that to my script, I will be very thankful if somebody could tell me if I am writing my scripts properly and if you could show me how to perform the image swap and text monitoring.

from tkinter import *
import os
from PIL import Image,ImageTk

window = Tk()
window.geometry('350x150')
window.title('OPENCV2 MENU')

def run():
    os.system('python3 find1.py')
 
btn1 = Button(window, text="find1", bg="gray", fg="white",command=run)
btn1.grid(row = 0, column = 1)

def run1():
    os.system('python3 find2.py')
    
btn2 = Button(window, text="find2", bg="gray", fg="white",command=run1)
btn2.grid(row = 0, column = 2)

def run2():
    os.system('python3 Detect.py')

btn3 = Button(window, text="detect", bg="gray", fg="white",command=run2)
btn3.grid(row = 0, column = 3)


with open('access.txt', 'r') as docu:
    val = int(docu.read().strip())
    if val == 0:
      photo = Image.open(r"red.png")
    else:
        photo = Image.open(r"green.png")
            
image2 = photo.resize((100,110),Image.ANTIALIAS)
photo2 =ImageTk.PhotoImage(image2)
Button(window, text = 'Click Me', image = photo2).grid(row =2, column =2)


window.mainloop()


  • Suggest to return the result using `sys.exit(...)` inside `Detect.py` and then you can check the return value of `os.system('...')` to determine which image to be shown. It is easier to monitor the content of file "access.txt". – acw1668 Jul 13 '23 at 07:33

1 Answers1

0

First of all, you don't need to use os.system to run another python program, you can simply make a function in each .py file that runs the full content of this file. Then, all you need to do is import this .py (considering .py files are in the same directory) using :

import find1 

Then you will call your inside function (let's say it was called run):

btn1 = Button(window, text="find1", bg="gray", fg="white",command=find1.run)

Second of all, you don't need to use r in front of your path to png file, r is used for absolute paths and not relative paths :

photo = Image.open(r"C://user/yourname/desktop/myproject/red.png")
photo = Image.open("red.png")

Instead of using outside program images, importing the scripts inside python itself (see my first point) will allow you to return for each function a boolean (True / False) to avoid using an image like red or green. All you will need to do is :

def run(python_filename):
    if python_filename == "find2"):
        value = find2.run()

This way you won't need to use any reading techniques outside your current program.

m4lou
  • 47
  • 1
  • 10
  • `r` in `r"..."` means *raw string* (see official [document](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals)), it means that escape codes, like `\n`, inside the string will not be interpreted. It is nothing related to whether the string is absolute path or relative path. – acw1668 Jul 13 '23 at 07:38
  • Hello, thank you for taking your time to answer my question! I applied your coding advice in my script, however when I `import find1` it runs the program immediately and then opens my tkinter menu, is there something that I am doing wrong? – Bingusfather Jul 14 '23 at 04:23
  • Hi, maybe you have arguments in your function ? If yes, you might have used the line : `command=find1.run(a)` which results in the immediate execution of your function. To avoid so, use : `command=lambda : find1.run(a)`. See more at this link : https://stackoverflow.com/questions/3704568/tkinter-button-command-activates-upon-running-program – m4lou Jul 17 '23 at 08:09