0

I don't have any experience with GUI in python.... So, I'll post the GUI code first.

from tkinter import*

def needTodo():
    #Enter your code

root = Tk()
root.title('Something')

# ******** MAIN MENU  ******** #

menu = Menu(root)
root.config(menu=menu)
root.minsize(320, 320)
root.geometry("320x320")

subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="Insert Image", command=needTodo)
subMenu.add_command(label="Web Cam", command=needTodo)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=needTodo)

editMenu = Menu(menu)
menu.add_cascade(label="Edit", command=editMenu)
editMenu.add_command(label="Redo", command=needTodo)

# *********** Toolbar ************ #

toolbar = Frame(root, bg="gray")

insertBar = Button(toolbar, text="Insert Image", command=needTodo)
insertBar.pack(side=LEFT, padx=2, pady=2)
printBar = Button(toolbar, text="Print", command=needTodo)
printBar.pack(side=RIGHT, padx=2, pady=2)

toolbar.pack(side=TOP, fill=X)

# ********* IMAGE BACKGROUND ************ #


canvas = Canvas(width=320, height=320, bg='white')
canvas.pack()
gif1 = PhotoImage(file='D:/Rotating_brain_colored.gif')
canvas.create_image(0, 0, image=gif1, anchor=NW)

# ********* STATUS BAR ************ #

status = Label(root, text="Preparing to do work....", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)


root.mainloop()

So, when in the sub menu, "web cam" option is clicked I want it to execute a function written in another file(main.py) in the same folder.

The function is called "TakeSnapAndSave()" which basically takes accesses the web cam and takes a pic under certain circumstances.

I want to keep the gui.py and main.py separate. How can I do that?

Thanks in advance.

main.py code:

import cv2
import numpy as np
import os
import matplotlib.pyplot as plt

cascade = cv2.CascadeClassifier('xcascade.xml')

def TakeSnapAndSave():
    cap = cv2.VideoCapture(0)

    num = 0
    while num<1000:
        ret, img = cap.read()
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

        cas = cascade.detectMultiScale(gray, 10, 10)
        for(x,y,w,h) in cas:
            cv2.rectangle(img,(x,y), (x+w,y+h),(255,255,0),5)
            cv2.imwrite('opencv'+str(num)+'.jpg',img)
            num = num+1
            cv2.imshow('img',img)
            cv2.waitKey(1000)
            cap.release()
            cv2.desrtoyAllWindows()
            break

TakeSnapAndSave()
Azazel
  • 167
  • 3
  • 12
  • You are asking how to import code from another file? Add `import main` on the top and then use the argument `command = main.TakeSnapAndSave`. – Novel May 17 '18 at 18:25
  • This doesn't do anything with the GUI...It just executes the "TakeSnapAndSave" function. I want it to just open the GUI and when I click the option "webcam" in the sub menu I want it to execute the function. – Azazel May 17 '18 at 18:36
  • I wouldn't ask to simply import code from another file...I could just google it. – Azazel May 17 '18 at 18:37
  • 1
    What I showed you is correct. It sounds like what you are trying includes the parenthesis. You need `subMenu.add_command(label="Web Cam", command=main.TakeSnapAndSave)` NOT `subMenu.add_command(label="Web Cam", command=main.TakeSnapAndSave())` – Novel May 17 '18 at 18:42
  • Well it simply executes the function.....It doesn't go to the GUI and let me choose the option.....I don't want the function to execute when I run gui.py. Rather I want it to show me the gui and when I click in the label "web cam" only then it should execute the function.. I floowed your instruction and it simply executes the function and opens up the web cam without allowing me to chose the option in sub menu. – Azazel May 17 '18 at 18:49
  • That means that you either included the `()` in the command, or you have an error in `main.py`, for instance some code outside of a function that's calling the TakeSnapAndSave function. I can't help you further unless you show us a [mcve]. – Novel May 17 '18 at 18:53
  • Well.....main.py uses a lot of libraries....like numpy and matplotlib.... – Azazel May 17 '18 at 18:57
  • I want an *example*, not your real code. Make an example that demonstrates your problem. A dummy GUI and a dummy TakeSnapAndSave function that just prints something. Read the link I sent. – Novel May 17 '18 at 19:01
  • Well gave the whole GUI and now added the main.py as well – Azazel May 17 '18 at 19:02
  • 1
    Well, like I guessed, you have code in main.py that's not in a function (last line). Use this as the last line for main.py: `if __name__ == '__main__': TakeSnapAndSave()` – Novel May 17 '18 at 19:14
  • Works now. Thanks. – Azazel May 17 '18 at 19:19

1 Answers1

1

The last line of code in your main.py file is calling the function TakeSnapAndSave when the file is imported rather than when the option is selected in the GUI. Remove that call to TakeSnapAndSave from main.py and Novel's advice should work:

subMenu.add_command(label="Web Cam", command=main.TakeSnapAndSave)

Check out the thread on guarding code from being automatically run: Why is Python running my module when I import it, and how do I stop it?

Ganimj
  • 61
  • 3