![](/img/trans.png)
[英]What is the best way to send a list of arguments to a function within a class in Python?
[英]What is the best way to Importing a list from a python function into tkinter GUI
我有以下 pyhton 脚本正在运行并返回一些列表:
如何使用 python 脚本(第一个代码)的 output 在 GUI(第二个代码)中触发一个动作(第一个代码)是否有一般的方法来做到这一点
我使用 Pycharm 作为代码编辑器,我使用 Pycharm 内置函数运行脚本
import cv2
import time
import os
import HandTrackingModule as htm
def fcf():
wCam, hCam = 640, 480
cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)
folderPath = "FingerImages"
myList = os.listdir(folderPath)
#print(myList)
overlayList = []
for imPath in myList:
image = cv2.imread(f'{folderPath}/{imPath}')
# print(f'{folderPath}/{imPath}')
overlayList.append(image)
#print(len(overlayList))
pTime = 0
detector = htm.handDetector(detectionCon=0.75)
tipIds = [4, 8, 12, 16, 20]
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPosition(img, draw=False)
ts = time.time()
#print(lmList)
if len(lmList) != 0:
fingers = []
satisfied = []
neutral = []
unsatisfied = []
# Thumb
if lmList[tipIds[0]][1] > lmList[tipIds[0] - 1][1]:
fingers.append(1)
else:
fingers.append(0)
# 4 Fingers
for id in range(1, 5):
if lmList[tipIds[id]][2] < lmList[tipIds[id] - 2][2]:
fingers.append(1)
else:
fingers.append(0)
#print(fingers)
totalFingers = fingers.count(1)
#print(totalFingers)
#h, w, c = overlayList[totalFingers - 1].shape
#img[0:h, 0:w] = overlayList[totalFingers - 1]
#cv2.rectangle(img, (20, 225), (170, 425), (0, 255, 0), cv2.FILLED)
#cv2.putText(img, str(totalFingers), (45, 375), cv2.FONT_HERSHEY_PLAIN,
# 10, (255, 0, 0), 25)
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, f'FPS: {int(fps)}', (400, 70), cv2.FONT_HERSHEY_PLAIN,
3, (255, 0, 0), 3)
cv2.imshow("Image", img)
cv2.waitKey(1)
if __name__=='main':
fcf()
我有以下 tkinter GUI:
from tkinter import *
from PIL import ImageTk, Image
from FingerCounter import *
root = Tk()
root.title('Customer Satisfaction Survey')
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
#root.geometry(f'{screen_width}x{screen_height}')
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.columnconfigure(2, weight=1)
#root.attributes('-fullscreen',True)
frame = Frame(root)
# Define the images
Img = ImageTk.PhotoImage(Image.open("coo.png"))
Img1 = ImageTk.PhotoImage(Image.open("Doo.png"))
Img2 = ImageTk.PhotoImage( Image.open("Wave.png"))
#
ImgLabel00 = Label(root, text=" \n \n \n \n\n\n\n\n\n\n\n \n \n \n \n ")
ImgLabel00.grid(row=0, column=0, padx=5, pady=5)
ImgLabel = Label(image=Img)
ImgLabel.grid(row=1, column=2, padx=5, pady=5)
ImgLabel1 = Label(image=Img1)
ImgLabel1.grid(row=1, column=0, padx=5, pady=5)
ImgLabel2 = Label(image=Img2)
ImgLabel2.grid(row=1, column=1, padx=5, pady=5)
# craeting a label widget
myLabel = Label(root, text=" ", font=("Arial", 35))
#myLabel1 = Label(root, text=" ")
myLabel2 = Label(root, text=" ", font=("Arial", 35))
#myLabel3 = Label(root, text=" ")
myLabel4 = Label(root, text=" ", font=("Arial", 35))
#shoving it into the screen
myLabel.grid(row=2, column=0)
#myLabel1.grid(row=0, column=2)
myLabel2.grid(row=2, column=1)
#myLabel3.grid(row=0, column=6)
myLabel4.grid(row=2, column=2)
root.mainloop()
我不确定我的方法是否最佳,但这就是我的工作方式:
我采用了多线程并创建了两个线程,一个运行 Tkinter GUI,第二个线程运行 python 脚本。
python 脚本将一个 function 的 output 写入文本文件,而第一个线程上的 GUI 读取相同的文本文件并触发操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.