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()
11
  • You are asking how to import code from another file? Add import main on the top and then use the argument command = main.TakeSnapAndSave. Commented May 17, 2018 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. Commented May 17, 2018 at 18:36
  • I wouldn't ask to simply import code from another file...I could just google it. Commented May 17, 2018 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()) Commented May 17, 2018 at 18:42
  • 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() Commented May 17, 2018 at 19:14

1 Answer 1

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?

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

Comments

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.