Question

I can not retrieve a variable from "Entry" and then enter the variable to a function. For example, I want to retrieve "NbF" which is a variable "entry". I did in "set" and I can retrieve it with "get" but when I get a new text "entry" I do not get the new value.

This is my code:

#!/usr/local/bin/python
# -*- coding:utf-8 -*-
from Tkinter import *
import tkFileDialog, tkMessageBox

application = Tk()
application.title("Application Drone - Analyse Vidéo")


def getFile(): 
        varName = tkFileDialog.askopenfilename().encode('utf-8')
        Name.set('Path : ' + varName)

def convert(nbFrame):
    print 'ffmpeg -i pathVideo -r '+nbFrame+' -f image2 "temp%4d.png"' 

def detect():
    pass

def analyse():
    pass

frame1 = Frame(application, bg="blue",   width=560, height=100)
frame1.pack(side=TOP, fill=X)

frame2 = Frame(application, bg="red",   width=560, height=100)
frame2.pack(side=TOP, fill=X)

frame3 = Frame(application, bg="green",   width=560, height=100)
frame3.pack(side=TOP, fill=X)

frame4 = Frame(application, bg="yellow",   width=560, height=100)
frame4.pack(side=TOP, fill=X)

frame5 = Frame(application, bg="grey",    width=560, height=100)
frame5.pack(side=BOTTOM, fill=BOTH, expand=1)

#frame 1
Name=StringVar()
lab1 = Label(frame1, text="Step 1 : Recup video").grid(row=0,column=0)
butt1 = Button(frame1,text="browse",command=getFile).grid(row=0,column=1)
nameVideo = Label(frame1, textvariable=Name).grid(row=1,column=0)

#frame 2
NbF = StringVar()
NbPictures=StringVar()
lab2 = Label(frame2, text="Step 2 : Convert video to images").grid(row=0,column=0)
ent2 = Entry(frame2, textvariable=NbF).grid(row=0,column=1)
# ent2.pack()
NbF.set('nb frame')
recup2 = NbF.get()
butt2 = Button(frame2,text="convert",command=lambda :convert(recup2)).grid(row=0,column=2)
nbP = Label(frame2, textvariable=NbPictures).grid(row=1,column=0)

#frame 3
NbS=StringVar()
lab3 = Label(frame3, text="Step 3 : Detect shape").grid(row=0,column=0)
butt3 = Button(frame3,text="detect",command=lambda :detect).grid(row=0,column=1)
numberShape = Label(frame3, textvariable=NbS).grid(row=1,column=0)

#frame 4
NbC=StringVar()
lab4 = Label(frame4, text="Step 4 : Analyse QrCode").grid(row=0,column=0)
butt4 = Button(frame4,text="analyse",command=lambda :analyse).grid(row=0,column=1)
numberCoord = Label(frame4, textvariable=NbC).grid(row=1,column=0)

#frame 5
lab5 = Label(frame5, text="Step 5 : Send coordonates").grid(row=0,column=0)
butt5 = Button(frame5,text="send",command=lambda :send).grid(row=0,column=1)

application.mainloop()
Was it helpful?

Solution

Perhaps you meant:

butt2 = Button(frame2,text="convert",command=lambda :convert(NbF.get())).grid(row=0,column=2)

This defers calling NbF.get() until the button is actually pressed. As the code was before, you were calling NbF.get() before the button was created and then passing that value to the function whenever you pressed the button.


As a side note, butt2 in the above code will always be None which is almost certainly not what you wanted it to be. The reason for this is because Widget.grid always returns None. I would advise you to not create a widget and pack/grid it on the same line -- always split that into 2 lines:

butt2 = Button(frame2,text="convert",command=lambda :convert(NbF.get()))
butt2.grid(row=0,column=2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top