我无法在bind
窗口中Button
到module
到tkinter
(实际上甚至没有显示窗口)
我import
module
可以运行,我想在单击按钮后而不是之前运行它。
我该怎么做,我们将不胜感激。这是我的GUI和模块代码
from Tkinter import*
import Tkinter as Tk
from DatasetCreator1 import DatasetCreator
class Buttons:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.TrainButton = Button(frame, text="Begin Train",
command = DatasetCreator.insertOrUpdate)
self.TrainButton.pack(side = LEFT)
self.quitButton = Button(frame, text = "Quit", command = frame.quit)
self.quitButton.pack(side = LEFT)
root = Tk()
b = Buttons(root)
root.mainloop()
运行此命令时,它将直接运行DatasetCreator1
,而不会显示任何tkinter
窗口。并要求输入:
enter your id
我的DatasetCreator1
是:
import cv2
import sqlite3
import numpy as np
cam = cv2.VideoCapture(0)
detector=cv2.CascadeClassifier('Classifiers/haarcascade_frontalface_default.xml');
class DatasetCreator():
def insertOrUpdate(Id,Name):
conn=sqlite3.connect("Facebase0.db")
cmd="SELECT * FROM People WHERE Id =" +str(Id)
cursor=conn.execute(cmd)
isRecordExist=0
for row in cursor:
isRecordExist=1
if(isRecordExist==1):
cmd="UPDATE people SET Name=' "+str(Name)+" ' WHERE ID="+str(Id)
else:
cmd="INSERT INTO people(ID,Name) Values("+str(Id)+",' "+str(Name)+" ' )"
conn.execute(cmd)
conn.commit()
conn.close()
ID = raw_input("enter your id")
name = raw_input("enter your name")
insertOrUpdate(ID,name)
sampleNum=0
while(True):
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
#incrementing sample number
sampleNum=sampleNum+1
#saving the captured face in the dataset folder
cv2.imwrite("dataSet/User."+ID +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
cv2.imshow('frame',img)
#wait for 100 miliseconds
if cv2.waitKey(100) & 0xFF == ord('q'):
break
# break if the sample number is morethan 20
elif sampleNum>20:
break
cam.release()
cv2.destroyAllWindows()
我的DatasetCreator1
的一项有趣功能是,它仍在运行input('enter your id')
即使删除函数调用
ID = raw_input("enter your id") # function call
name = raw_input("enter your name")
insertOrUpdate(ID,name)
sampleNum=0
或者在函数定义中我没有给出任何参数
class DatasetCreator():
def insertOrUpdate(Id,Name): # function definition
conn=sqlite3.connect("Facebase0.db")
cmd="SELECT * FROM People WHERE Id =" +str(Id)
cursor=conn.execute(cmd)